Skip to content

Commit

Permalink
refactor: restore fallback verification
Browse files Browse the repository at this point in the history
also, replace ternary expression w/ if block
  • Loading branch information
iamcrazycoder committed Nov 3, 2023
1 parent c7aa890 commit 4a8a6e6
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions packages/sdk/src/signatures/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,32 @@ export async function signMessage(options: SignMessageOptions) {
}

export function verifyMessage(options: VerifyMessageOptions) {
return Address.isP2PKH(options.address)
? verify(options.message, options.address, options.signature)
: Verifier.verifySignature(options.address, options.message, options.signature)
try {
if (Address.isP2PKH(options.address)) {
return !verify(options.message, options.address, options.signature) ? fallbackVerification(options) : true
}

return Verifier.verifySignature(options.address, options.message, options.signature)
} catch (_) {
return false
}
}

function fallbackVerification({ message, address, signature }: VerifyMessageOptions) {
let isValid = false
const flags = [...Array(12).keys()].map((i) => i + 31)
for (const flag of flags) {
const flagByte = Buffer.alloc(1)
flagByte.writeInt8(flag)
let sigBuffer = Buffer.from(signature, "base64").slice(1)
sigBuffer = Buffer.concat([flagByte, sigBuffer])
const candidateSig = sigBuffer.toString("base64")
try {
isValid = verify(message, address, candidateSig)
if (isValid) break
} catch (e) {}
}
return isValid
}

export type SignMessageOptions = {
Expand Down

0 comments on commit 4a8a6e6

Please sign in to comment.