@onekeyfe/kaspa-core-lib 1.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +32 -0
- package/README.md +59 -0
- package/index.d.ts +410 -0
- package/index.js +93 -0
- package/lib/address.js +792 -0
- package/lib/block/block.js +281 -0
- package/lib/block/blockheader.js +296 -0
- package/lib/block/index.js +4 -0
- package/lib/block/merkleblock.js +313 -0
- package/lib/crypto/bn.js +218 -0
- package/lib/crypto/ecdsa.js +296 -0
- package/lib/crypto/hash.js +85 -0
- package/lib/crypto/point.js +166 -0
- package/lib/crypto/random.js +57 -0
- package/lib/crypto/schnorr.js +287 -0
- package/lib/crypto/signature.js +441 -0
- package/lib/encoding/base58.js +70 -0
- package/lib/encoding/base58check.js +95 -0
- package/lib/encoding/bufferreader.js +202 -0
- package/lib/encoding/bufferwriter.js +155 -0
- package/lib/encoding/varint.js +72 -0
- package/lib/errors/index.js +61 -0
- package/lib/errors/spec.js +191 -0
- package/lib/hdprivatekey.js +646 -0
- package/lib/hdpublickey.js +496 -0
- package/lib/networks.js +277 -0
- package/lib/opcode.js +261 -0
- package/lib/privatekey.js +431 -0
- package/lib/publickey.js +375 -0
- package/lib/script/index.js +3 -0
- package/lib/script/interpreter.js +2107 -0
- package/lib/script/script.js +1163 -0
- package/lib/transaction/index.js +7 -0
- package/lib/transaction/input/index.js +6 -0
- package/lib/transaction/input/input.js +375 -0
- package/lib/transaction/input/multisig.js +221 -0
- package/lib/transaction/input/multisigscripthash.js +178 -0
- package/lib/transaction/input/publickey.js +91 -0
- package/lib/transaction/input/publickeyhash.js +100 -0
- package/lib/transaction/output.js +188 -0
- package/lib/transaction/sighash.js +534 -0
- package/lib/transaction/signature.js +89 -0
- package/lib/transaction/transaction.js +1384 -0
- package/lib/transaction/unspentoutput.js +109 -0
- package/lib/unit.js +238 -0
- package/lib/uri.js +223 -0
- package/lib/util/base32.js +63 -0
- package/lib/util/buffer.js +162 -0
- package/lib/util/convertBits.js +62 -0
- package/lib/util/js.js +110 -0
- package/lib/util/log.js +10 -0
- package/lib/util/preconditions.js +34 -0
- package/package.json +62 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Important references for schnorr implementation
|
|
4
|
+
// https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/2019-05-15-schnorr.md
|
|
5
|
+
// https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/2019-11-15-schnorrmultisig.md#wallet-implementation-guidelines
|
|
6
|
+
const secp256k1 = require('@noble/secp256k1');
|
|
7
|
+
var BN = require('./bn');
|
|
8
|
+
var Point = require('./point');
|
|
9
|
+
var Signature = require('./signature');
|
|
10
|
+
var PublicKey = require('../publickey');
|
|
11
|
+
var Random = require('./random');
|
|
12
|
+
var Hash = require('./hash');
|
|
13
|
+
var BufferUtil = require('../util/buffer');
|
|
14
|
+
var _ = require('lodash');
|
|
15
|
+
var $ = require('../util/preconditions');
|
|
16
|
+
|
|
17
|
+
var Schnorr = function Schnorr(obj) {
|
|
18
|
+
if (!(this instanceof Schnorr)) {
|
|
19
|
+
return new Schnorr(obj);
|
|
20
|
+
}
|
|
21
|
+
if (obj) {
|
|
22
|
+
this.set(obj);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Function written to ensure r part of signature is at least 32 bytes, when converting
|
|
28
|
+
* from a BN to type Buffer.
|
|
29
|
+
* The BN type naturally cuts off leading zeros, e.g.
|
|
30
|
+
* <BN: 4f92d8094f710bc11b93935ac157730dda26c5c2a856650dbd8ebcd730d2d4> 31 bytes
|
|
31
|
+
* Buffer <00 4f 92 d8 09 4f 71 0b c1 1b 93 93 5a c1 57 73 0d da 26 c5 c2 a8 56 65 0d bd 8e bc d7 30 d2 d4> 32 bytes
|
|
32
|
+
* Both types are equal, however Schnorr signatures must be a minimum of 64 bytes.
|
|
33
|
+
* In a previous implementation of this schnorr module, was resulting in 63 byte signatures.
|
|
34
|
+
* (Although it would have been verified, it's proper to ensure the min requirement)
|
|
35
|
+
* @param {*} s BN
|
|
36
|
+
* @return {Buffer}
|
|
37
|
+
*/
|
|
38
|
+
function getrBuffer(r) {
|
|
39
|
+
|
|
40
|
+
let rNaturalLength = r.toBuffer().length;
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
if (rNaturalLength < 32) {
|
|
44
|
+
return r.toBuffer({size: 32});
|
|
45
|
+
}
|
|
46
|
+
return r.toBuffer();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Function written to ensure s part of signature is at least 32 bytes, when converting
|
|
51
|
+
* from a BN to type Buffer.
|
|
52
|
+
* The BN type naturally cuts off leading zeros, e.g.
|
|
53
|
+
* <BN: 4f92d8094f710bc11b93935ac157730dda26c5c2a856650dbd8ebcd730d2d4> 31 bytes
|
|
54
|
+
* Buffer <00 4f 92 d8 09 4f 71 0b c1 1b 93 93 5a c1 57 73 0d da 26 c5 c2 a8 56 65 0d bd 8e bc d7 30 d2 d4> 32 bytes
|
|
55
|
+
* Both types are equal, however Schnorr signatures must be a minimum of 64 bytes.
|
|
56
|
+
* In a previous implementation of this schnorr module, was resulting in 63 byte signatures.
|
|
57
|
+
* (Although it would have been verified, it's proper to ensure the min requirement)
|
|
58
|
+
* @param {*} s BN
|
|
59
|
+
* @return {Buffer}
|
|
60
|
+
*/
|
|
61
|
+
function getsBuffer(s) {
|
|
62
|
+
let sNaturalLength = s.toBuffer().length;
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
if (sNaturalLength < 32) {
|
|
66
|
+
return s.toBuffer({size: 32});
|
|
67
|
+
}
|
|
68
|
+
return s.toBuffer();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/* jshint maxcomplexity: 9 */
|
|
72
|
+
Schnorr.prototype.set = function(obj) {
|
|
73
|
+
this.hashbuf = obj.hashbuf || this.hashbuf;
|
|
74
|
+
this.endian = obj.endian || this.endian; // the endianness of hashbuf
|
|
75
|
+
this.privkey = obj.privkey || this.privkey;
|
|
76
|
+
this.pubkey = obj.pubkey || (this.privkey ? this.privkey.publicKey : this.pubkey);
|
|
77
|
+
this.sig = obj.sig || this.sig;
|
|
78
|
+
this.verified = obj.verified || this.verified;
|
|
79
|
+
return this;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
Schnorr.prototype.privkey2pubkey = function() {
|
|
83
|
+
this.pubkey = this.privkey.toPublicKey();
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
Schnorr.prototype.toPublicKey = function() {
|
|
87
|
+
return this.privkey.toPublicKey();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
Schnorr.prototype.sign = function() {
|
|
91
|
+
var hashbuf = this.hashbuf;
|
|
92
|
+
var privkey = this.privkey;
|
|
93
|
+
var d = privkey.bn;
|
|
94
|
+
|
|
95
|
+
$.checkState(hashbuf && privkey && d, new Error('invalid parameters'));
|
|
96
|
+
$.checkState(BufferUtil.isBuffer(hashbuf) && hashbuf.length === 32, new Error('hashbuf must be a 32 byte buffer'));
|
|
97
|
+
|
|
98
|
+
var e = BN.fromBuffer(hashbuf, this.endian ? {
|
|
99
|
+
endian: this.endian
|
|
100
|
+
} : undefined);
|
|
101
|
+
|
|
102
|
+
var obj = this._findSignature(d, e);
|
|
103
|
+
obj.compressed = this.pubkey.compressed;
|
|
104
|
+
obj.isSchnorr = true;
|
|
105
|
+
|
|
106
|
+
this.sig = new Signature(obj);
|
|
107
|
+
return this;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Schnorr implementation used from bitcoinabc at https://reviews.bitcoinabc.org/D2501
|
|
112
|
+
*/
|
|
113
|
+
Schnorr.prototype._findSignature = function(d, e) {
|
|
114
|
+
// d is the private key;
|
|
115
|
+
// e is the message to be signed
|
|
116
|
+
|
|
117
|
+
let n = Point.getN();
|
|
118
|
+
let G = Point.getG();
|
|
119
|
+
|
|
120
|
+
$.checkState(!d.lte(new BN(0)), new Error('privkey out of field of curve'));
|
|
121
|
+
$.checkState(!d.gte(n), new Error('privkey out of field of curve'));
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
let k = this.nonceFunctionRFC6979(d.toBuffer({ size: 32 }), e.toBuffer({ size: 32 }));
|
|
125
|
+
|
|
126
|
+
let P = G.mul(d);
|
|
127
|
+
let R = G.mul(k);
|
|
128
|
+
|
|
129
|
+
// Find deterministic k
|
|
130
|
+
if(R.hasSquare()) {
|
|
131
|
+
k = k;
|
|
132
|
+
} else {
|
|
133
|
+
k = n.sub(k);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let r = R.getX();
|
|
137
|
+
let e0 = BN.fromBuffer(Hash.sha256(Buffer.concat([getrBuffer(r), Point.pointToCompressed(P), e.toBuffer({ size: 32 })])));
|
|
138
|
+
|
|
139
|
+
let s = ((e0.mul(d)).add(k)).mod(n);
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
r: r,
|
|
143
|
+
s: s
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
Schnorr.prototype.sigError = function() {
|
|
149
|
+
if (!BufferUtil.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) {
|
|
150
|
+
return 'hashbuf must be a 32 byte buffer';
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
let sigLength = getrBuffer(this.sig.r).length + getsBuffer(this.sig.s).length;
|
|
154
|
+
|
|
155
|
+
if(!(sigLength === 64 || sigLength === 65)) {
|
|
156
|
+
return 'signature must be a 64 byte or 65 byte array';
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let hashbuf = this.endian === 'little' ? BufferUtil.reverse(this.hashbuf) : this.hashbuf
|
|
160
|
+
|
|
161
|
+
let P = this.pubkey.point;
|
|
162
|
+
let G = Point.getG();
|
|
163
|
+
|
|
164
|
+
if(P.isInfinity()) return true;
|
|
165
|
+
|
|
166
|
+
let r = this.sig.r;
|
|
167
|
+
let s = this.sig.s;
|
|
168
|
+
|
|
169
|
+
let p = new BN('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 'hex');
|
|
170
|
+
let n = Point.getN();
|
|
171
|
+
|
|
172
|
+
if(r.gte(p) || s.gte(n)) {
|
|
173
|
+
// ("Failed >= condition")
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let Br = getrBuffer(this.sig.r);
|
|
178
|
+
let Bp = Point.pointToCompressed(P);
|
|
179
|
+
|
|
180
|
+
let hash = Hash.sha256(Buffer.concat([Br, Bp, hashbuf]));
|
|
181
|
+
let e = BN.fromBuffer(hash, 'big').umod(n);
|
|
182
|
+
|
|
183
|
+
let sG = G.mul(s);
|
|
184
|
+
let eP = P.mul(n.sub(e));
|
|
185
|
+
let R = sG.add(eP);
|
|
186
|
+
|
|
187
|
+
if(R.isInfinity() || !R.hasSquare() || !R.getX().eq(r)) {
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
return false;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
Schnorr.prototype.verify = function() {
|
|
194
|
+
|
|
195
|
+
if (!this.sigError()) {
|
|
196
|
+
this.verified = true;
|
|
197
|
+
} else {
|
|
198
|
+
this.verified = false;
|
|
199
|
+
}
|
|
200
|
+
return this;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* RFC6979 deterministic nonce generation used from https://reviews.bitcoinabc.org/D2501
|
|
205
|
+
* @param {Buffer} privkeybuf
|
|
206
|
+
* @param {Buffer} msgbuf
|
|
207
|
+
* @return k {BN}
|
|
208
|
+
*/
|
|
209
|
+
Schnorr.prototype.nonceFunctionRFC6979 = function(privkey, msgbuf) {
|
|
210
|
+
let V = Buffer.from("0101010101010101010101010101010101010101010101010101010101010101","hex");
|
|
211
|
+
let K = Buffer.from("0000000000000000000000000000000000000000000000000000000000000000","hex");
|
|
212
|
+
|
|
213
|
+
let blob = Buffer.concat([privkey, msgbuf, Buffer.from("", "ascii"), Buffer.from("Schnorr+SHA256 ", "ascii")]);
|
|
214
|
+
|
|
215
|
+
K = Hash.sha256hmac(Buffer.concat([V, Buffer.from('00', 'hex'), blob]), K);
|
|
216
|
+
V = Hash.sha256hmac(V,K);
|
|
217
|
+
|
|
218
|
+
K = Hash.sha256hmac(Buffer.concat([V,Buffer.from('01','hex'), blob]), K);
|
|
219
|
+
V = Hash.sha256hmac(V,K);
|
|
220
|
+
|
|
221
|
+
let k = new BN(0);
|
|
222
|
+
let T;
|
|
223
|
+
while (true) {
|
|
224
|
+
V = Hash.sha256hmac(V,K);
|
|
225
|
+
T = BN.fromBuffer(V);
|
|
226
|
+
|
|
227
|
+
k = T;
|
|
228
|
+
$.checkState(V.length >= 32, "V length should be >= 32");
|
|
229
|
+
if (k.gt(new BN(0)) && k.lt(Point.getN())) {
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
K = Hash.sha256hmac(Buffer.concat([V, Buffer.from("00", 'hex')]), K);
|
|
233
|
+
V = Hash.hmac(Hash.sha256, V, K);
|
|
234
|
+
}
|
|
235
|
+
return k;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
Schnorr.sign = function(hashbuf, privkey, endian) {
|
|
240
|
+
/*
|
|
241
|
+
return Schnorr().set({
|
|
242
|
+
hashbuf: hashbuf,
|
|
243
|
+
endian: endian,
|
|
244
|
+
privkey: privkey
|
|
245
|
+
}).sign().sig;
|
|
246
|
+
*/
|
|
247
|
+
//console.log(":::sighash:", hashbuf.toString("hex"))
|
|
248
|
+
let result = secp256k1.schnorr.sign(Buffer.from(hashbuf).toString('hex'), privkey.toString());
|
|
249
|
+
|
|
250
|
+
let sig = Signature.fromString(result.sig);
|
|
251
|
+
let b = sig.toBuffer("schnorr").toString("hex");
|
|
252
|
+
if(b.length < 128)
|
|
253
|
+
throw new Error(`Invalid Signature\nsecp256k1 sig:${result.sig}\nSignature.fromString:${b}`)
|
|
254
|
+
//console.log("schnorrsig_sign sig.length:", result.sig.length, b.length, b)
|
|
255
|
+
sig.compressed = true;
|
|
256
|
+
return sig;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
Schnorr.verify = function(hashbuf, sig, pubkey, endian) {
|
|
260
|
+
return true//TODO
|
|
261
|
+
/*
|
|
262
|
+
return Schnorr().set({
|
|
263
|
+
hashbuf: hashbuf,
|
|
264
|
+
endian: endian,
|
|
265
|
+
sig: sig,
|
|
266
|
+
pubkey: pubkey
|
|
267
|
+
}).verify().verified;
|
|
268
|
+
*/
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/*
|
|
272
|
+
let str = "B341907592EE417F7433FDBE357B44B6837E2425FBE5CC1DE201585F07CB892B00DBE36182F03F0784FAA4D00F0384749E7002CE56DFF1FDED79A14C47399624";
|
|
273
|
+
let buf = Buffer.from(str, "hex");
|
|
274
|
+
let obj = Signature.parseSchnorrEncodedSig(buf);
|
|
275
|
+
let _sig = new Signature();
|
|
276
|
+
_sig.r = obj.r;
|
|
277
|
+
_sig.s = obj.s;
|
|
278
|
+
_sig.isSchnorr = true;
|
|
279
|
+
let sig = Signature.fromString(str);
|
|
280
|
+
let s = sig.toBuffer("schnorr").toString("hex");
|
|
281
|
+
let s2 = _sig.toBuffer("schnorr").toString("hex");
|
|
282
|
+
let cc = _sig.s.toBuffer({size1:32})
|
|
283
|
+
console.log("s:", _sig.s, cc.toString("hex").toUpperCase(), cc.length)
|
|
284
|
+
//console.log("sig:", s.length, buf.length, buf, obj, _sig, s2, s)
|
|
285
|
+
*/
|
|
286
|
+
|
|
287
|
+
module.exports = Schnorr;
|