Skip to content

Commit

Permalink
truncate messages for signing
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrouid committed Mar 20, 2020
1 parent f1bc102 commit c4fb728
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ecdsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export function checkMessage(msg: Buffer): void {
assert(msg.length <= MAX_MSG_LENGTH, 'Message is too long');
}

export function truncateMsg(msg: Buffer): Buffer {
return msg.slice(0, MAX_MSG_LENGTH);
}

export function compress(publicKey: Buffer): Buffer {
return isNode() ? secp256k1Compress(publicKey) : ellipticCompress(publicKey);
}
Expand Down Expand Up @@ -101,6 +105,7 @@ export async function sign(
rsvSig = false
): Promise<Buffer> {
checkPrivateKey(privateKey);
msg = truncateMsg(msg);
checkMessage(msg);
return isNode()
? secp256k1Sign(msg, privateKey, rsvSig)
Expand All @@ -112,6 +117,7 @@ export async function recover(
sig: Buffer,
compressed = false
): Promise<Buffer> {
msg = truncateMsg(msg);
checkMessage(msg);
return isNode()
? secp256k1Recover(sig, msg, compressed)
Expand All @@ -124,6 +130,7 @@ export async function verify(
sig: Buffer
): Promise<null> {
checkPublicKey(publicKey);
msg = truncateMsg(msg);
checkMessage(msg);
const sigGood = isNode()
? secp256k1Verify(sig, msg, publicKey)
Expand Down

0 comments on commit c4fb728

Please sign in to comment.