@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.
Files changed (53) hide show
  1. package/LICENSE +32 -0
  2. package/README.md +59 -0
  3. package/index.d.ts +410 -0
  4. package/index.js +93 -0
  5. package/lib/address.js +792 -0
  6. package/lib/block/block.js +281 -0
  7. package/lib/block/blockheader.js +296 -0
  8. package/lib/block/index.js +4 -0
  9. package/lib/block/merkleblock.js +313 -0
  10. package/lib/crypto/bn.js +218 -0
  11. package/lib/crypto/ecdsa.js +296 -0
  12. package/lib/crypto/hash.js +85 -0
  13. package/lib/crypto/point.js +166 -0
  14. package/lib/crypto/random.js +57 -0
  15. package/lib/crypto/schnorr.js +287 -0
  16. package/lib/crypto/signature.js +441 -0
  17. package/lib/encoding/base58.js +70 -0
  18. package/lib/encoding/base58check.js +95 -0
  19. package/lib/encoding/bufferreader.js +202 -0
  20. package/lib/encoding/bufferwriter.js +155 -0
  21. package/lib/encoding/varint.js +72 -0
  22. package/lib/errors/index.js +61 -0
  23. package/lib/errors/spec.js +191 -0
  24. package/lib/hdprivatekey.js +646 -0
  25. package/lib/hdpublickey.js +496 -0
  26. package/lib/networks.js +277 -0
  27. package/lib/opcode.js +261 -0
  28. package/lib/privatekey.js +431 -0
  29. package/lib/publickey.js +375 -0
  30. package/lib/script/index.js +3 -0
  31. package/lib/script/interpreter.js +2107 -0
  32. package/lib/script/script.js +1163 -0
  33. package/lib/transaction/index.js +7 -0
  34. package/lib/transaction/input/index.js +6 -0
  35. package/lib/transaction/input/input.js +375 -0
  36. package/lib/transaction/input/multisig.js +221 -0
  37. package/lib/transaction/input/multisigscripthash.js +178 -0
  38. package/lib/transaction/input/publickey.js +91 -0
  39. package/lib/transaction/input/publickeyhash.js +100 -0
  40. package/lib/transaction/output.js +188 -0
  41. package/lib/transaction/sighash.js +534 -0
  42. package/lib/transaction/signature.js +89 -0
  43. package/lib/transaction/transaction.js +1384 -0
  44. package/lib/transaction/unspentoutput.js +109 -0
  45. package/lib/unit.js +238 -0
  46. package/lib/uri.js +223 -0
  47. package/lib/util/base32.js +63 -0
  48. package/lib/util/buffer.js +162 -0
  49. package/lib/util/convertBits.js +62 -0
  50. package/lib/util/js.js +110 -0
  51. package/lib/util/log.js +10 -0
  52. package/lib/util/preconditions.js +34 -0
  53. package/package.json +62 -0
@@ -0,0 +1,534 @@
1
+ 'use strict';
2
+
3
+ var buffer = require('buffer');
4
+
5
+ var Signature = require('../crypto/signature');
6
+ var Script = require('../script');
7
+ var Output = require('./output');
8
+ var BufferReader = require('../encoding/bufferreader');
9
+ var BufferWriter = require('../encoding/bufferwriter');
10
+ var BN = require('../crypto/bn');
11
+ var Hash = require('../crypto/hash');
12
+ var ECDSA = require('../crypto/ecdsa');
13
+ var Schnorr = require('../crypto/schnorr');
14
+ var $ = require('../util/preconditions');
15
+ var BufferUtil = require('../util/buffer');
16
+ var Interpreter = require('../script/interpreter');
17
+ var _ = require('lodash');
18
+ const JSUtil = require('../util/js');
19
+ const { blake2b } = require('@noble/hashes/blake2b')
20
+ //const { isBuffer } = require('lodash');
21
+
22
+ var SIGHASH_SINGLE_BUG = '0000000000000000000000000000000000000000000000000000000000000001';
23
+ var BITS_64_ON = 'ffffffffffffffff';
24
+
25
+ // By default, we sign with sighash_forkid
26
+ var DEFAULT_SIGN_FLAGS = Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID;
27
+
28
+
29
+ const TransactionSigningHashKey = Buffer.from("TransactionSigningHash");
30
+
31
+ const blake2b_256 = (buf, key)=>{
32
+ return Buffer.from(blake2b.create({dkLen: 32, key: key}).update(buf).digest());
33
+ }
34
+
35
+ var sighashForForkId = function(transaction, sighashType, inputNumber, subscript, satoshisBN) {
36
+ var input = transaction.inputs[inputNumber];
37
+ $.checkArgument(
38
+ satoshisBN instanceof BN,
39
+ 'For ForkId=0 signatures, satoshis or complete input must be provided'
40
+ );
41
+
42
+ function GetForkId() {
43
+ return 0; // In the UAHF, a fork id of 0 is used (see [4] REQ-6-2 NOTE 4)
44
+ };
45
+
46
+ function GetPrevoutHash(tx) {
47
+ var writer = new BufferWriter()
48
+
49
+ _.each(tx.inputs, function(input) {
50
+ writer.writeReverse(input.prevTxId);
51
+ writer.writeUInt32LE(input.outputIndex);
52
+ });
53
+
54
+ var buf = writer.toBuffer();
55
+ var ret = Hash.sha256sha256(buf);
56
+ return ret;
57
+ }
58
+
59
+ function GetSequenceHash(tx) {
60
+ var writer = new BufferWriter()
61
+
62
+ _.each(tx.inputs, function(input) {
63
+ writer.writeUInt32LE(input.sequenceNumber);
64
+ });
65
+
66
+ var buf = writer.toBuffer();
67
+ var ret = Hash.sha256sha256(buf);
68
+ return ret;
69
+ }
70
+
71
+ function GetOutputsHash(tx, n) {
72
+ var writer = new BufferWriter()
73
+
74
+ if (_.isUndefined(n)) {
75
+ _.each(tx.outputs, function(output) {
76
+ output.toBufferWriter(writer);
77
+ });
78
+ } else {
79
+ tx.outputs[n].toBufferWriter(writer);
80
+ }
81
+
82
+ var buf = writer.toBuffer();
83
+ var ret = Hash.sha256sha256(buf);
84
+ return ret;
85
+ }
86
+
87
+ var hashPrevouts = BufferUtil.emptyBuffer(32);
88
+ var hashSequence = BufferUtil.emptyBuffer(32);
89
+ var hashOutputs = BufferUtil.emptyBuffer(32);
90
+
91
+ if (!(sighashType & Signature.SIGHASH_ANYONECANPAY)) {
92
+ hashPrevouts = GetPrevoutHash(transaction);
93
+ }
94
+
95
+ if (!(sighashType & Signature.SIGHASH_ANYONECANPAY) &&
96
+ (sighashType & 31) != Signature.SIGHASH_SINGLE &&
97
+ (sighashType & 31) != Signature.SIGHASH_NONE) {
98
+ hashSequence = GetSequenceHash(transaction);
99
+ }
100
+
101
+ if ((sighashType & 31) != Signature.SIGHASH_SINGLE && (sighashType & 31) != Signature.SIGHASH_NONE) {
102
+ hashOutputs = GetOutputsHash(transaction);
103
+ } else if ((sighashType & 31) == Signature.SIGHASH_SINGLE && inputNumber < transaction.outputs.length) {
104
+ hashOutputs = GetOutputsHash(transaction, inputNumber);
105
+ }
106
+
107
+
108
+ function getHash(w) {
109
+
110
+ var buf = w.toBuffer();
111
+ var ret = Hash.sha256sha256(buf);
112
+ ret = new BufferReader(ret).readReverse();
113
+ return ret;
114
+ };
115
+
116
+
117
+
118
+ var writer = new BufferWriter()
119
+
120
+ // Version
121
+ writer.writeInt32LE(transaction.version);
122
+
123
+ // Input prevouts/nSequence (none/all, depending on flags)
124
+ writer.write(hashPrevouts);
125
+ writer.write(hashSequence);
126
+
127
+ // outpoint (32-byte hash + 4-byte little endian)
128
+ writer.writeReverse(input.prevTxId);
129
+ writer.writeUInt32LE(input.outputIndex);
130
+
131
+ // scriptCode of the input (serialized as scripts inside CTxOuts)
132
+ writer.writeVarintNum(subscript.toBuffer().length)
133
+ writer.write(subscript.toBuffer());
134
+
135
+ // value of the output spent by this input (8-byte little endian)
136
+ writer.writeUInt64LEBN(satoshisBN);
137
+
138
+ // nSequence of the input (4-byte little endian)
139
+ var sequenceNumber = input.sequenceNumber;
140
+ writer.writeUInt32LE(sequenceNumber);
141
+
142
+ // Outputs (none/one/all, depending on flags)
143
+ writer.write(hashOutputs);
144
+
145
+ // Locktime
146
+ writer.writeUInt32LE(transaction.nLockTime);
147
+
148
+ // sighashType
149
+ writer.writeUInt32LE(sighashType >>> 0);
150
+
151
+ var buf = writer.toBuffer();
152
+ var ret = Hash.sha256sha256(buf);
153
+ ret = new BufferReader(ret).readReverse();
154
+ return ret;
155
+ }
156
+
157
+ function isSighashAnyoneCanPay(sighashType){
158
+ return (sighashType & Signature.SIGHASH_ANYONECANPAY) === Signature.SIGHASH_ANYONECANPAY;
159
+ }
160
+
161
+ function isSighashSingle(sighashType){
162
+ return (sighashType & 31) === Signature.SIGHASH_SINGLE;
163
+ }
164
+
165
+ function isSighashNone(sighashType){
166
+ return (sighashType & 31) === Signature.SIGHASH_NONE;
167
+ }
168
+
169
+ function HashWriter(){
170
+ // this.hash = blake2b(32, TransactionSigningHashKey);
171
+ //let me = this;
172
+ this.hash = {
173
+ hash: blake2b.create({dkLen: 32, key: TransactionSigningHashKey}),
174
+ bw:new BufferWriter(),
175
+ update: function(buf){
176
+ /*
177
+ if(me.L){
178
+ if(!isBuffer(buf))
179
+ buf = Buffer.from(buf)
180
+ console.log("#### Write "+buf.toString("hex"))
181
+ }
182
+ */
183
+ this.bw.write(buf);
184
+ this.hash.update(buf);
185
+ },
186
+ digest: function(){
187
+ return Buffer.from(this.hash.digest());
188
+ },
189
+ };
190
+ }
191
+
192
+ HashWriter.prototype.writeUInt8 = function(value){
193
+ const buf = new BufferWriter();
194
+ buf.writeUInt8(value);
195
+ this.hash.update(buf.toBuffer());
196
+ }
197
+
198
+ HashWriter.prototype.writeUInt16LE = function(value){
199
+ const buf = new BufferWriter();
200
+ buf.writeUInt16LE(value);
201
+ this.hash.update(buf.toBuffer());
202
+ }
203
+
204
+ HashWriter.prototype.writeUInt32LE = function(value){
205
+ const buf = new BufferWriter();
206
+ buf.writeUInt32LE(value);
207
+ this.hash.update(buf.toBuffer());
208
+ }
209
+
210
+ HashWriter.prototype.writeUInt64LE = function(value){
211
+ const buf = new BufferWriter();
212
+ buf.writeUInt64LEBN(BN.fromNumber(value));
213
+ this.hash.update(buf.toBuffer());
214
+ }
215
+
216
+ HashWriter.prototype.writeVarBytes = function(buf){
217
+ this.writeUInt64LE(buf.length);
218
+ this.hash.update(buf);
219
+ }
220
+
221
+ HashWriter.prototype.writeHash = function(buf){
222
+ this.hash.update(buf);
223
+ }
224
+
225
+ HashWriter.prototype.finalize = function(){
226
+ return this.hash.digest();
227
+ }
228
+
229
+ function zeroHash(){
230
+ return Buffer.alloc(32)
231
+ }
232
+
233
+ function zeroSubnetworkID(){
234
+ return Buffer.alloc(20)
235
+ }
236
+
237
+ /**
238
+ * @param {Transaction} transaction
239
+ *
240
+ */
241
+ function getPreviousOutputsHash(transaction, sighashType, reusedValues){
242
+ if (isSighashAnyoneCanPay(sighashType)) {
243
+ return zeroHash()
244
+ }
245
+
246
+ if (_.isUndefined(reusedValues.previousOutputsHash)){
247
+ const hashWriter = new HashWriter();
248
+ transaction.inputs.forEach(input => hashOutpoint(hashWriter, input));
249
+ reusedValues.previousOutputsHash = hashWriter.finalize()
250
+ }
251
+
252
+ return reusedValues.previousOutputsHash;
253
+ }
254
+
255
+ /**
256
+ * @param {Transaction} transaction
257
+ *
258
+ */
259
+ function getSequencesHash(transaction, sighashType, reusedValues){
260
+ if (isSighashSingle(sighashType) || isSighashAnyoneCanPay(sighashType) || isSighashNone(sighashType)) {
261
+ return zeroHash()
262
+ }
263
+
264
+ if (_.isUndefined(reusedValues.sequencesHash)){
265
+ const hashWriter = new HashWriter();
266
+ transaction.inputs.forEach(input => hashWriter.writeUInt64LE(input.sequenceNumber));
267
+ reusedValues.sequencesHash = hashWriter.finalize()
268
+ }
269
+
270
+ return reusedValues.sequencesHash;
271
+ }
272
+
273
+ function getSigOpCountsHash(transaction, sigHashType, reusedValues){
274
+ if (isSighashAnyoneCanPay(sigHashType)) {
275
+ return zeroHash()
276
+ }
277
+
278
+ if (_.isUndefined(reusedValues.sigOpCountsHash)){
279
+ const hashWriter = new HashWriter();
280
+ transaction.inputs.forEach(input => hashWriter.writeUInt8(1));//input.script.getSignatureOperationsCount())//sigOpCount));
281
+ reusedValues.sigOpCountsHash = hashWriter.finalize()
282
+ }
283
+
284
+ return reusedValues.sigOpCountsHash
285
+ }
286
+
287
+ /**
288
+ * @param {Transaction} transaction
289
+ *
290
+ */
291
+ function getOutputsHash(transaction, inputNumber, sighashType, reusedValues){
292
+ if (isSighashNone(sighashType)) {
293
+ return zeroHash()
294
+ }
295
+
296
+ // SigHashSingle: If the relevant output exists - return its hash, otherwise return zero-hash
297
+ if (isSighashSingle(sighashType)){
298
+ if (inputNumber >= transaction.outputs.length){
299
+ return zeroHash();
300
+ }
301
+
302
+ const hashWriter = new HashWriter();
303
+ return hashWriter.finalize();
304
+ }
305
+
306
+ if (_.isUndefined(reusedValues.outputsHash)){
307
+ const hashWriter = new HashWriter();
308
+ transaction.outputs.forEach(output => hashTxOut(hashWriter, output));
309
+ reusedValues.outputsHash = hashWriter.finalize()
310
+ }
311
+
312
+ return reusedValues.outputsHash;
313
+ }
314
+
315
+ /**
316
+ * @param {HashWriter} hashWriter
317
+ * @param {Input} input
318
+ *
319
+ */
320
+ function hashOutpoint(hashWriter, input){
321
+ hashWriter.writeHash(input.prevTxId);
322
+ hashWriter.writeUInt32LE(input.outputIndex);
323
+ }
324
+
325
+ /**
326
+ * @param {HashWriter} hashWriter
327
+ * @param {Output} output
328
+ *
329
+ */
330
+ function hashTxOut(hashWriter, output){
331
+ hashWriter.writeUInt64LE(output.satoshis);
332
+ hashWriter.writeUInt16LE(0); // TODO: USE REAL SCRIPT VERSION
333
+ hashWriter.writeVarBytes(output.script.toBuffer());
334
+ }
335
+
336
+ /**
337
+ * Returns a buffer of length 32 bytes with the hash that needs to be signed
338
+ * for OP_CHECKSIG.
339
+ *
340
+ * @name Signing.sighash
341
+ * @param {Transaction} transaction the transaction to sign
342
+ * @param {number} sighashType the type of the hash
343
+ * @param {number} inputNumber the input index for the signature
344
+ * @param {Script} subscript the script that will be signed
345
+ * @param {satoshisBN} input's amount (for ForkId signatures)
346
+ *
347
+ */
348
+ function sighash(transaction, sighashType, inputNumber, subscript, satoshisBN, flags, reusedValues = {}) {
349
+ const Transaction = require('./transaction');
350
+
351
+ const hashWriter = new HashWriter();
352
+ //hashWriter.L = true
353
+
354
+ hashWriter.writeUInt16LE(transaction.version)
355
+ hashWriter.writeHash(getPreviousOutputsHash(transaction, sighashType, reusedValues));
356
+ hashWriter.writeHash(getSequencesHash(transaction, sighashType, reusedValues));
357
+ hashWriter.writeHash(getSigOpCountsHash(transaction, sighashType, reusedValues))
358
+
359
+ const input = transaction.inputs[inputNumber];
360
+ hashOutpoint(hashWriter, input);
361
+ hashWriter.writeUInt16LE(0); // TODO: USE REAL SCRIPT VERSION
362
+ hashWriter.writeVarBytes(input.output.script.toBuffer());
363
+ hashWriter.writeUInt64LE(input.output.satoshis);
364
+ hashWriter.writeUInt64LE(input.sequenceNumber);
365
+ hashWriter.writeUInt8(1)//sigOpCount
366
+ hashWriter.writeHash(getOutputsHash(transaction, inputNumber, sighashType, reusedValues));
367
+ hashWriter.writeUInt64LE(transaction.nLockTime);
368
+ hashWriter.writeHash(zeroSubnetworkID()); // TODO: USE REAL SUBNETWORK ID
369
+ hashWriter.writeUInt64LE(0); // TODO: USE REAL GAS
370
+ hashWriter.writeHash(zeroHash()); // TODO: USE REAL PAYLOAD HASH
371
+ hashWriter.writeUInt8(sighashType);
372
+ return hashWriter.finalize();
373
+ }
374
+
375
+
376
+ var sighash2 = function sighash(transaction, sighashType, inputNumber, subscript, satoshisBN, flags) {
377
+
378
+ if (_.isUndefined(flags)) {
379
+ flags = DEFAULT_SIGN_FLAGS;
380
+ }
381
+
382
+ // Copy transaction
383
+ var txcopy = transaction;//Transaction.shallowCopy(transaction);
384
+ const inputs = transaction.inputs;
385
+ const outputs = transaction.outputs;
386
+ txcopy.inputs = [];
387
+
388
+ // Copy script
389
+ subscript = new Script(subscript);
390
+
391
+ if (flags & Interpreter.SCRIPT_ENABLE_REPLAY_PROTECTION) {
392
+ // Legacy chain's value for fork id must be of the form 0xffxxxx.
393
+ // By xoring with 0xdead, we ensure that the value will be different
394
+ // from the original one, even if it already starts with 0xff.
395
+ var forkValue = sighashType >> 8;
396
+ var newForkValue = 0xff0000 | (forkValue ^ 0xdead);
397
+ sighashType = (newForkValue << 8) | (sighashType & 0xff)
398
+ }
399
+
400
+ if ((sighashType & Signature.SIGHASH_FORKID) && (flags & Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID)) {
401
+ return sighashForForkId(txcopy, sighashType, inputNumber, subscript, satoshisBN);
402
+ }
403
+
404
+ // For no ForkId sighash, separators need to be removed.
405
+ subscript.removeCodeseparators();
406
+
407
+ var i;
408
+
409
+ for (i = 0; i < inputs.length; i++) {
410
+ // Blank signatures for other inputs
411
+ txcopy.inputs[i] = new Input(inputs[i]).setScript(Script.empty());
412
+ }
413
+
414
+ txcopy.inputs[inputNumber] = new Input(inputs[inputNumber]).setScript(subscript);
415
+
416
+ if ((sighashType & 31) === Signature.SIGHASH_NONE ||
417
+ (sighashType & 31) === Signature.SIGHASH_SINGLE) {
418
+
419
+ // clear all sequenceNumbers
420
+ for (i = 0; i < txcopy.inputs.length; i++) {
421
+ if (i !== inputNumber) {
422
+ txcopy.inputs[i].sequenceNumber = 0;
423
+ }
424
+ }
425
+ }
426
+
427
+ if ((sighashType & 31) === Signature.SIGHASH_NONE) {
428
+ txcopy.outputs = [];
429
+
430
+ } else if ((sighashType & 31) === Signature.SIGHASH_SINGLE) {
431
+ // The SIGHASH_SINGLE bug.
432
+ // https://bitcointalk.org/index.php?topic=260595.0
433
+ if (inputNumber >= txcopy.outputs.length) {
434
+ return Buffer.from(SIGHASH_SINGLE_BUG, 'hex');
435
+ }
436
+
437
+ //txcopy.outputs.length = inputNumber + 1;
438
+ txcopy.outputs = [];
439
+
440
+ for (i = 0; i < inputNumber; i++) {
441
+ txcopy.outputs[i] = new Output({
442
+ satoshis: BN.fromBuffer(Buffer.from(BITS_64_ON, 'hex')),
443
+ script: Script.empty()
444
+ });
445
+ }
446
+ }
447
+
448
+ if (sighashType & Signature.SIGHASH_ANYONECANPAY) {
449
+ txcopy.inputs = [txcopy.inputs[inputNumber]];
450
+ }
451
+
452
+ JSUtil.LogBufferActive = true;
453
+ let buf = new BufferWriter()
454
+ .write(txcopy.toBuffer())
455
+ .writeInt32LE(sighashType)
456
+ .toBuffer()
457
+ let ret = blake2b_256(buf, TransactionSigningHashKey);
458
+ JSUtil.LogBufferActive = false;
459
+ transaction.inputs = inputs;
460
+ transaction.outputs = outputs;
461
+ return Buffer.from(ret);
462
+ }
463
+
464
+
465
+ /**
466
+ * Create a signature
467
+ *
468
+ * @name Signing.sign
469
+ * @param {Transaction} transaction
470
+ * @param {PrivateKey} privateKey
471
+ * @param {number} sighash
472
+ * @param {number} inputIndex
473
+ * @param {Script} subscript
474
+ * @param {satoshisBN} input's amount
475
+ * @param {signingMethod} signingMethod "ecdsa" or "schnorr" to sign a tx
476
+ * @return {Signature}
477
+ */
478
+ function sign(transaction, privateKey, sighashType, inputIndex, subscript, satoshisBN, flags, signingMethod) {
479
+ //let ts0 = Date.now();
480
+ var hashbuf = sighash(transaction, sighashType, inputIndex, subscript, satoshisBN, flags);
481
+ //let ts1 = Date.now();
482
+ //console.log("#### sighash.sign", "inputIndex:", inputIndex, "sighash time:", ts1-ts0)
483
+ signingMethod = signingMethod || "ecdsa";
484
+ let sig;
485
+
486
+ if (signingMethod === "schnorr") {
487
+ sig = Schnorr.sign(hashbuf, privateKey, 'little').set({
488
+ nhashtype: sighashType
489
+ });
490
+ return sig;
491
+ } else if (signingMethod === "ecdsa") {
492
+ sig = ECDSA.sign(hashbuf, privateKey, 'little').set({
493
+ nhashtype: sighashType
494
+ });
495
+ return sig;
496
+ }
497
+ }
498
+
499
+ /**
500
+ * Verify a signature
501
+ *
502
+ * @name Signing.verify
503
+ * @param {Transaction} transaction
504
+ * @param {Signature} signature
505
+ * @param {PublicKey} publicKey
506
+ * @param {number} inputIndex
507
+ * @param {Script} subscript
508
+ * @param {satoshisBN} input's amount
509
+ * @param {flags} verification flags
510
+ * @param {signingMethod} signingMethod "ecdsa" or "schnorr" to sign a tx
511
+ * @return {boolean}
512
+ */
513
+ function verify(transaction, signature, publicKey, inputIndex, subscript, satoshisBN, flags, signingMethod) {
514
+ $.checkArgument(!_.isUndefined(transaction));
515
+ $.checkArgument(!_.isUndefined(signature) && !_.isUndefined(signature.nhashtype));
516
+ var hashbuf = sighash(transaction, signature.nhashtype, inputIndex, subscript, satoshisBN, flags);
517
+
518
+ signingMethod = signingMethod || "ecdsa";
519
+
520
+ if (signingMethod === "schnorr") {
521
+ return Schnorr.verify(hashbuf, signature, publicKey, 'little')
522
+ } else if (signingMethod === "ecdsa") {
523
+ return ECDSA.verify(hashbuf, signature, publicKey, 'little');
524
+ }
525
+ }
526
+
527
+ /**
528
+ * @namespace Signing
529
+ */
530
+ module.exports = {
531
+ sighash: sighash,
532
+ sign: sign,
533
+ verify: verify
534
+ };
@@ -0,0 +1,89 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash');
4
+ var $ = require('../util/preconditions');
5
+ var inherits = require('inherits');
6
+ var BufferUtil = require('../util/buffer');
7
+ var JSUtil = require('../util/js');
8
+
9
+ var PublicKey = require('../publickey');
10
+ var errors = require('../errors');
11
+ var Signature = require('../crypto/signature');
12
+
13
+ /**
14
+ * @desc
15
+ * Wrapper around Signature with fields related to signing a transaction specifically
16
+ *
17
+ * @param {Object|string|TransactionSignature} arg
18
+ * @constructor
19
+ */
20
+ function TransactionSignature(arg) {
21
+ if (!(this instanceof TransactionSignature)) {
22
+ return new TransactionSignature(arg);
23
+ }
24
+ if (arg instanceof TransactionSignature) {
25
+ return arg;
26
+ }
27
+ if (_.isObject(arg)) {
28
+ return this._fromObject(arg);
29
+ }
30
+ throw new errors.InvalidArgument('TransactionSignatures must be instantiated from an object');
31
+ }
32
+ inherits(TransactionSignature, Signature);
33
+
34
+ TransactionSignature.prototype._fromObject = function(arg) {
35
+ this._checkObjectArgs(arg);
36
+ this.publicKey = new PublicKey(arg.publicKey);
37
+ this.prevTxId = BufferUtil.isBuffer(arg.prevTxId) ? arg.prevTxId : Buffer.from(arg.prevTxId, 'hex');
38
+ this.outputIndex = arg.outputIndex;
39
+ this.inputIndex = arg.inputIndex;
40
+ this.signature = (arg.signature instanceof Signature) ? arg.signature :
41
+ BufferUtil.isBuffer(arg.signature) ? Signature.fromBuffer(arg.signature) :
42
+ Signature.fromString(arg.signature);
43
+ this.sigtype = arg.sigtype;
44
+ return this;
45
+ };
46
+
47
+ TransactionSignature.prototype._checkObjectArgs = function(arg) {
48
+ $.checkArgument(PublicKey(arg.publicKey), 'publicKey');
49
+ $.checkArgument(!_.isUndefined(arg.inputIndex), 'inputIndex');
50
+ $.checkArgument(!_.isUndefined(arg.outputIndex), 'outputIndex');
51
+ $.checkState(_.isNumber(arg.inputIndex), 'inputIndex must be a number');
52
+ $.checkState(_.isNumber(arg.outputIndex), 'outputIndex must be a number');
53
+ $.checkArgument(arg.signature, 'signature');
54
+ $.checkArgument(arg.prevTxId, 'prevTxId');
55
+ $.checkState(arg.signature instanceof Signature ||
56
+ BufferUtil.isBuffer(arg.signature) ||
57
+ JSUtil.isHexa(arg.signature), 'signature must be a buffer or hexa value');
58
+ $.checkState(BufferUtil.isBuffer(arg.prevTxId) ||
59
+ JSUtil.isHexa(arg.prevTxId), 'prevTxId must be a buffer or hexa value');
60
+ $.checkArgument(arg.sigtype, 'sigtype');
61
+ $.checkState(_.isNumber(arg.sigtype), 'sigtype must be a number');
62
+ };
63
+
64
+ /**
65
+ * Serializes a transaction to a plain JS object
66
+ * @return {Object}
67
+ */
68
+ TransactionSignature.prototype.toObject = TransactionSignature.prototype.toJSON = function toObject() {
69
+ return {
70
+ publicKey: this.publicKey.toString(),
71
+ prevTxId: this.prevTxId.toString('hex'),
72
+ outputIndex: this.outputIndex,
73
+ inputIndex: this.inputIndex,
74
+ signature: this.signature.toString(),
75
+ sigtype: this.sigtype
76
+ };
77
+ };
78
+
79
+ /**
80
+ * Builds a TransactionSignature from an object
81
+ * @param {Object} object
82
+ * @return {TransactionSignature}
83
+ */
84
+ TransactionSignature.fromObject = function(object) {
85
+ $.checkArgument(object);
86
+ return new TransactionSignature(object);
87
+ };
88
+
89
+ module.exports = TransactionSignature;