Skip to content

Commit e750936

Browse files
committed
fix: throw error if signature type not supported
1 parent 5cf0efa commit e750936

File tree

3 files changed

+50
-38
lines changed

3 files changed

+50
-38
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"ox": patch
3+
---
4+
5+
Throw error if signature envelope type not supported.

src/tempo/SignatureEnvelope.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,7 @@ describe('verify', () => {
18571857
})
18581858

18591859
describe('keychain', () => {
1860-
test('behavior: returns false for keychain signatures', () => {
1860+
test('error: throws for keychain signatures', () => {
18611861
const privateKey = Secp256k1.randomPrivateKey()
18621862
const secp256k1PublicKey = Secp256k1.getPublicKey({ privateKey })
18631863
const payload = '0xdeadbeef' as const
@@ -1869,12 +1869,14 @@ describe('verify', () => {
18691869
inner: innerEnvelope,
18701870
})
18711871

1872-
expect(
1872+
expect(() =>
18731873
SignatureEnvelope.verify(envelope, {
18741874
payload,
18751875
publicKey: secp256k1PublicKey,
18761876
}),
1877-
).toBe(false)
1877+
).toThrowErrorMatchingInlineSnapshot(
1878+
`[SignatureEnvelope.VerificationError: Unable to verify signature envelope of type "keychain".]`,
1879+
)
18781880
})
18791881
})
18801882
})

src/tempo/SignatureEnvelope.ts

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ export declare namespace validate {
957957
* Supports `secp256k1`, `p256`, and `webAuthn` signature types.
958958
*
959959
* :::warning
960-
* `keychain` signatures are not supported and will return `false`.
960+
* `keychain` signatures are not supported and will throw an error.
961961
* :::
962962
*
963963
* @example
@@ -1068,44 +1068,42 @@ export function verify(
10681068
})()
10691069
if (!address) return false
10701070

1071-
try {
1072-
const envelope = from(signature)
1073-
1074-
if (envelope.type === 'secp256k1') {
1075-
if (!address) return false
1076-
return ox_Secp256k1.verify({
1077-
address,
1078-
payload,
1079-
signature: envelope.signature,
1080-
})
1081-
}
1071+
const envelope = from(signature)
10821072

1083-
if (envelope.type === 'p256') {
1084-
const envelopeAddress = Address.fromPublicKey(envelope.publicKey)
1085-
if (!Address.isEqual(envelopeAddress, address)) return false
1086-
return ox_P256.verify({
1087-
hash: envelope.prehash,
1088-
publicKey: envelope.publicKey,
1089-
payload,
1090-
signature: envelope.signature,
1091-
})
1092-
}
1073+
if (envelope.type === 'secp256k1') {
1074+
if (!address) return false
1075+
return ox_Secp256k1.verify({
1076+
address,
1077+
payload,
1078+
signature: envelope.signature,
1079+
})
1080+
}
10931081

1094-
if (envelope.type === 'webAuthn') {
1095-
const envelopeAddress = Address.fromPublicKey(envelope.publicKey)
1096-
if (!Address.isEqual(envelopeAddress, address)) return false
1097-
return ox_WebAuthnP256.verify({
1098-
challenge: Hex.from(payload),
1099-
metadata: envelope.metadata,
1100-
publicKey: envelope.publicKey,
1101-
signature: envelope.signature,
1102-
})
1103-
}
1082+
if (envelope.type === 'p256') {
1083+
const envelopeAddress = Address.fromPublicKey(envelope.publicKey)
1084+
if (!Address.isEqual(envelopeAddress, address)) return false
1085+
return ox_P256.verify({
1086+
hash: envelope.prehash,
1087+
publicKey: envelope.publicKey,
1088+
payload,
1089+
signature: envelope.signature,
1090+
})
1091+
}
11041092

1105-
return false
1106-
} catch {
1107-
return false
1093+
if (envelope.type === 'webAuthn') {
1094+
const envelopeAddress = Address.fromPublicKey(envelope.publicKey)
1095+
if (!Address.isEqual(envelopeAddress, address)) return false
1096+
return ox_WebAuthnP256.verify({
1097+
challenge: Hex.from(payload),
1098+
metadata: envelope.metadata,
1099+
publicKey: envelope.publicKey,
1100+
signature: envelope.signature,
1101+
})
11081102
}
1103+
1104+
throw new VerificationError(
1105+
`Unable to verify signature envelope of type "${envelope.type}".`,
1106+
)
11091107
}
11101108

11111109
export declare namespace verify {
@@ -1173,3 +1171,10 @@ export class InvalidSerializedError extends Errors.BaseError {
11731171
})
11741172
}
11751173
}
1174+
1175+
/**
1176+
* Error thrown when a signature envelope fails to verify.
1177+
*/
1178+
export class VerificationError extends Errors.BaseError {
1179+
override readonly name = 'SignatureEnvelope.VerificationError'
1180+
}

0 commit comments

Comments
 (0)