Skip to content

Commit 34af6d1

Browse files
authored
improve naming of identity/keys (#116)
1 parent ad11615 commit 34af6d1

File tree

8 files changed

+25
-29
lines changed

8 files changed

+25
-29
lines changed

packages/hypergraph-react/src/HypergraphAuthContext.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export function HypergraphAuthProvider({
145145
}
146146
}
147147

148-
async function loginWithKeys(keys: Identity.Keys, accountId: Address) {
148+
async function loginWithKeys(keys: Identity.IdentityKeys, accountId: Address) {
149149
const sessionToken = Identity.loadSyncServerSessionToken(storage, accountId);
150150
if (sessionToken) {
151151
// use whoami to check if the session token is still valid
@@ -213,7 +213,7 @@ export function HypergraphAuthProvider({
213213
}
214214

215215
async function signup(signer: Identity.Signer, accountId: Address) {
216-
const keys = Identity.createIdentity();
216+
const keys = Identity.createIdentityKeys();
217217
const { ciphertext, nonce } = await Identity.encryptIdentity(signer, accountId, keys);
218218
const { accountProof, keyProof } = await Identity.proveIdentityOwnership(signer, accountId, keys);
219219

@@ -368,5 +368,5 @@ type HypergraphAuthState = {
368368
authenticated: boolean;
369369
accountId: Address | null;
370370
sessionToken: string | null;
371-
keys: Identity.Keys | null;
371+
keys: Identity.IdentityKeys | null;
372372
};

packages/hypergraph/src/identity/auth-storage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Schema } from 'effect';
22
import { deserialize, serialize } from '../messages/index.js';
3-
import { type Keys, KeysSchema, type Storage } from './types.js';
3+
import { type IdentityKeys, KeysSchema, type Storage } from './types.js';
44

55
export const getEnv = (): 'dev' | 'production' | 'local' => {
66
return 'dev';
@@ -14,7 +14,7 @@ export const buildKeysStorageKey = (walletAddress: string) =>
1414
export const buildSessionTokenStorageKey = (walletAddress: string) =>
1515
walletAddress ? `hypergraph:${getEnv()}:session-token:${walletAddress}` : '';
1616

17-
export const loadKeys = (storage: Storage, walletAddress: string): Keys | null => {
17+
export const loadKeys = (storage: Storage, walletAddress: string): IdentityKeys | null => {
1818
const accessKey = buildKeysStorageKey(walletAddress);
1919
const val = storage.getItem(accessKey);
2020
if (!val) {
@@ -29,7 +29,7 @@ export const loadKeys = (storage: Storage, walletAddress: string): Keys | null =
2929
};
3030
};
3131

32-
export const storeKeys = (storage: Storage, walletAddress: string, keys: Keys) => {
32+
export const storeKeys = (storage: Storage, walletAddress: string, keys: IdentityKeys) => {
3333
const keysMsg = serialize(Schema.encodeSync(KeysSchema)(keys));
3434
storage.setItem(buildKeysStorageKey(walletAddress), keysMsg);
3535
};

packages/hypergraph/src/identity/create-identity.ts renamed to packages/hypergraph/src/identity/create-identity-keys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { secp256k1 } from '@noble/curves/secp256k1';
22

33
import { generateKeypair } from '../key/index.js';
44
import { bytesToHex } from '../utils/index.js';
5-
import type { Keys } from './types.js';
5+
import type { IdentityKeys } from './types.js';
66

7-
export const createIdentity = (): Keys => {
7+
export const createIdentityKeys = (): IdentityKeys => {
88
// generate a random private key for encryption
99
const { publicKey: encryptionPublicKey, secretKey: encryptionPrivateKey } = generateKeypair();
1010
// generate a random private key for signing

packages/hypergraph/src/identity/identity-encryption.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Hex } from 'viem';
44
import { verifyMessage } from 'viem';
55

66
import { bytesToHex, hexToBytes } from '../utils/index.js';
7-
import type { Keys, Signer } from './types.js';
7+
import type { IdentityKeys, Signer } from './types.js';
88

99
// Adapted from the XMTP approach to encrypt keys
1010
// See: https://github.com/xmtp/xmtp-js/blob/8d6e5a65813902926baac8150a648587acbaad92/sdks/js-sdk/src/keystore/providers/NetworkKeyManager.ts#L79-L116
@@ -17,7 +17,7 @@ const signatureMessage = (nonce: Uint8Array): string => {
1717
export const encryptIdentity = async (
1818
signer: Signer,
1919
accountId: string,
20-
keys: Keys,
20+
keys: IdentityKeys,
2121
): Promise<{ ciphertext: string; nonce: string }> => {
2222
const nonce = randomBytes(32);
2323
const message = signatureMessage(nonce);
@@ -51,7 +51,7 @@ export const decryptIdentity = async (
5151
accountId: string,
5252
ciphertext: string,
5353
nonce: string,
54-
): Promise<Keys> => {
54+
): Promise<IdentityKeys> => {
5555
const message = signatureMessage(hexToBytes(nonce));
5656
const signature = (await signer.signMessage(message)) as Hex;
5757

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export * from './auth-storage.js';
2-
export * from './create-identity.js';
2+
export * from './create-identity-keys.js';
33
export * from './identity-encryption.js';
44
export * from './prove-ownership.js';
55
export * from './types.js';

packages/hypergraph/src/identity/prove-ownership.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type Hex, verifyMessage } from 'viem';
22
import { privateKeyToAccount } from 'viem/accounts';
33

44
import { publicKeyToAddress } from '../utils/index.js';
5-
import type { Keys, Signer } from './types.js';
5+
import type { IdentityKeys, Signer } from './types.js';
66

77
export const getAccountProofMessage = (accountId: string, publicKey: string): string => {
88
return `This message proves I am the owner of the account ${accountId} and the public key ${publicKey}`;
@@ -15,7 +15,7 @@ export const getKeyProofMessage = (accountId: string, publicKey: string): string
1515
export const proveIdentityOwnership = async (
1616
signer: Signer,
1717
accountId: string,
18-
keys: Keys,
18+
keys: IdentityKeys,
1919
): Promise<{ accountProof: string; keyProof: string }> => {
2020
const publicKey = keys.signaturePublicKey;
2121
const accountProofMessage = getAccountProofMessage(accountId, publicKey);

packages/hypergraph/src/identity/types.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type Signer = {
1313
signMessage: SignMessage;
1414
};
1515

16-
export type Keys = {
16+
export type IdentityKeys = {
1717
encryptionPublicKey: string;
1818
encryptionPrivateKey: string;
1919
signaturePublicKey: string;
@@ -29,10 +29,6 @@ export const KeysSchema = Schema.Struct({
2929

3030
export type KeysSchema = Schema.Schema.Type<typeof KeysSchema>;
3131

32-
export type Identity = {
32+
export type Identity = IdentityKeys & {
3333
accountId: string;
34-
encryptionPublicKey: string;
35-
encryptionPrivateKey: string;
36-
signaturePublicKey: string;
37-
signaturePrivateKey: string;
3834
};

packages/hypergraph/test/identity/identity.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
wipeKeys,
1616
wipeSyncServerSessionToken,
1717
} from '../../src/identity/auth-storage.js';
18-
import { createIdentity } from '../../src/identity/create-identity.js';
18+
import { createIdentityKeys } from '../../src/identity/create-identity-keys.js';
1919
import { decryptIdentity, encryptIdentity } from '../../src/identity/identity-encryption.js';
2020
import { proveIdentityOwnership, verifyIdentityOwnership } from '../../src/identity/prove-ownership.js';
2121
import type { Signer } from '../../src/identity/types.js';
@@ -48,7 +48,7 @@ const accountSigner = (account: PrivateKeyAccount): Signer => {
4848

4949
describe('createIdentity', () => {
5050
it('should generate an identity with signing and encryption keys', () => {
51-
const id = createIdentity();
51+
const id = createIdentityKeys();
5252
expect(id).toBeDefined();
5353
expect(id.encryptionPublicKey).toBeDefined();
5454
expect(id.encryptionPrivateKey).toBeDefined();
@@ -60,7 +60,7 @@ describe('createIdentity', () => {
6060
});
6161
it('should generate an encryption keys able to encrypt and decrypt', () => {
6262
// Check that we can use the encryption keypair to encrypt and decrypt
63-
const id = createIdentity();
63+
const id = createIdentityKeys();
6464
const nonce = randomBytes(24);
6565
const message = new TextEncoder().encode('Hello, world!');
6666

@@ -82,7 +82,7 @@ describe('createIdentity', () => {
8282
});
8383
it('should generate a signature keys able to sign and verify', () => {
8484
// Check that we can use the signature keypair to sign and verify
85-
const id = createIdentity();
85+
const id = createIdentityKeys();
8686
const message = new TextEncoder().encode('Hello, world!');
8787
const sig = secp256k1.sign(message, hexToBytes(id.signaturePrivateKey));
8888
const valid = secp256k1.verify(sig, message, hexToBytes(id.signaturePublicKey));
@@ -97,7 +97,7 @@ describe('identity encryption', () => {
9797
const account = privateKeyToAccount(bytesToHex(randomBytes(32)) as Hex);
9898
const signer = accountSigner(account);
9999
const accountId = await signer.getAddress();
100-
const keys = createIdentity();
100+
const keys = createIdentityKeys();
101101
const { ciphertext, nonce } = await encryptIdentity(signer, accountId, keys);
102102
const decrypted = await decryptIdentity(signer, accountId, ciphertext, nonce);
103103

@@ -119,7 +119,7 @@ describe('auth/identity storage', () => {
119119
});
120120
it('stores, loads and wipes keys', () => {
121121
expect(loadKeys(storageMock, '0x1234')).toBeNull();
122-
const keys = createIdentity();
122+
const keys = createIdentityKeys();
123123
storeKeys(storageMock, '0x1234', keys);
124124
expect(loadKeys(storageMock, '0x1234')).toEqual(keys);
125125
wipeKeys(storageMock, '0x1234');
@@ -142,7 +142,7 @@ describe('identity ownership proofs', () => {
142142

143143
const signer = accountSigner(account);
144144
const accountId = await signer.getAddress();
145-
const keys = createIdentity();
145+
const keys = createIdentityKeys();
146146
const { accountProof, keyProof } = await proveIdentityOwnership(signer, accountId, keys);
147147

148148
const valid = await verifyIdentityOwnership(accountId, keys.signaturePublicKey, accountProof, keyProof);
@@ -153,14 +153,14 @@ describe('identity ownership proofs', () => {
153153
const account = privateKeyToAccount(bytesToHex(randomBytes(32)) as Hex);
154154
const signer = accountSigner(account);
155155
const accountId = await signer.getAddress();
156-
const keys = createIdentity();
156+
const keys = createIdentityKeys();
157157
const { accountProof, keyProof } = await proveIdentityOwnership(signer, accountId, keys);
158158

159159
// Create invalid proofs using a different account
160160
const account2 = privateKeyToAccount(bytesToHex(randomBytes(32)) as Hex);
161161
const signer2 = accountSigner(account2);
162162
const accountId2 = await signer2.getAddress();
163-
const keys2 = createIdentity();
163+
const keys2 = createIdentityKeys();
164164
const { accountProof: accountProof2, keyProof: keyProof2 } = await proveIdentityOwnership(
165165
signer2,
166166
accountId2,

0 commit comments

Comments
 (0)