forked from wallet42/node-forknote-util
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathbitcoin_utils.js
More file actions
290 lines (254 loc) · 8.94 KB
/
Copy pathbitcoin_utils.js
File metadata and controls
290 lines (254 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const bech32 = require('bech32');
const crypto = require('crypto');
const varuint = require('varuint-bitcoin');
const ADVANCED_TRANSACTION_MARKER = 0x00;
const ADVANCED_TRANSACTION_FLAG = 0x01;
const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
const BASE58_INDEXES = new Map(Array.from(BASE58_ALPHABET, (char, index) => [char, index]));
const MAX_BASE58_ADDRESS_LENGTH = 128;
const MAX_BECH32_ADDRESS_LENGTH = 128;
const UINT64_MAX = (1n << 64n) - 1n;
function sha256(buffer) {
return crypto.createHash('sha256').update(buffer).digest();
}
function hash256(buffer) {
return sha256(sha256(buffer));
}
function packInt32LE(num) {
const buff = Buffer.alloc(4);
buff.writeInt32LE(num, 0);
return buff;
}
function packUInt32LE(num) {
const buff = Buffer.alloc(4);
buff.writeUInt32LE(num, 0);
return buff;
}
function packUInt64LE(value) {
const amount = BigInt(value);
if (amount < 0n || amount > UINT64_MAX) throw new Error('Invalid uint64 value');
const buff = Buffer.alloc(8);
buff.writeUInt32LE(Number(amount & 0xffffffffn), 0);
buff.writeUInt32LE(Number(amount >> 32n), 4);
return buff;
}
function varIntBuffer(n) {
if (!Number.isSafeInteger(n) || n < 0) throw new Error('Invalid varint value');
return varuint.encode(n, Buffer.alloc(varuint.encodingLength(n)), 0);
}
function decodeBase58Check(value) {
if (typeof value !== 'string' || value.length > MAX_BASE58_ADDRESS_LENGTH) throw new Error('Base58 address too long');
let num = 0n;
for (const char of value) {
const index = BASE58_INDEXES.get(char);
if (index === undefined) throw new Error('Invalid base58 character');
num = (num * 58n) + BigInt(index);
}
let hex = num.toString(16);
if (hex.length % 2 !== 0) hex = `0${ hex}`;
let decoded = hex === '00' ? Buffer.alloc(0) : Buffer.from(hex, 'hex');
let leadingZeros = 0;
while (leadingZeros < value.length && value[leadingZeros] === '1') leadingZeros++;
if (leadingZeros > 0) decoded = Buffer.concat([Buffer.alloc(leadingZeros), decoded]);
if (decoded.length < 5) throw new Error('Invalid base58check payload');
const payload = decoded.subarray(0, -4);
const checksum = decoded.subarray(-4);
const expectedChecksum = hash256(payload).subarray(0, 4);
if (!crypto.timingSafeEqual(checksum, expectedChecksum)) throw new Error('Invalid base58check checksum');
return decoded;
}
function p2pkhScript(pubkeyHash) {
if (!Buffer.isBuffer(pubkeyHash) || pubkeyHash.length !== 20) throw new Error('Invalid pubkey hash');
return Buffer.concat([Buffer.from([0x76, 0xa9, 0x14]), pubkeyHash, Buffer.from([0x88, 0xac])]);
}
function base58AddressToHash160(addr) {
const decoded = decodeBase58Check(addr);
if (decoded.length !== 25) throw new Error('Invalid base58 address');
return decoded.slice(1, -4);
}
function addressToScript(addr) {
if (typeof addr !== 'string') throw new Error('Invalid address');
if (addr.length > MAX_BECH32_ADDRESS_LENGTH) throw new Error('Invalid address length');
try {
return p2pkhScript(base58AddressToHash160(addr));
} catch (_err) {
// not base58check; fall through to try bech32 decoding below
}
let decoded;
try {
decoded = Buffer.from(bech32.bech32.fromWords(bech32.bech32.decode(addr).words.slice(1)));
} catch (_err) {
throw new Error('Invalid address');
}
if (decoded.length !== 20) throw new Error('Invalid address');
return Buffer.concat([Buffer.from([0x00, 0x14]), decoded]);
}
function serializeTransaction(tx) {
const inputs = tx.inputs || [];
const outputs = tx.outputs || [];
const parts = [
packInt32LE(tx.version === undefined ? 1 : tx.version),
varIntBuffer(inputs.length)
];
for (const input of inputs) {
if (!Buffer.isBuffer(input.hash) || input.hash.length !== 32) throw new Error('Invalid transaction input hash');
const script = input.script || Buffer.alloc(0);
parts.push(
input.hash,
packUInt32LE(input.index),
varIntBuffer(script.length),
script,
packUInt32LE(input.sequence === undefined ? 0xffffffff : input.sequence)
);
}
parts.push(varIntBuffer(outputs.length));
for (const output of outputs) {
const script = output.script || Buffer.alloc(0);
parts.push(
packUInt64LE(output.value),
varIntBuffer(script.length),
script
);
}
parts.push(packUInt32LE(tx.locktime === undefined ? 0 : tx.locktime));
return Buffer.concat(parts);
}
function readRawBitcoinTransaction(buffer, offsetArg) {
let offset = offsetArg;
const start = offset;
if (offset + 4 > buffer.length) throw new Error('Unexpected end of transaction version');
const version = buffer.readInt32LE(offset);
offset += 4;
let hasWitnessMarker = false;
if (offset + 2 <= buffer.length &&
buffer[offset] === ADVANCED_TRANSACTION_MARKER &&
buffer[offset + 1] === ADVANCED_TRANSACTION_FLAG) {
hasWitnessMarker = true;
offset += 2;
}
const inputCount = readRawVarInt(buffer, offset, 'input count');
offset = inputCount.offset;
const ins = [];
for (let i = 0; i < inputCount.value; i++) {
const hash = readRawSlice(buffer, offset, 32, 'input hash');
const index = readRawSlice(buffer, hash.offset, 4, 'input index');
const script = readRawVarSlice(buffer, index.offset, 'input script');
const sequence = readRawSlice(buffer, script.offset, 4, 'input sequence');
ins.push({
hash: hash.value,
index: index.value.readUInt32LE(0),
script: script.value,
sequence: sequence.value.readUInt32LE(0),
witness: []
});
offset = sequence.offset;
}
const outputCount = readRawVarInt(buffer, offset, 'output count');
offset = outputCount.offset;
const outs = [];
for (let i = 0; i < outputCount.value; i++) {
const value = readRawSlice(buffer, offset, 8, 'output value');
const script = readRawVarSlice(buffer, value.offset, 'output script');
outs.push({
valueBuffer: value.value,
script: script.value
});
offset = script.offset;
}
const witnessStart = offset;
if (hasWitnessMarker) {
for (let i = 0; i < inputCount.value; i++) {
const witness = readRawWitnessVector(buffer, offset);
ins[i].witness = witness.value;
offset = witness.offset;
}
if (!ins.some((input) => input.witness.length > 0)) {
throw new Error('Transaction has superfluous witness data');
}
}
const locktimeStart = offset;
const locktime = readRawSlice(buffer, offset, 4, 'locktime');
offset = locktime.offset;
const rawWithWitness = buffer.slice(start, offset);
const rawNoWitness = hasWitnessMarker ?
Buffer.concat([
buffer.slice(start, start + 4),
buffer.slice(start + 6, witnessStart),
buffer.slice(locktimeStart, offset)
]) :
rawWithWitness;
return {
transaction: createRawTransactionView(version, ins, outs, locktime.value.readUInt32LE(0), rawWithWitness, rawNoWitness),
offset
};
}
function createRawTransactionView(version, ins, outs, locktime, rawWithWitness, rawNoWitness) {
return {
version,
ins,
outs,
locktime,
isCoinbase() {
return this.ins.length === 1 && isZeroHash(this.ins[0].hash);
},
hasWitnesses() {
return this.ins.some((input) => input.witness && input.witness.length > 0);
},
byteLength(allowWitness) {
return this.__toBuffer(undefined, undefined, allowWitness).length;
},
__toBuffer(_buffer, _initialOffset, allowWitness) {
return allowWitness && this.hasWitnesses() ? rawWithWitness : rawNoWitness;
}
};
}
function readRawVarInt(buffer, offset, context) {
if (offset >= buffer.length) throw new Error(`Unexpected end of transaction ${context}`);
const value = varuint.decode(buffer, offset);
if (!Number.isSafeInteger(value)) throw new Error(`Invalid transaction ${context}`);
return {
value,
offset: offset + varuint.decode.bytes
};
}
function readRawSlice(buffer, offset, size, context) {
if (size < 0 || offset + size > buffer.length) throw new Error(`Unexpected end of transaction ${context}`);
return {
value: buffer.slice(offset, offset + size),
offset: offset + size
};
}
function readRawVarSlice(buffer, offset, context) {
const parsed = readRawVarInt(buffer, offset, `${context} length`);
return readRawSlice(buffer, parsed.offset, parsed.value, context);
}
function readRawWitnessVector(buffer, offset) {
const count = readRawVarInt(buffer, offset, 'witness item count');
let nextOffset = count.offset;
const vector = [];
for (let i = 0; i < count.value; i++) {
const item = readRawVarSlice(buffer, nextOffset, 'witness item');
vector.push(item.value);
nextOffset = item.offset;
}
return {
value: vector,
offset: nextOffset
};
}
function isZeroHash(buffer) {
for (let i = 0; i < buffer.length; i++) {
if (buffer[i] !== 0) return false;
}
return true;
}
module.exports = {
ADVANCED_TRANSACTION_FLAG,
ADVANCED_TRANSACTION_MARKER,
addressToScript,
base58AddressToHash160,
p2pkhScript,
readRawBitcoinTransaction,
serializeTransaction,
varIntBuffer
};