1 | 'use strict';
|
2 |
|
3 | var Signature = require('../crypto/signature');
|
4 | var Script = require('../script');
|
5 | var Output = require('./output');
|
6 | var BufferReader = require('../encoding/bufferreader');
|
7 | var BufferWriter = require('../encoding/bufferwriter');
|
8 | var BN = require('../crypto/bn');
|
9 | var Hash = require('../crypto/hash');
|
10 | var ECDSA = require('../crypto/ecdsa');
|
11 | var $ = require('../util/preconditions');
|
12 | var _ = require('lodash');
|
13 | const schnorr = require('bip-schnorr');
|
14 |
|
15 | var SIGHASH_SINGLE_BUG = '0000000000000000000000000000000000000000000000000000000000000001';
|
16 | var BITS_64_ON = 'ffffffffffffffff';
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var sighash = function sighash(transaction, sighashType, inputNumber, subscript) {
|
29 | var Transaction = require('./transaction');
|
30 | var Input = require('./input');
|
31 |
|
32 | var i;
|
33 |
|
34 | var txcopy = Transaction.shallowCopy(transaction);
|
35 |
|
36 |
|
37 | subscript = new Script(subscript);
|
38 | subscript.removeCodeseparators();
|
39 |
|
40 | for (i = 0; i < txcopy.inputs.length; i++) {
|
41 |
|
42 | txcopy.inputs[i] = new Input(txcopy.inputs[i]).setScript(Script.empty());
|
43 | }
|
44 |
|
45 | txcopy.inputs[inputNumber] = new Input(txcopy.inputs[inputNumber]).setScript(subscript);
|
46 |
|
47 | if ((sighashType & 31) === Signature.SIGHASH_NONE ||
|
48 | (sighashType & 31) === Signature.SIGHASH_SINGLE) {
|
49 |
|
50 |
|
51 | for (i = 0; i < txcopy.inputs.length; i++) {
|
52 | if (i !== inputNumber) {
|
53 | txcopy.inputs[i].sequenceNumber = 0;
|
54 | }
|
55 | }
|
56 | }
|
57 |
|
58 | if ((sighashType & 31) === Signature.SIGHASH_NONE) {
|
59 | txcopy.outputs = [];
|
60 |
|
61 | } else if ((sighashType & 31) === Signature.SIGHASH_SINGLE) {
|
62 |
|
63 |
|
64 | if (inputNumber >= txcopy.outputs.length) {
|
65 | return Buffer.from(SIGHASH_SINGLE_BUG, 'hex');
|
66 | }
|
67 |
|
68 | txcopy.outputs.length = inputNumber + 1;
|
69 |
|
70 | for (i = 0; i < inputNumber; i++) {
|
71 | txcopy.outputs[i] = new Output({
|
72 | satoshis: BN.fromBuffer(Buffer.from(BITS_64_ON, 'hex')),
|
73 | script: Script.empty()
|
74 | });
|
75 | }
|
76 | }
|
77 |
|
78 | if (sighashType & Signature.SIGHASH_ANYONECANPAY) {
|
79 | txcopy.inputs = [txcopy.inputs[inputNumber]];
|
80 | }
|
81 |
|
82 | var buf = new BufferWriter()
|
83 | .write(txcopy.toBuffer())
|
84 | .writeInt32LE(sighashType)
|
85 | .toBuffer();
|
86 | var ret = Hash.sha256sha256(buf);
|
87 | ret = new BufferReader(ret).readReverse();
|
88 | return ret;
|
89 | };
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | function sign(transaction, privateKey, sighashType, inputIndex, subscript, signingMethod) {
|
104 | signingMethod = signingMethod || 'ecdsa';
|
105 |
|
106 | let hashbuf = sighash(transaction, sighashType, inputIndex, subscript);
|
107 | let sig;
|
108 | switch (signingMethod) {
|
109 | case 'ecdsa':
|
110 | sig = ECDSA.sign(hashbuf, privateKey, 'little').set({ nhashtype: sighashType });
|
111 | break;
|
112 | case 'schnorr':
|
113 | sig = schnorr.sign(privateKey.toString(), hashbuf);
|
114 | break;
|
115 | default:
|
116 | throw new Error("signingMethod not supported ", signingMethod);
|
117 | }
|
118 | return sig;
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | function verify(transaction, signature, publicKey, inputIndex, subscript, signingMethod) {
|
134 | $.checkArgument(!_.isUndefined(transaction), "Transaction Undefined");
|
135 | $.checkArgument(!_.isUndefined(signature) && !_.isUndefined(signature.nhashtype), "Signature Undefined");
|
136 |
|
137 | signingMethod = signingMethod || 'ecdsa';
|
138 | let hashbuf = sighash(transaction, signature.nhashtype, inputIndex, subscript);
|
139 | let verified = false;
|
140 |
|
141 | switch (signingMethod) {
|
142 | case 'ecdsa':
|
143 | verified = ECDSA.verify(hashbuf, signature, publicKey, 'little');
|
144 | break;
|
145 | case 'schnorr':
|
146 | verified = schnorr.verify(publicKey, hashbuf, signature);
|
147 | break;
|
148 | default:
|
149 | throw new Error("signingMethod not supported ", signingMethod);
|
150 | }
|
151 | return verified;
|
152 | }
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | module.exports = {
|
158 | sighash: sighash,
|
159 | sign: sign,
|
160 | verify: verify
|
161 | };
|