bip-schnorr
Advanced tools
Comparing version 0.6.2 to 0.6.3
{ | ||
"name": "bip-schnorr", | ||
"version": "0.6.2", | ||
"version": "0.6.3", | ||
"description": "Pure JavaScript implementation of the BIP schnorr signature scheme and the muSig multi-signature scheme", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -52,10 +52,15 @@ # Pure JavaScript implementation of BIP340 Schnorr Signatures for secp256k1 | ||
// signing | ||
// PrivateKey as BigInteger from bigi or valid hex string | ||
const privateKey = BigInteger.fromHex('B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF'); | ||
const privateKeyHex = 'B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF'; | ||
const message = Buffer.from('243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89', 'hex'); | ||
const createdSignature = schnorr.sign(privateKey, message); | ||
const createdSignatureFromHex = schnorr.sign(privateKeyHex, message); | ||
console.log('The signature is: ' + createdSignature.toString('hex')); | ||
console.log('The signature is: ' + createdSignatureFromHex.toString('hex')); | ||
// verifying | ||
const publicKey = Buffer.from('DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659', 'hex'); | ||
const signatureToVerify = Buffer.from('2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D96EF2BE1AF1CAE22BF6736FA9650DE69E7DA1D37F92C4A92FBC93CC28FDBDB84', 'hex'); | ||
const signatureToVerify = Buffer.from('6D461BEB2F2DA00027D884FD13A24E2AE85CAECCA8AAA2D41777217EC38FB4960A67D47BC4F0722754EDB0E9017072600FFE4030C2E73771DCD3773F46A62652', 'hex'); | ||
try { | ||
@@ -250,3 +255,3 @@ schnorr.verify(publicKey, message, signatureToVerify); | ||
### schnorr.sign(privateKey : BigInteger, message : Buffer) : Buffer | ||
### schnorr.sign(privateKey : BigInteger | string, message : Buffer) : Buffer | ||
Sign a 32-byte message with the private key, returning a 64-byte signature. | ||
@@ -253,0 +258,0 @@ |
@@ -56,5 +56,15 @@ const BigInteger = require('bigi'); | ||
const idxStr = (idx !== undefined ? '[' + idx + ']' : ''); | ||
if (!BigInteger.isBigInteger(privateKey)) { | ||
throw new Error('privateKey' + idxStr + ' must be a BigInteger'); | ||
if (!BigInteger.isBigInteger(privateKey) && !(typeof privateKey == 'string')) { | ||
throw new Error('privateKey' + idxStr + ' must be a BigInteger or valid hex string'); | ||
} | ||
if (typeof(privateKey) == 'string') { | ||
if (privateKey.match(/[^a-f^A-F^0-9]+/)) { | ||
throw new Error('privateKey must be a BigInteger or valid hex string'); | ||
} | ||
checkRange('privateKey', BigInteger.fromHex(privateKey)); | ||
return | ||
} | ||
checkRange('privateKey', privateKey); | ||
@@ -61,0 +71,0 @@ } |
@@ -20,6 +20,6 @@ const BigInteger = require('bigi'); | ||
function deterministicGetK0(privateKey, message) { | ||
function deterministicGetK0(privateKey, publicKey, message) { | ||
check.checkSignParams(privateKey, message); | ||
const h = convert.hash(concat([convert.intToBuffer(privateKey), message])); | ||
const h = taggedHash('BIP0340/nonce', concat([convert.intToBuffer(privateKey), publicKey, message])); | ||
const i = convert.bufferToInt(h); | ||
@@ -26,0 +26,0 @@ return i.mod(n); |
@@ -18,2 +18,3 @@ const BigInteger = require('bigi'); | ||
check.checkSignParams(privateKey, message); | ||
privateKey = typeof (privateKey) == 'string' ? BigInteger.fromHex(privateKey) : privateKey; | ||
@@ -32,3 +33,3 @@ const P = G.multiply(privateKey); | ||
} else { | ||
kPrime = math.deterministicGetK0(d, message); | ||
kPrime = math.deterministicGetK0(d, Px, message); | ||
} | ||
@@ -35,0 +36,0 @@ |
@@ -26,3 +26,5 @@ /* global describe, it, beforeEach */ | ||
it('can check sign params', () => { | ||
try { schnorr.sign('foo', m); } catch (e) { assertError(e, 'privateKey must be a BigInteger'); } | ||
try { schnorr.sign('foo', m); } catch (e) { assertError(e, 'privateKey must be a BigInteger or valid hex string'); } | ||
try { schnorr.sign('abdcefg', m) } catch(e) { assertError(e, 'privateKey must be a BigInteger or valid hex string') }; | ||
try { schnorr.sign('@!$%', m) } catch(e) { assertError(e, 'privateKey must be a BigInteger or valid hex string') }; | ||
try { schnorr.sign(BigInteger.valueOf(1), 'foo'); } catch (e) { assertError(e, 'message must be a Buffer'); } | ||
@@ -36,7 +38,10 @@ try { schnorr.sign(BigInteger.valueOf(1), Buffer.from([])); } catch (e) { assertError(e, 'message must be 32 bytes long'); } | ||
const privateKey = BigInteger.fromHex('B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF'); | ||
const privateKeyHexString = 'B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF'; | ||
const message = Buffer.from('243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89', 'hex'); | ||
const signatureToVerify = Buffer.from('2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D96EF2BE1AF1CAE22BF6736FA9650DE69E7DA1D37F92C4A92FBC93CC28FDBDB84', 'hex'); | ||
const signatureToVerify = Buffer.from('6D461BEB2F2DA00027D884FD13A24E2AE85CAECCA8AAA2D41777217EC38FB4960A67D47BC4F0722754EDB0E9017072600FFE4030C2E73771DCD3773F46A62652', 'hex'); | ||
const createdSignature = schnorr.sign(privateKey, message); | ||
const createdSignatureFromPrivKeyString = schnorr.sign(privateKeyHexString, message); | ||
assert.strictEqual(createdSignature.toString('hex'), signatureToVerify.toString('hex')); | ||
assert.strictEqual(createdSignatureFromPrivKeyString.toString('hex'), signatureToVerify.toString('hex')); | ||
}); | ||
@@ -58,3 +63,3 @@ }); | ||
const message = Buffer.from('243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89', 'hex'); | ||
const signatureToVerify = Buffer.from('2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D96EF2BE1AF1CAE22BF6736FA9650DE69E7DA1D37F92C4A92FBC93CC28FDBDB84', 'hex'); | ||
const signatureToVerify = Buffer.from('6D461BEB2F2DA00027D884FD13A24E2AE85CAECCA8AAA2D41777217EC38FB4960A67D47BC4F0722754EDB0E9017072600FFE4030C2E73771DCD3773F46A62652', 'hex'); | ||
const publicKey = Buffer.from('DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659', 'hex'); | ||
@@ -61,0 +66,0 @@ try { |
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
196658
3867
327