bitcoinjs-message
Advanced tools
Comparing version 2.1.4 to 2.2.0
@@ -6,2 +6,16 @@ interface SignatureOptions { | ||
export interface Signer { | ||
// param hash: 32 byte Buffer containing the digest of the message | ||
// param extraEntropy (optional): the 32 byte Buffer of the "extra data" part of RFC6979 nonces | ||
// returns object | ||
// attribute signature: 64 byte Buffer, first 32 R value, last 32 S value of ECDSA signature | ||
// attribute recovery: Number (integer) from 0 to 3 (inclusive), also known as recid, used for pubkey recovery | ||
sign(hash: Buffer, extraEntropy?: Buffer): { signature: Buffer; recovery: number; }; | ||
} | ||
export interface SignerAsync { | ||
// Same as Signer, but return is wrapped in a Promise | ||
sign(hash: Buffer, extraEntropy?: Buffer): Promise<{ signature: Buffer; recovery: number; }>; | ||
} | ||
export function magicHash( | ||
@@ -15,3 +29,3 @@ message: string | Buffer, | ||
message: string | Buffer, | ||
privateKey: Buffer, | ||
privateKey: Buffer | Signer, | ||
compressed?: boolean, | ||
@@ -22,3 +36,3 @@ sigOptions?: SignatureOptions | ||
message: string | Buffer, | ||
privateKey: Buffer, | ||
privateKey: Buffer | Signer, | ||
compressed?: boolean, | ||
@@ -29,2 +43,17 @@ messagePrefix?: string, | ||
// signAsync function is overloaded | ||
export function signAsync( | ||
message: string | Buffer, | ||
privateKey: Buffer | SignerAsync | Signer, | ||
compressed?: boolean, | ||
sigOptions?: SignatureOptions | ||
): Promise<Buffer>; | ||
export function signAsync( | ||
message: string | Buffer, | ||
privateKey: Buffer | SignerAsync | Signer, | ||
compressed?: boolean, | ||
messagePrefix?: string, | ||
sigOptions?: SignatureOptions | ||
): Promise<Buffer>; | ||
export function verify( | ||
@@ -31,0 +60,0 @@ message: string | Buffer, |
77
index.js
@@ -75,12 +75,9 @@ const bs58check = require('bs58check') | ||
function sign ( | ||
message, | ||
privateKey, | ||
compressed, | ||
messagePrefix, | ||
function prepareSign ( | ||
messagePrefixArg, | ||
sigOptions | ||
) { | ||
if (typeof messagePrefix === 'object' && sigOptions === undefined) { | ||
sigOptions = messagePrefix | ||
messagePrefix = undefined | ||
if (typeof messagePrefixArg === 'object' && sigOptions === undefined) { | ||
sigOptions = messagePrefixArg | ||
messagePrefixArg = undefined | ||
} | ||
@@ -107,4 +104,30 @@ let { segwitType, extraEntropy } = sigOptions || {} | ||
} | ||
const hash = magicHash(message, messagePrefix) | ||
const sigObj = secp256k1.sign(hash, privateKey, { data: extraEntropy }) | ||
return { | ||
messagePrefixArg, | ||
segwitType, | ||
extraEntropy | ||
} | ||
} | ||
function isSigner (obj) { | ||
return obj && typeof obj.sign === 'function' | ||
} | ||
function sign ( | ||
message, | ||
privateKey, | ||
compressed, | ||
messagePrefix, | ||
sigOptions | ||
) { | ||
const { | ||
messagePrefixArg, | ||
segwitType, | ||
extraEntropy | ||
} = prepareSign(messagePrefix, sigOptions) | ||
const hash = magicHash(message, messagePrefixArg) | ||
const sigObj = isSigner(privateKey) | ||
? privateKey.sign(hash, extraEntropy) | ||
: secp256k1.sign(hash, privateKey, { data: extraEntropy }) | ||
return encodeSignature( | ||
@@ -118,2 +141,30 @@ sigObj.signature, | ||
function signAsync ( | ||
message, | ||
privateKey, | ||
compressed, | ||
messagePrefix, | ||
sigOptions | ||
) { | ||
let messagePrefixArg, segwitType, extraEntropy | ||
return Promise.resolve().then(() => { | ||
({ | ||
messagePrefixArg, | ||
segwitType, | ||
extraEntropy | ||
} = prepareSign(messagePrefix, sigOptions)) | ||
const hash = magicHash(message, messagePrefixArg) | ||
return isSigner(privateKey) | ||
? privateKey.sign(hash, extraEntropy) | ||
: secp256k1.sign(hash, privateKey, { data: extraEntropy }) | ||
}).then((sigObj) => { | ||
return encodeSignature( | ||
sigObj.signature, | ||
sigObj.recovery, | ||
compressed, | ||
segwitType | ||
) | ||
}) | ||
} | ||
function segwitRedeemHash (publicKeyHash) { | ||
@@ -156,3 +207,6 @@ const redeemScript = Buffer.concat([ | ||
expected = bs58check.decode(address).slice(1) | ||
} else if (parsed.segwitType === SEGWIT_TYPES.P2WPKH) { | ||
} else { | ||
// parsed.segwitType === SEGWIT_TYPES.P2WPKH | ||
// must be true since we only return null, P2SH_P2WPKH, or P2WPKH | ||
// from the decodeSignature function. | ||
actual = publicKeyHash | ||
@@ -188,3 +242,4 @@ expected = decodeBech32(address) | ||
sign: sign, | ||
signAsync: signAsync, | ||
verify: verify | ||
} |
{ | ||
"name": "bitcoinjs-message", | ||
"version": "2.1.4", | ||
"version": "2.2.0", | ||
"description": "bitcoinjs-message", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13711
274