UNPKG

ox

Version:

Ethereum Standard Library

1,338 lines (1,160 loc) 115 kB
import { Address, Hex, P256, PublicKey, Rlp, Secp256k1, Signature, WebAuthnP256, WebCryptoP256, } from 'ox' import { describe, expect, test } from 'vp/test' import * as MultisigConfig from './MultisigConfig.js' import * as SignatureEnvelope from './SignatureEnvelope.js' const publicKey = PublicKey.from({ prefix: 4, x: '0xad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f5', y: '0x11edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0', }) const p256Signature = Signature.from({ r: '0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e', s: '0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35', yParity: 0, }) const signature_secp256k1 = Secp256k1.sign({ payload: '0xdeadbeef', privateKey: Secp256k1.randomPrivateKey(), }) const signature_p256 = SignatureEnvelope.from({ signature: p256Signature, publicKey, prehash: true, }) const signature_webauthn = SignatureEnvelope.from({ signature: p256Signature, publicKey, metadata: { authenticatorData: WebAuthnP256.getAuthenticatorData({ rpId: 'localhost' }), clientDataJSON: WebAuthnP256.getClientDataJSON({ challenge: '0xdeadbeef', origin: 'http://localhost', }), }, }) // Keychain signatures with different inner types const signature_keychain_secp256k1 = SignatureEnvelope.from({ userAddress: '0x1234567890123456789012345678901234567890', inner: SignatureEnvelope.from(signature_secp256k1), version: 'v2', }) const signature_keychain_p256 = SignatureEnvelope.from({ userAddress: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd', inner: signature_p256, version: 'v2', }) const signature_keychain_webauthn = SignatureEnvelope.from({ userAddress: '0xfedcbafedcbafedcbafedcbafedcbafedcbafedc', inner: signature_webauthn, version: 'v2', }) describe('assert', () => { describe('secp256k1', () => { test('behavior: validates valid signature', () => { expect(() => SignatureEnvelope.assert({ signature: signature_secp256k1, type: 'secp256k1', }), ).not.toThrow() }) test('behavior: validates signature without explicit type', () => { expect(() => SignatureEnvelope.assert({ signature: signature_secp256k1 }), ).not.toThrow() }) test('error: throws on invalid signature values', () => { expect(() => SignatureEnvelope.assert({ signature: { r: '0x0000000000000000000000000000000000000000000000000000000000000000', s: '0x0000000000000000000000000000000000000000000000000000000000000000', yParity: 2, }, type: 'secp256k1', }), ).toThrowErrorMatchingInlineSnapshot( `[Signature.InvalidYParityError: Value \`2\` is an invalid y-parity value. Y-parity must be 0 or 1.]`, ) }) }) describe('p256', () => { test('behavior: validates valid P256 signature', () => { expect(() => SignatureEnvelope.assert(signature_p256)).not.toThrow() }) test('behavior: validates P256 signature without explicit type', () => { const { type: _, ...signatureWithoutType } = signature_p256 expect(() => SignatureEnvelope.assert(signatureWithoutType)).not.toThrow() }) test('error: throws on invalid prehash type', () => { expect(() => SignatureEnvelope.assert({ ...signature_p256, prehash: 'true' as any, }), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "p256" is missing required properties: \`prehash\`. Provided: {"signature":{"r":"0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e","s":"0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35","yParity":0},"publicKey":{"prefix":4,"x":"0xad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f5","y":"0x11edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0"},"prehash":"true","type":"p256"}] `, ) }) test('error: throws on missing publicKey', () => { const { publicKey: _, ...withoutPublicKey } = signature_p256 expect(() => SignatureEnvelope.assert(withoutPublicKey as any), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "p256" is missing required properties: \`publicKey\`. Provided: {"signature":{"r":"0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e","s":"0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35","yParity":0},"prehash":true,"type":"p256"}] `, ) }) test('error: throws on missing signature.r', () => { const invalid = { signature: { s: '0x0000000000000000000000000000000000000000000000000000000000000001', } as any, publicKey, prehash: true, type: 'p256' as const, } expect(() => SignatureEnvelope.assert(invalid), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "p256" is missing required properties: \`signature.r\`. Provided: {"signature":{"s":"0x0000000000000000000000000000000000000000000000000000000000000001"},"publicKey":{"prefix":4,"x":"0xad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f5","y":"0x11edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0"},"prehash":true,"type":"p256"}] `, ) }) test('error: throws on missing signature.s', () => { const invalid = { signature: { r: '0x0000000000000000000000000000000000000000000000000000000000000001', } as any, publicKey, prehash: true, type: 'p256' as const, } expect(() => SignatureEnvelope.assert(invalid), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "p256" is missing required properties: \`signature.s\`. Provided: {"signature":{"r":"0x0000000000000000000000000000000000000000000000000000000000000001"},"publicKey":{"prefix":4,"x":"0xad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f5","y":"0x11edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0"},"prehash":true,"type":"p256"}] `, ) }) test('error: throws on missing publicKey.x', () => { const invalid = { signature: p256Signature, publicKey: { y: '0x0000000000000000000000000000000000000000000000000000000000000001', } as any, prehash: true, type: 'p256' as const, } expect(() => SignatureEnvelope.assert(invalid), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "p256" is missing required properties: \`publicKey.x\`. Provided: {"signature":{"r":"0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e","s":"0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35","yParity":0},"publicKey":{"y":"0x0000000000000000000000000000000000000000000000000000000000000001"},"prehash":true,"type":"p256"}] `, ) }) test('error: throws on missing publicKey.y', () => { const invalid = { signature: p256Signature, publicKey: { x: '0x0000000000000000000000000000000000000000000000000000000000000001', } as any, prehash: true, type: 'p256' as const, } expect(() => SignatureEnvelope.assert(invalid), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "p256" is missing required properties: \`publicKey.y\`. Provided: {"signature":{"r":"0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e","s":"0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35","yParity":0},"publicKey":{"x":"0x0000000000000000000000000000000000000000000000000000000000000001"},"prehash":true,"type":"p256"}] `, ) }) test('error: throws with all missing properties listed', () => { const invalid = { signature: {} as any, type: 'p256' as const, } expect(() => SignatureEnvelope.assert(invalid), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "p256" is missing required properties: \`signature.r\`, \`signature.s\`, \`prehash\`, \`publicKey\`. Provided: {"signature":{},"type":"p256"}] `, ) }) }) describe('webAuthn', () => { test('behavior: validates valid WebAuthn signature', () => { expect(() => SignatureEnvelope.assert(signature_webauthn)).not.toThrow() }) test('behavior: validates WebAuthn signature without explicit type', () => { const { type: _, ...signatureWithoutType } = signature_webauthn expect(() => SignatureEnvelope.assert(signatureWithoutType)).not.toThrow() }) test('error: throws on missing metadata', () => { const { metadata: _, ...withoutMetadata } = signature_webauthn expect(() => SignatureEnvelope.assert(withoutMetadata as any), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "webAuthn" is missing required properties: \`metadata\`. Provided: {"signature":{"r":"0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e","s":"0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35","yParity":0},"publicKey":{"prefix":4,"x":"0xad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f5","y":"0x11edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0"},"type":"webAuthn"}] `, ) }) test('error: throws on missing publicKey', () => { const { publicKey: _, ...withoutPublicKey } = signature_webauthn expect(() => SignatureEnvelope.assert(withoutPublicKey as any), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "webAuthn" is missing required properties: \`publicKey\`. Provided: {"signature":{"r":"0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e","s":"0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35","yParity":0},"metadata":{"authenticatorData":"0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000","clientDataJSON":"{\\"type\\":\\"webauthn.get\\",\\"challenge\\":\\"3q2-7w\\",\\"origin\\":\\"http://localhost\\",\\"crossOrigin\\":false}"},"type":"webAuthn"}] `, ) }) test('error: throws on missing signature.r', () => { const invalid = { signature: { s: '0x0000000000000000000000000000000000000000000000000000000000000001', } as any, publicKey, metadata: { authenticatorData: WebAuthnP256.getAuthenticatorData({ rpId: 'localhost', }), clientDataJSON: WebAuthnP256.getClientDataJSON({ challenge: '0xdeadbeef', origin: 'http://localhost', }), }, type: 'webAuthn' as const, } expect(() => SignatureEnvelope.assert(invalid), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "webAuthn" is missing required properties: \`signature.r\`. Provided: {"signature":{"s":"0x0000000000000000000000000000000000000000000000000000000000000001"},"publicKey":{"prefix":4,"x":"0xad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f5","y":"0x11edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0"},"metadata":{"authenticatorData":"0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000","clientDataJSON":"{\\"type\\":\\"webauthn.get\\",\\"challenge\\":\\"3q2-7w\\",\\"origin\\":\\"http://localhost\\",\\"crossOrigin\\":false}"},"type":"webAuthn"}] `, ) }) test('error: throws on missing signature.s', () => { const invalid = { signature: { r: '0x0000000000000000000000000000000000000000000000000000000000000001', } as any, publicKey, metadata: { authenticatorData: WebAuthnP256.getAuthenticatorData({ rpId: 'localhost', }), clientDataJSON: WebAuthnP256.getClientDataJSON({ challenge: '0xdeadbeef', origin: 'http://localhost', }), }, type: 'webAuthn' as const, } expect(() => SignatureEnvelope.assert(invalid), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "webAuthn" is missing required properties: \`signature.s\`. Provided: {"signature":{"r":"0x0000000000000000000000000000000000000000000000000000000000000001"},"publicKey":{"prefix":4,"x":"0xad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f5","y":"0x11edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0"},"metadata":{"authenticatorData":"0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000","clientDataJSON":"{\\"type\\":\\"webauthn.get\\",\\"challenge\\":\\"3q2-7w\\",\\"origin\\":\\"http://localhost\\",\\"crossOrigin\\":false}"},"type":"webAuthn"}] `, ) }) test('error: throws on missing metadata.authenticatorData', () => { const invalid = { signature: p256Signature, publicKey, metadata: { clientDataJSON: WebAuthnP256.getClientDataJSON({ challenge: '0xdeadbeef', origin: 'http://localhost', }), } as any, type: 'webAuthn' as const, } expect(() => SignatureEnvelope.assert(invalid), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "webAuthn" is missing required properties: \`metadata.authenticatorData\`. Provided: {"signature":{"r":"0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e","s":"0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35","yParity":0},"publicKey":{"prefix":4,"x":"0xad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f5","y":"0x11edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0"},"metadata":{"clientDataJSON":"{\\"type\\":\\"webauthn.get\\",\\"challenge\\":\\"3q2-7w\\",\\"origin\\":\\"http://localhost\\",\\"crossOrigin\\":false}"},"type":"webAuthn"}] `, ) }) test('error: throws on missing metadata.clientDataJSON', () => { const invalid = { signature: p256Signature, publicKey, metadata: { authenticatorData: WebAuthnP256.getAuthenticatorData({ rpId: 'localhost', }), } as any, type: 'webAuthn' as const, } expect(() => SignatureEnvelope.assert(invalid), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "webAuthn" is missing required properties: \`metadata.clientDataJSON\`. Provided: {"signature":{"r":"0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e","s":"0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35","yParity":0},"publicKey":{"prefix":4,"x":"0xad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f5","y":"0x11edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0"},"metadata":{"authenticatorData":"0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000"},"type":"webAuthn"}] `, ) }) test('error: throws on missing publicKey.x', () => { const invalid = { signature: p256Signature, publicKey: { y: '0x0000000000000000000000000000000000000000000000000000000000000001', } as any, metadata: { authenticatorData: WebAuthnP256.getAuthenticatorData({ rpId: 'localhost', }), clientDataJSON: WebAuthnP256.getClientDataJSON({ challenge: '0xdeadbeef', origin: 'http://localhost', }), }, type: 'webAuthn' as const, } expect(() => SignatureEnvelope.assert(invalid), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "webAuthn" is missing required properties: \`publicKey.x\`. Provided: {"signature":{"r":"0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e","s":"0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35","yParity":0},"publicKey":{"y":"0x0000000000000000000000000000000000000000000000000000000000000001"},"metadata":{"authenticatorData":"0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000","clientDataJSON":"{\\"type\\":\\"webauthn.get\\",\\"challenge\\":\\"3q2-7w\\",\\"origin\\":\\"http://localhost\\",\\"crossOrigin\\":false}"},"type":"webAuthn"}] `, ) }) test('error: throws on missing publicKey.y', () => { const invalid = { signature: p256Signature, publicKey: { x: '0x0000000000000000000000000000000000000000000000000000000000000001', } as any, metadata: { authenticatorData: WebAuthnP256.getAuthenticatorData({ rpId: 'localhost', }), clientDataJSON: WebAuthnP256.getClientDataJSON({ challenge: '0xdeadbeef', origin: 'http://localhost', }), }, type: 'webAuthn' as const, } expect(() => SignatureEnvelope.assert(invalid), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.MissingPropertiesError: Signature envelope of type "webAuthn" is missing required properties: \`publicKey.y\`. Provided: {"signature":{"r":"0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e","s":"0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35","yParity":0},"publicKey":{"x":"0x0000000000000000000000000000000000000000000000000000000000000001"},"metadata":{"authenticatorData":"0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000","clientDataJSON":"{\\"type\\":\\"webauthn.get\\",\\"challenge\\":\\"3q2-7w\\",\\"origin\\":\\"http://localhost\\",\\"crossOrigin\\":false}"},"type":"webAuthn"}] `, ) }) }) describe('keychain', () => { test('behavior: validates valid keychain with secp256k1 inner', () => { expect(() => SignatureEnvelope.assert(signature_keychain_secp256k1), ).not.toThrow() }) test('behavior: validates valid keychain with p256 inner', () => { expect(() => SignatureEnvelope.assert(signature_keychain_p256), ).not.toThrow() }) test('behavior: validates valid keychain with webAuthn inner', () => { expect(() => SignatureEnvelope.assert(signature_keychain_webauthn), ).not.toThrow() }) test('behavior: validates keychain without explicit type', () => { const { type: _, ...signatureWithoutType } = signature_keychain_secp256k1 expect(() => SignatureEnvelope.assert(signatureWithoutType)).not.toThrow() }) test('error: throws on invalid inner signature', () => { expect(() => SignatureEnvelope.assert({ userAddress: '0x1234567890123456789012345678901234567890', inner: SignatureEnvelope.from({ r: '0x0000000000000000000000000000000000000000000000000000000000000000', s: '0x0000000000000000000000000000000000000000000000000000000000000000', yParity: 2, }), type: 'keychain', } as any), ).toThrowErrorMatchingInlineSnapshot( `[Signature.InvalidYParityError: Value \`2\` is an invalid y-parity value. Y-parity must be 0 or 1.]`, ) }) }) test('error: throws on invalid envelope', () => { expect(() => SignatureEnvelope.assert({} as any), ).toThrowErrorMatchingInlineSnapshot( `[SignatureEnvelope.CoercionError: Unable to coerce value (\`{}\`) to a valid signature envelope.]`, ) }) test('error: throws on incomplete signature', () => { expect(() => SignatureEnvelope.assert({ r: '0x0000000000000000000000000000000000000000000000000000000000000000', s: '0x0000000000000000000000000000000000000000000000000000000000000000', } as any), ).toThrowErrorMatchingInlineSnapshot( `[SignatureEnvelope.CoercionError: Unable to coerce value (\`{"r":"0x0000000000000000000000000000000000000000000000000000000000000000","s":"0x0000000000000000000000000000000000000000000000000000000000000000"}\`) to a valid signature envelope.]`, ) }) }) describe('deserialize', () => { describe('secp256k1', () => { test('behavior: deserializes valid signature', () => { const serialized = Signature.toHex(signature_secp256k1) const envelope = SignatureEnvelope.deserialize(serialized) expect(envelope).toMatchObject({ signature: { r: signature_secp256k1.r, s: signature_secp256k1.s, yParity: signature_secp256k1.yParity, }, type: 'secp256k1', }) }) test('behavior: deserializes signature with magic identifier', () => { const serialized = SignatureEnvelope.serialize( { signature: signature_secp256k1, type: 'secp256k1' }, { magic: true }, ) const envelope = SignatureEnvelope.deserialize(serialized) expect(envelope).toMatchObject({ signature: { r: signature_secp256k1.r, s: signature_secp256k1.s, yParity: signature_secp256k1.yParity, }, type: 'secp256k1', }) }) test('error: throws on invalid size', () => { expect(() => SignatureEnvelope.deserialize('0xdeadbeef'), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.InvalidSerializedError: Unable to deserialize signature envelope: Unknown signature type identifier: 0xde. Expected 0x01 (P256), 0x02 (WebAuthn), 0x03 (Keychain V1), 0x04 (Keychain V2), or 0x05 (Multisig) Serialized: 0xdeadbeef] `, ) }) test('error: throws on invalid yParity', () => { // Signature with invalid yParity (must be 0 or 1) const invalidSig = '0x0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000102' expect(() => SignatureEnvelope.deserialize(invalidSig), ).toThrowErrorMatchingInlineSnapshot( `[Signature.InvalidYParityError: Value \`2\` is an invalid y-parity value. Y-parity must be 0 or 1.]`, ) }) }) describe('p256', () => { test('behavior: deserializes P256 signature', () => { const serialized = SignatureEnvelope.serialize(signature_p256) const deserialized = SignatureEnvelope.deserialize(serialized) expect(deserialized).toMatchObject({ signature: { r: signature_p256.signature.r, s: signature_p256.signature.s, }, publicKey: { x: signature_p256.publicKey.x, y: signature_p256.publicKey.y, }, prehash: signature_p256.prehash, type: 'p256', }) }) test('behavior: deserializes P256 signature with magic identifier', () => { const serialized = SignatureEnvelope.serialize(signature_p256, { magic: true, }) const deserialized = SignatureEnvelope.deserialize(serialized) expect(deserialized).toMatchObject({ signature: { r: signature_p256.signature.r, s: signature_p256.signature.s, }, publicKey: { x: signature_p256.publicKey.x, y: signature_p256.publicKey.y, }, prehash: signature_p256.prehash, type: 'p256', }) }) test('error: throws on invalid P256 signature length', () => { // P256 signature with wrong length (should be 130 bytes total, but only 100) const invalidSig = `0x01${'00'.repeat(100)}` as `0x${string}` expect(() => SignatureEnvelope.deserialize(invalidSig), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.InvalidSerializedError: Unable to deserialize signature envelope: Invalid P256 signature envelope size: expected 129 bytes, got 100 bytes Serialized: 0x0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000] `, ) }) }) describe('webAuthn', () => { test('behavior: deserializes WebAuthn signature', () => { const serialized = SignatureEnvelope.serialize(signature_webauthn) const deserialized = SignatureEnvelope.deserialize(serialized) expect(deserialized).toMatchObject({ signature: { r: signature_webauthn.signature.r, s: signature_webauthn.signature.s, }, publicKey: { x: signature_webauthn.publicKey.x, y: signature_webauthn.publicKey.y, }, metadata: { authenticatorData: signature_webauthn.metadata.authenticatorData, clientDataJSON: signature_webauthn.metadata.clientDataJSON, }, type: 'webAuthn', }) }) test('behavior: deserializes WebAuthn signature with magic identifier', () => { const serialized = SignatureEnvelope.serialize(signature_webauthn, { magic: true, }) const deserialized = SignatureEnvelope.deserialize(serialized) expect(deserialized).toMatchObject({ signature: { r: signature_webauthn.signature.r, s: signature_webauthn.signature.s, }, publicKey: { x: signature_webauthn.publicKey.x, y: signature_webauthn.publicKey.y, }, metadata: { authenticatorData: signature_webauthn.metadata.authenticatorData, clientDataJSON: signature_webauthn.metadata.clientDataJSON, }, type: 'webAuthn', }) }) test('error: throws on invalid WebAuthn signature length', () => { // WebAuthn signature too short (must be at least 129 bytes: 1 type + 128 signature data) const invalidSig = `0x02${'00'.repeat(100)}` as const expect(() => SignatureEnvelope.deserialize(invalidSig), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.InvalidSerializedError: Unable to deserialize signature envelope: Invalid WebAuthn signature envelope size: expected at least 128 bytes, got 100 bytes Serialized: 0x0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000] `, ) }) test('error: throws on invalid clientDataJSON', () => { // Create a signature with invalid JSON (not properly formatted) const invalidMetadata = { authenticatorData: `0x${'00'.repeat(37)}` as const, clientDataJSON: 'not-valid-json', } const serialized = SignatureEnvelope.serialize({ ...signature_webauthn, metadata: invalidMetadata, }) expect(() => SignatureEnvelope.deserialize(serialized), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.InvalidSerializedError: Unable to deserialize signature envelope: Unable to parse WebAuthn metadata: could not extract valid authenticatorData and clientDataJSON Serialized: 0x02000000000000000000000000000000000000000000000000000000000000000000000000006e6f742d76616c69642d6a736f6eccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35ad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f511edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0] `, ) }) test('error: throws on unknown type identifier', () => { const unknownType = `0xff${'00'.repeat(129)}` as const expect(() => SignatureEnvelope.deserialize(unknownType), ).toThrowErrorMatchingInlineSnapshot( ` [SignatureEnvelope.InvalidSerializedError: Unable to deserialize signature envelope: Unknown signature type identifier: 0xff. Expected 0x01 (P256), 0x02 (WebAuthn), 0x03 (Keychain V1), 0x04 (Keychain V2), or 0x05 (Multisig) Serialized: 0xff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000] `, ) }) }) describe('keychain', () => { test('behavior: deserializes keychain signature with secp256k1 inner', () => { const serialized = SignatureEnvelope.serialize( signature_keychain_secp256k1, ) const deserialized = SignatureEnvelope.deserialize(serialized) expect(deserialized).toMatchObject({ userAddress: signature_keychain_secp256k1.userAddress, inner: SignatureEnvelope.from(signature_secp256k1), type: 'keychain', }) }) test('behavior: deserializes keychain signature with p256 inner', () => { const serialized = SignatureEnvelope.serialize(signature_keychain_p256) const deserialized = SignatureEnvelope.deserialize(serialized) expect(deserialized).toMatchInlineSnapshot(` { "inner": { "prehash": true, "publicKey": { "prefix": 4, "x": "0xad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f5", "y": "0x11edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0", }, "signature": { "r": "0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e", "s": "0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35", }, "type": "p256", }, "type": "keychain", "userAddress": "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd", "version": "v2", } `) }) test('behavior: deserializes keychain signature with webAuthn inner', () => { const serialized = SignatureEnvelope.serialize( signature_keychain_webauthn, ) const deserialized = SignatureEnvelope.deserialize(serialized) expect(deserialized).toMatchInlineSnapshot(` { "inner": { "metadata": { "authenticatorData": "0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000", "clientDataJSON": "{"type":"webauthn.get","challenge":"3q2-7w","origin":"http://localhost","crossOrigin":false}", }, "publicKey": { "prefix": 4, "x": "0xad8ac16e167d6992c3e120d7f17d2376bc1cbcf30c46ba6dd00ce07303e742f5", "y": "0x11edf6ce1c32de66846f56afa7be1cbd729bc35750b6d0cdcf3ec9d75461aba0", }, "signature": { "r": "0xccbb3485d4726235f13cb15ef394fb7158179fb7b1925eccec0147671090c52e", "s": "0x77c3c53373cc1e3b05e7c23f609deb17cea8fe097300c45411237e9fe4166b35", }, "type": "webAuthn", }, "type": "keychain", "userAddress": "0xfedcbafedcbafedcbafedcbafedcbafedcbafedc", "version": "v2", } `) }) test('behavior: deserializes keychain signature with magic identifier', () => { const serialized = SignatureEnvelope.serialize( signature_keychain_secp256k1, { magic: true }, ) const deserialized = SignatureEnvelope.deserialize(serialized) expect(deserialized).toMatchObject({ userAddress: signature_keychain_secp256k1.userAddress, inner: SignatureEnvelope.from(signature_secp256k1), type: 'keychain', }) }) test('error: throws on invalid keychain signature length', () => { // Keychain signature too short (must be at least 21 bytes: 1 type + 20 address) const invalidSig = `0x03${'00'.repeat(10)}` as const expect(() => SignatureEnvelope.deserialize(invalidSig), ).toThrowErrorMatchingInlineSnapshot( `[Hex.SliceOffsetOutOfBoundsError: Slice starting at offset \`20\` is out-of-bounds (size: \`10\`).]`, ) }) }) }) describe('extractAddress', () => { describe('secp256k1', () => { test('default', () => { const privateKey = Secp256k1.randomPrivateKey() const address = Address.fromPublicKey( Secp256k1.getPublicKey({ privateKey }), ) const payload = '0xdeadbeef' as const const signature = Secp256k1.sign({ payload, privateKey }) const envelope = SignatureEnvelope.from(signature) expect( SignatureEnvelope.extractAddress({ payload, signature: envelope }), ).toBe(address) }) }) describe('p256', () => { test('default', () => { const privateKey = P256.randomPrivateKey() const pk = P256.getPublicKey({ privateKey }) const address = Address.fromPublicKey(pk) const payload = '0xdeadbeef' as const const signature = P256.sign({ payload, privateKey }) const envelope = SignatureEnvelope.from({ prehash: false, publicKey: pk, signature, }) expect( SignatureEnvelope.extractAddress({ payload, signature: envelope }), ).toBe(address) }) }) describe('webAuthn', () => { test('default', () => { const address = Address.fromPublicKey(publicKey) expect( SignatureEnvelope.extractAddress({ payload: '0xdeadbeef', signature: signature_webauthn, }), ).toBe(address) }) }) describe('keychain', () => { test('default', () => { const privateKey = Secp256k1.randomPrivateKey() const address = Address.fromPublicKey( Secp256k1.getPublicKey({ privateKey }), ) const payload = '0xdeadbeef' as const const signature = Secp256k1.sign({ payload, privateKey }) const envelope = SignatureEnvelope.from({ userAddress: '0x1234567890123456789012345678901234567890', inner: SignatureEnvelope.from(signature), }) expect( SignatureEnvelope.extractAddress({ payload, signature: envelope }), ).toBe(address) }) test('behavior: root = true returns userAddress', () => { expect( SignatureEnvelope.extractAddress({ payload: '0xdeadbeef', signature: signature_keychain_secp256k1, root: true, }), ).toBe('0x1234567890123456789012345678901234567890') }) }) }) describe('extractPublicKey', () => { describe('secp256k1', () => { test('default', () => { const privateKey = Secp256k1.randomPrivateKey() const pk = Secp256k1.getPublicKey({ privateKey }) const payload = '0xdeadbeef' as const const signature = Secp256k1.sign({ payload, privateKey }) const envelope = SignatureEnvelope.from(signature) expect( SignatureEnvelope.extractPublicKey({ payload, signature: envelope }), ).toEqual(pk) }) }) describe('p256', () => { test('default', () => { const privateKey = P256.randomPrivateKey() const pk = P256.getPublicKey({ privateKey }) const payload = '0xdeadbeef' as const const signature = P256.sign({ payload, privateKey }) const envelope = SignatureEnvelope.from({ prehash: false, publicKey: pk, signature, }) expect( SignatureEnvelope.extractPublicKey({ payload, signature: envelope }), ).toEqual(pk) }) }) describe('webAuthn', () => { test('default', () => { expect( SignatureEnvelope.extractPublicKey({ payload: '0xdeadbeef', signature: signature_webauthn, }), ).toEqual(publicKey) }) }) describe('keychain', () => { test('default', () => { const privateKey = Secp256k1.randomPrivateKey() const pk = Secp256k1.getPublicKey({ privateKey }) const payload = '0xdeadbeef' as const const signature = Secp256k1.sign({ payload, privateKey }) const envelope = SignatureEnvelope.from({ userAddress: '0x1234567890123456789012345678901234567890', inner: SignatureEnvelope.from(signature), }) expect( SignatureEnvelope.extractPublicKey({ payload, signature: envelope }), ).toEqual(pk) }) }) }) describe('from', () => { describe('secp256k1', () => { test('behavior: coerces from hex string', () => { const serialized = Signature.toHex(signature_secp256k1) const envelope = SignatureEnvelope.from(serialized) expect(envelope).toMatchObject({ signature: { r: signature_secp256k1.r, s: signature_secp256k1.s, yParity: signature_secp256k1.yParity, }, type: 'secp256k1', }) }) test('behavior: returns object as-is', () => { const envelope: SignatureEnvelope.SignatureEnvelope = { signature: signature_secp256k1, type: 'secp256k1', } const result = SignatureEnvelope.from(envelope) expect(result).toEqual(envelope) }) test('behavior: coerces from flat signature', () => { const result = SignatureEnvelope.from(signature_secp256k1) expect(result).toMatchObject({ signature: { r: signature_secp256k1.r, s: signature_secp256k1.s, yParity: signature_secp256k1.yParity, }, type: 'secp256k1', }) }) }) describe('p256', () => { test('behavior: coerces from hex string', () => { const serialized = SignatureEnvelope.serialize(signature_p256) const envelope = SignatureEnvelope.from(serialized) expect(envelope).toMatchObject({ signature: { r: signature_p256.signature.r, s: signature_p256.signature.s, }, type: 'p256', }) }) test('behavior: adds type to object', () => { const { type: _, ...withoutType } = signature_p256 const envelope = SignatureEnvelope.from(withoutType) expect(envelope.type).toBe('p256') }) }) describe('webAuthn', () => { test('behavior: coerces from hex string', () => { const serialized = SignatureEnvelope.serialize(signature_webauthn) const envelope = SignatureEnvelope.from(serialized) expect(envelope).toMatchObject({ signature: { r: signature_webauthn.signature.r, s: signature_webauthn.signature.s, }, type: 'webAuthn', }) }) test('behavior: adds type to object', () => { const { type: _, ...withoutType } = signature_webauthn const envelope = SignatureEnvelope.from(withoutType) expect(envelope.type).toBe('webAuthn') }) }) describe('keychain', () => { test('behavior: coerces from hex string with secp256k1 inner', () => { const serialized = SignatureEnvelope.serialize( signature_keychain_secp256k1, ) const envelope = SignatureEnvelope.from(serialized) expect(envelope).toMatchObject({ userAddress: signature_keychain_secp256k1.userAddress, inner: SignatureEnvelope.from(signature_secp256k1), type: 'keychain', }) }) test('behavior: coerces from hex string with p256 inner', () => { const serialized = SignatureEnvelope.serialize(signature_keychain_p256) const envelope = SignatureEnvelope.from(serialized) expect(envelope).toMatchObject({ userAddress: signature_keychain_p256.userAddress, type: 'keychain', }) }) test('behavior: adds type to object', () => { const { type: _, ...withoutType } = signature_keychain_secp256k1 const envelope = SignatureEnvelope.from(withoutType) expect(envelope.type).toBe('keychain') }) test('behavior: computes keyId from p256 inner publicKey', () => { const envelope = SignatureEnvelope.from({ userAddress: '0x1234567890123456789012345678901234567890', inner: signature_p256, }) expect(envelope.keyId).toBe(Address.fromPublicKey(publicKey)) }) test('behavior: computes keyId from webAuthn inner publicKey', () => { const envelope = SignatureEnvelope.from({ userAddress: '0x1234567890123456789012345678901234567890', inner: signature_webauthn, }) expect(envelope.keyId).toBe(Address.fromPublicKey(publicKey)) }) test('behavior: computes keyId from secp256k1 inner with payload', () => { const privateKey = Secp256k1.randomPrivateKey() const accessKeyPublicKey = Secp256k1.getPublicKey({ privateKey }) const payload = '0xdeadbeef' const sig = Secp256k1.sign({ payload, privateKey }) const envelope = SignatureEnvelope.from( { userAddress: '0x1234567890123456789012345678901234567890', inner: SignatureEnvelope.from(sig), }, { payload }, ) expect(envelope.keyId).toBe(Address.fromPublicKey(accessKeyPublicKey)) }) test('behavior: does not compute keyId for secp256k1 without payload', () => { const envelope = SignatureEnvelope.from({ userAddress: '0x1234567890123456789012345678901234567890', inner: SignatureEnvelope.from(signature_secp256k1), }) expect(envelope.keyId).toBeUndefined() }) test('behavior: preserves explicit keyId', () => { const explicitKeyId = '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' const envelope = SignatureEnvelope.from({ userAddress: '0x1234567890123456789012345678901234567890', inner: signature_p256, keyId: explicitKeyId, }) expect(envelope.keyId).toBe(explicitKeyId) }) }) describe('multisig', () => { const genesisConfig = MultisigConfig.from({ threshold: 1, owners: [ { owner: '0x1111111111111111111111111111111111111111', weight: 1, }, ], }) test('behavior: derives `account` from `genesisConfig`', () => { const envelope = SignatureEnvelope.from({ genesisConfig, signatures: [SignatureEnvelope.from(signature_secp256k1)], }) expect(envelope).toMatchObject({ type: 'multisig', account: MultisigConfig.getAddress(genesisConfig), }) expect('genesisConfig' in envelope).toBe(false) expect((envelope as SignatureEnvelope.Multisig).init).toBeUndefined() }) test('behavior: `init: true` opts into bootstrap (uses `genesisConfig` as `init`)', () => { const envelope = SignatureEnvelope.from({ genesisConfig, signatures: [SignatureEnvelope.from(signature_secp256k1)], init: true, }) expect((envelope as SignatureEnvelope.Multisig).init).toEqual( genesisConfig, ) }) test('behavior: `init` accepts an explicit config', () => { const otherConfig = MultisigConfig.from({ threshold: 1, owners: [ { owner: '0x2222222222222222222222222222222222222222', weight: 1, }, ], }) const envelope = SignatureEnvelope.from({ genesisConfig, signatures: [SignatureEnvelope.from(signature_secp256k1)], init: otherConfig, }) expect((envelope as SignatureEnvelope.Multisig).init).toEqual(otherConfig) }) test('behavior: `{ account }` form still works', () => { const account = MultisigConfig.getAddress(genesisConfig) const envelope = SignatureEnvelope.from({ account, signatures: [SignatureEnvelope.from(signature_secp256k1)], }) expect(envelope).toMatchObject({ type: 'multisig', account, }) }) }) }) describe('getType', () => { describe('secp256k1', () => { test('behavior: returns explicit type', () => { const envelope: SignatureEnvelope.SignatureEnvelope = { signature: { r: '0x0000000000000000000000000000000000000000000000000000000000000000', s: '0x0000000000000000000000000000000000000000000000000000000000000000', yParity: 0, }, type: 'secp256k1', } expect(SignatureEnvelope.getType(envelope)).toBe('secp256k1') }) test('behavior: infers type from properties', () => { expect( SignatureEnvelope.getType({ signature: signature_secp256k1 }), ).toBe('secp256k1') }) test('behavior: infers type from flat signature', () => { const signature = { r: '0x0000000000000000000000000000000000000000000000000000000000000000', s: '0x0000000000000000000000000000000000000000000000000000000000000000', yParity: 0, } expect(SignatureEnvelope.getType(signature)).toBe('secp256k1') }) }) describe('p256', () => { test('behavior: returns explicit type', () => { expect(SignatureEnvelope.getType(signature_p256)).toBe('p256') }) test('behavior: infers type from properties', () => { const { type: _, ...signatureWithoutType } = signature_p256 expect(SignatureEnvelope.getType(signatureWithoutType)).toBe('p256') }) }) describe('webAuthn', () => { test('behavior: returns explicit type', () => { expect(SignatureEnvelope.getType(signature_webauthn)).toBe('webAuthn') }) test('behavior: infers type from properties', () => { const { type: _, ...signatureWithoutType } = signature_webauthn expect(SignatureEnvelope.getType(signatureWithoutType)).toBe('webAuthn') }) }) describe('keychain', () => { test('behavior: returns explicit type', () => { expect(SignatureEnvelope.getType(signature_keychain_secp256k1)).toBe( 'keychain', ) }) test('behavior: infers type from properties', () => { const { type: _, ...signatureWithoutType } = signature_keychain_secp256k1 expect(SignatureEnvelope.getType(signatureWithoutType)).toBe('keychain') }) test('behavior: infers type for keychain with p256 inner', () => { const { type: _, ...signatureWithoutType } = signature_keychain_p256 expect(SignatureEnvelope.getType(signatureWithoutType)).toBe('keychain') }) test('behavior: infers type for keychain with webAuthn inner', () => { const { type: _, ...signatureWithoutType } = signature_keychain_webauthn expect(SignatureEnvelope.getType(signatureWithoutType)).toBe('keychain') }) }) test('error: throws on invalid envelope', () => { expect(() => SignatureEnvelope.getType({} as any), ).toThrowErrorMatchingInlineSnapshot( `[SignatureEnvelope.CoercionError: Unable to coerce value (\`{}\`) to a valid signature envelope.]`, ) }) test('error: throws on incomplete signature', () => { expect(() => SignatureEnvelope.getType({ r: '0x0000000000000000000000000000000000000000000000000000000000000000', s: '0x0000000000000000000000000000000000000000000000000000000000000000', } as any), ).toThrowErrorMatchingInlineSnapshot( `[SignatureEnvelope.CoercionError: Unable to coerce value (\`{"r":"0x0000000000000000000000000000000000000000000000000000000000000000","s":"0x0000000000000000000000000000000000000000000000000000000000000000"}\`) to a valid signature envelope.]`, ) }) }) describe('serialize', () => { describe('secp256k1', () => { test('behavior: serializes with explicit type', () => { const envelope: SignatureEnvelope.SignatureEnvelope = { signature: signature_secp256k1, type: 'secp256k1', } const serialized = SignatureEnvelope.serialize(envelope) expect(serialized).toBe(Signature.toHex(signature_secp256k1)) }) test('behavior: serializes without explicit type', () => { const serialized = SignatureEnvelope.serialize({ signature: signature_secp256k1, type: 'secp256k1', }) expect(serialized).toBe(Signature.toHex(signature_secp256k1)) }) test('behavior: serializes with magic identifier', () => { const envelope: SignatureEnvelope.SignatureEnvelope = { signature: signature_secp256k1, type: 'secp256k1', } const serialized = SignatureEnvelope.serialize(envelope, { magic: true }) expect(serialized.endsWith(SignatureEnvelope.magicBytes.slice(2))).toBe( true, ) expect(Hex.size(serialized)).toBe(65 + 32) // signature + magic identifier }) }) describe('p256', () => { test('behavior: serializes P256 signature with type identifier', () => { const serialized = SignatureEnvelope.serialize(signature_p256) // Should be 130 bytes: 1 (type) + 32 (r) + 32 (s) + 32 (pubKeyX) + 32 (pubKeyY) + 1 (prehash) expect(serialized.length).toBe(2 + 130 * 2) // 2 for '0x' prefix + 130 bytes * 2 hex chars // First byte should be P256 type identifier (0x01) expect(serialized.slice(0, 4)).toBe('0x01') }) test('behavior: serializes prehash flag correctly', () => { const withPreHashFalse = { ...signature_p256, prehash: false } const serialized = SignatureEnvelope.serialize(withPreHashFalse) // Last byte should be 0x00 for false expect(serialized.slice(-