-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.js
301 lines (270 loc) Β· 7.73 KB
/
util.js
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
291
292
293
294
295
296
297
298
299
300
301
// Import Required Modules
const crypto = require('crypto');
const base58 = require('base58-native');
const bignum = require('bignum');
const bitcoin = require('bitcoinjs-lib');
// Hash Address from exAddress + Key
exports.addressFromEx = function (exAddress, ripdm160Key) {
try {
const versionByte = exports.getVersionByte(exAddress);
const addrBase = Buffer.concat([versionByte, Buffer.from(ripdm160Key, 'hex')]);
const checksum = exports.sha256d(addrBase).slice(0, 4);
const address = Buffer.concat([addrBase, checksum]);
return base58.encode(address);
} catch (e) {
return null;
}
};
// Convert Address to Script
exports.addressToScript = function (network, addr) {
if (typeof network !== 'undefined' && network !== null) {
return bitcoin.address.toOutputScript(addr, network);
}
return Buffer.concat(
[
Buffer.from([0x76, 0xa9, 0x14]),
bitcoin.address.fromBase58Check(addr).hash,
Buffer.from([0x88, 0xac])
]
);
};
// Convert Bits into Target Bignum
exports.bignumFromBitsBuffer = function (bitsBuff) {
const numBytes = bitsBuff.readUInt8(0);
const bigBits = bignum.fromBuffer(bitsBuff.slice(1));
const target = bigBits.mul(
bignum(2).pow(
bignum(8).mul(
numBytes - 3,
),
),
);
return target;
};
// Convert Bits into Target Bignum
exports.bignumFromBitsHex = function (bitsString) {
const bitsBuff = Buffer.from(bitsString, 'hex');
return exports.bignumFromBitsBuffer(bitsBuff);
};
// Convert Buffer to Compact Bits
exports.bufferToCompactBits = function (startingBuff) {
const bigNum = bignum.fromBuffer(startingBuff);
let buff = bigNum.toBuffer();
buff = buff.readUInt8(0) > 0x7f ? Buffer.concat([Buffer.from([0x00]), buff]) : buff;
buff = Buffer.concat([Buffer.from([buff.length]), buff]);
const compact = buff.slice(0, 4);
return compact;
};
// Convert Bits to Buffer
exports.convertBitsToBuff = function (bitsBuff) {
const target = exports.bignumFromBitsBuffer(bitsBuff);
const resultBuff = target.toBuffer();
const buff256 = Buffer.alloc(32);
buff256.fill(0);
resultBuff.copy(buff256, buff256.length - resultBuff.length);
return buff256;
};
// Get Truncated Difficulty
exports.getTruncatedDiff = function (shift) {
return exports.convertBitsToBuff(exports.bufferToCompactBits(exports.shiftMax256Right(shift)));
};
// Get Address Version Byte
exports.getVersionByte = function (addr) {
const versionByte = base58.decode(addr).slice(0, 1);
return versionByte;
};
// Generate Hex String from Input Buffer
exports.hexFromReversedBuffer = function (buffer) {
return exports.reverseBuffer(buffer).toString('hex');
};
// Convert Mining Key to Script
exports.miningKeyToScript = function (key) {
const keyBuffer = Buffer.from(key, 'hex');
return Buffer.concat([Buffer.from([0x76, 0xa9, 0x14]), keyBuffer, Buffer.from([0x88, 0xac])]);
};
// Alloc/Write UInt16LE
exports.packUInt16LE = function (num) {
const buff = Buffer.alloc(2);
buff.writeUInt16LE(num, 0);
return buff;
};
// Alloc/Write UInt32LE
exports.packUInt32LE = function (num) {
const buff = Buffer.alloc(4);
buff.writeUInt32LE(num, 0);
return buff;
};
// Alloc/Write UInt32BE
exports.packUInt32BE = function (num) {
const buff = Buffer.alloc(4);
buff.writeUInt32BE(num, 0);
return buff;
};
// Alloc/Write Int32LE
exports.packInt32LE = function (num) {
const buff = Buffer.alloc(4);
buff.writeInt32LE(num, 0);
return buff;
};
// Alloc/Write Int32BE
exports.packInt32BE = function (num) {
const buff = Buffer.alloc(4);
buff.writeInt32BE(num, 0);
return buff;
};
// Alloc/Write Int64LE
exports.packInt64LE = function (num) {
const buff = Buffer.alloc(8);
buff.writeUInt32LE(num % (2 ** 32), 0);
buff.writeUInt32LE(Math.floor(num / 2 ** 32), 4);
return buff;
};
// Convert Pubkey to Script
exports.pubkeyToScript = function (key) {
if (key.length !== 66) {
console.error(`Invalid pubkey: ${key}`);
throw new Error();
}
const pubkey = Buffer.alloc(35);
pubkey[0] = 0x21;
pubkey[34] = 0xac;
Buffer.from(key, 'hex').copy(pubkey, 1);
return pubkey;
};
// Range Function
exports.range = function (start, stop, step) {
if (typeof stop === 'undefined') {
stop = start;
start = 0;
}
if (typeof step === 'undefined') {
step = 1;
}
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
return [];
}
const result = [];
for (let i = start; step > 0 ? i < stop : i > stop; i += step) {
result.push(i);
}
return result;
};
// Reverse Input Buffer
exports.reverseBuffer = function (buff) {
const reversed = Buffer.alloc(buff.length);
for (let i = buff.length - 1; i >= 0; i -= 1) reversed[buff.length - i - 1] = buff[i];
return reversed;
};
// Reverse Byte Order of Input Buffer
exports.reverseByteOrder = function (buff) {
for (let i = 0; i < 8; i += 1) buff.writeUInt32LE(buff.readUInt32BE(i * 4), i * 4);
return exports.reverseBuffer(buff);
};
// Reverse Input Buffer + Hex String
exports.reverseHex = function (hex) {
return exports.reverseBuffer(Buffer.from(hex, 'hex')).toString('hex');
};
// Serialize Height/Date Input
exports.serializeNumber = function (n) {
if (n >= 1 && n <= 16) return Buffer.from([0x50 + n]);
let l = 1;
const buff = Buffer.alloc(9);
while (n > 0x7f) {
buff.writeUInt8(n & 0xff, l += 1);
n >>= 8;
}
buff.writeUInt8(l, 0);
buff.writeUInt8(n, l += 1);
return buff.slice(0, l);
};
// Serialize Strings used for Signature
exports.serializeString = function (s) {
if (s.length < 253) {
return Buffer.concat([
Buffer.from([s.length]),
Buffer.from(s),
]);
}
if (s.length < 0x10000) {
return Buffer.concat([
Buffer.from([253]),
exports.packUInt16LE(s.length),
Buffer.from(s),
]);
}
if (s.length < 0x100000000) {
return Buffer.concat([
Buffer.from([254]),
exports.packUInt32LE(s.length),
Buffer.from(s),
]);
}
return Buffer.concat([
Buffer.from([255]),
exports.packUInt16LE(s.length),
Buffer.from(s),
]);
};
// Hash Input w/ Sha256
exports.sha256 = function (buffer) {
const hash1 = crypto.createHash('sha256');
hash1.update(buffer);
return hash1.digest();
};
// Hash Input w/ Sha256d
exports.sha256d = function (buffer) {
return exports.sha256(exports.sha256(buffer));
};
// Bitwise Right-Shift Max UInt256
exports.shiftMax256Right = function (shiftRight) {
let arr256 = Array(...new Array(256)).map(Number.prototype.valueOf, 1);
const arrLeft = Array(...new Array(shiftRight)).map(Number.prototype.valueOf, 0);
arr256 = arrLeft.concat(arr256).slice(0, 256);
const octets = [];
for (let i = 0; i < 32; i += 1) {
octets[i] = 0;
const bits = arr256.slice(i * 8, i * 8 + 8);
for (let f = 0; f < bits.length; f += 1) {
const multiplier = 2 ** f;
octets[i] += bits[f] * multiplier;
}
}
return Buffer.from(octets);
};
// Generate Reverse Buffer from Input Hash
exports.uint256BufferFromHash = function (hex) {
let fromHex = Buffer.from(hex, 'hex');
if (fromHex.length !== 32) {
const empty = Buffer.alloc(32);
empty.fill(0);
fromHex.copy(empty);
fromHex = empty;
}
return exports.reverseBuffer(fromHex);
};
// Generate VarInt Buffer
exports.varIntBuffer = function (n) {
let buff;
if (n < 0xfd) return Buffer.from([n]);
if (n <= 0xffff) {
buff = Buffer.alloc(3);
buff[0] = 0xfd;
buff.writeUInt16LE(n, 1);
return buff;
}
if (n <= 0xffffffff) {
buff = Buffer.alloc(5);
buff[0] = 0xfe;
buff.writeUInt32LE(n, 1);
return buff;
}
buff = Buffer.alloc(9);
buff[0] = 0xff;
exports.packUInt16LE(n).copy(buff, 1);
return buff;
};
// Generate VarString Buffer
exports.varStringBuffer = function (string) {
const strBuff = Buffer.from(string);
return Buffer.concat([exports.varIntBuffer(strBuff.length), strBuff]);
};