Skip to content

Commit

Permalink
fix(signature): add support for unisat
Browse files Browse the repository at this point in the history
  • Loading branch information
kodemon committed Aug 2, 2023
1 parent 2751047 commit 409558d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/Methods/Order/CreateOrder/Method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ export const createOrder = method({
);
break;
}
case "sliced": {
if (params.signature.pubkey === undefined) {
throw new BadRequestError("Signature format 'sliced' requires a public key");
}
validate.order.signature.ordit(
order.toHex(params.order),
`03${params.signature.pubkey}`,
params.signature.value,
lookup.btcnetwork
);
break;
}
case "core": {
validate.order.signature.core(order.toHex(params.order), params.order.maker, params.signature.value);
break;
Expand Down
25 changes: 23 additions & 2 deletions src/Validators/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,29 @@ function validateOrditSignature(message: string, key: string, signature: string,
* @param signature - Signature to verify.
*/
function validateCoreSignature(message: string, address: string, signature: string): void {
const verified = verifyMessage(message, address, signature, "", true);
if (verified === false) {
if (
verifyMessage(message, address, signature) === false &&
fallbackVerification({ message, address, signature }) === false
) {
throw new BadRequestError("Message signature is invalid");
}
}

function fallbackVerification({ message, address, signature }: any) {
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 = verifyMessage(message, address, candidateSig);
if (isValid) break;
} catch (_) {
// ...
}
}
return isValid;
}
2 changes: 1 addition & 1 deletion src/Validators/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const schema = {
type: Schema.either("receive" as const, "change" as const).error("Expected value to be 'receive' or 'change'"),
},
signature: {
format: Schema.either("psbt" as const, "ordit" as const, "core" as const).error(
format: Schema.either("psbt" as const, "ordit" as const, "sliced" as const, "core" as const).error(
"Expected value to be 'psbt', 'ordit', or 'core'"
),
},
Expand Down

0 comments on commit 409558d

Please sign in to comment.