Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/hypergraph-react/src/HypergraphAuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export function HypergraphAuthProvider({
}
}

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

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

Expand Down Expand Up @@ -368,5 +368,5 @@ type HypergraphAuthState = {
authenticated: boolean;
accountId: Address | null;
sessionToken: string | null;
keys: Identity.Keys | null;
keys: Identity.IdentityKeys | null;
};
6 changes: 3 additions & 3 deletions packages/hypergraph/src/identity/auth-storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Schema } from 'effect';
import { deserialize, serialize } from '../messages/index.js';
import { type Keys, KeysSchema, type Storage } from './types.js';
import { type IdentityKeys, KeysSchema, type Storage } from './types.js';

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

export const loadKeys = (storage: Storage, walletAddress: string): Keys | null => {
export const loadKeys = (storage: Storage, walletAddress: string): IdentityKeys | null => {
const accessKey = buildKeysStorageKey(walletAddress);
const val = storage.getItem(accessKey);
if (!val) {
Expand All @@ -29,7 +29,7 @@ export const loadKeys = (storage: Storage, walletAddress: string): Keys | null =
};
};

export const storeKeys = (storage: Storage, walletAddress: string, keys: Keys) => {
export const storeKeys = (storage: Storage, walletAddress: string, keys: IdentityKeys) => {
const keysMsg = serialize(Schema.encodeSync(KeysSchema)(keys));
storage.setItem(buildKeysStorageKey(walletAddress), keysMsg);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { secp256k1 } from '@noble/curves/secp256k1';

import { generateKeypair } from '../key/index.js';
import { bytesToHex } from '../utils/index.js';
import type { Keys } from './types.js';
import type { IdentityKeys } from './types.js';

export const createIdentity = (): Keys => {
export const createIdentityKeys = (): IdentityKeys => {
// generate a random private key for encryption
const { publicKey: encryptionPublicKey, secretKey: encryptionPrivateKey } = generateKeypair();
// generate a random private key for signing
Expand Down
6 changes: 3 additions & 3 deletions packages/hypergraph/src/identity/identity-encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Hex } from 'viem';
import { verifyMessage } from 'viem';

import { bytesToHex, hexToBytes } from '../utils/index.js';
import type { Keys, Signer } from './types.js';
import type { IdentityKeys, Signer } from './types.js';

// Adapted from the XMTP approach to encrypt keys
// See: https://github.com/xmtp/xmtp-js/blob/8d6e5a65813902926baac8150a648587acbaad92/sdks/js-sdk/src/keystore/providers/NetworkKeyManager.ts#L79-L116
Expand All @@ -17,7 +17,7 @@ const signatureMessage = (nonce: Uint8Array): string => {
export const encryptIdentity = async (
signer: Signer,
accountId: string,
keys: Keys,
keys: IdentityKeys,
): Promise<{ ciphertext: string; nonce: string }> => {
const nonce = randomBytes(32);
const message = signatureMessage(nonce);
Expand Down Expand Up @@ -51,7 +51,7 @@ export const decryptIdentity = async (
accountId: string,
ciphertext: string,
nonce: string,
): Promise<Keys> => {
): Promise<IdentityKeys> => {
const message = signatureMessage(hexToBytes(nonce));
const signature = (await signer.signMessage(message)) as Hex;

Expand Down
2 changes: 1 addition & 1 deletion packages/hypergraph/src/identity/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './auth-storage.js';
export * from './create-identity.js';
export * from './create-identity-keys.js';
export * from './identity-encryption.js';
export * from './prove-ownership.js';
export * from './types.js';
4 changes: 2 additions & 2 deletions packages/hypergraph/src/identity/prove-ownership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Hex, verifyMessage } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';

import { publicKeyToAddress } from '../utils/index.js';
import type { Keys, Signer } from './types.js';
import type { IdentityKeys, Signer } from './types.js';

export const getAccountProofMessage = (accountId: string, publicKey: string): string => {
return `This message proves I am the owner of the account ${accountId} and the public key ${publicKey}`;
Expand All @@ -15,7 +15,7 @@ export const getKeyProofMessage = (accountId: string, publicKey: string): string
export const proveIdentityOwnership = async (
signer: Signer,
accountId: string,
keys: Keys,
keys: IdentityKeys,
): Promise<{ accountProof: string; keyProof: string }> => {
const publicKey = keys.signaturePublicKey;
const accountProofMessage = getAccountProofMessage(accountId, publicKey);
Expand Down
8 changes: 2 additions & 6 deletions packages/hypergraph/src/identity/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Signer = {
signMessage: SignMessage;
};

export type Keys = {
export type IdentityKeys = {
encryptionPublicKey: string;
encryptionPrivateKey: string;
signaturePublicKey: string;
Expand All @@ -29,10 +29,6 @@ export const KeysSchema = Schema.Struct({

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

export type Identity = {
export type Identity = IdentityKeys & {
accountId: string;
encryptionPublicKey: string;
encryptionPrivateKey: string;
signaturePublicKey: string;
signaturePrivateKey: string;
};
18 changes: 9 additions & 9 deletions packages/hypergraph/test/identity/identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
wipeKeys,
wipeSyncServerSessionToken,
} from '../../src/identity/auth-storage.js';
import { createIdentity } from '../../src/identity/create-identity.js';
import { createIdentityKeys } from '../../src/identity/create-identity-keys.js';
import { decryptIdentity, encryptIdentity } from '../../src/identity/identity-encryption.js';
import { proveIdentityOwnership, verifyIdentityOwnership } from '../../src/identity/prove-ownership.js';
import type { Signer } from '../../src/identity/types.js';
Expand Down Expand Up @@ -48,7 +48,7 @@ const accountSigner = (account: PrivateKeyAccount): Signer => {

describe('createIdentity', () => {
it('should generate an identity with signing and encryption keys', () => {
const id = createIdentity();
const id = createIdentityKeys();
expect(id).toBeDefined();
expect(id.encryptionPublicKey).toBeDefined();
expect(id.encryptionPrivateKey).toBeDefined();
Expand All @@ -60,7 +60,7 @@ describe('createIdentity', () => {
});
it('should generate an encryption keys able to encrypt and decrypt', () => {
// Check that we can use the encryption keypair to encrypt and decrypt
const id = createIdentity();
const id = createIdentityKeys();
const nonce = randomBytes(24);
const message = new TextEncoder().encode('Hello, world!');

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

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

const signer = accountSigner(account);
const accountId = await signer.getAddress();
const keys = createIdentity();
const keys = createIdentityKeys();
const { accountProof, keyProof } = await proveIdentityOwnership(signer, accountId, keys);

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

// Create invalid proofs using a different account
const account2 = privateKeyToAccount(bytesToHex(randomBytes(32)) as Hex);
const signer2 = accountSigner(account2);
const accountId2 = await signer2.getAddress();
const keys2 = createIdentity();
const keys2 = createIdentityKeys();
const { accountProof: accountProof2, keyProof: keyProof2 } = await proveIdentityOwnership(
signer2,
accountId2,
Expand Down
Loading