-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathpush_data.js
61 lines (61 loc) · 1.69 KB
/
push_data.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
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.decode = exports.encode = exports.encodingLength = void 0;
const ops_1 = require('./ops');
function encodingLength(i) {
return i < ops_1.OPS.OP_PUSHDATA1 ? 1 : i <= 0xff ? 2 : i <= 0xffff ? 3 : 5;
}
exports.encodingLength = encodingLength;
function encode(buffer, num, offset) {
const size = encodingLength(num);
// ~6 bit
if (size === 1) {
buffer.writeUInt8(num, offset);
// 8 bit
} else if (size === 2) {
buffer.writeUInt8(ops_1.OPS.OP_PUSHDATA1, offset);
buffer.writeUInt8(num, offset + 1);
// 16 bit
} else if (size === 3) {
buffer.writeUInt8(ops_1.OPS.OP_PUSHDATA2, offset);
buffer.writeUInt16LE(num, offset + 1);
// 32 bit
} else {
buffer.writeUInt8(ops_1.OPS.OP_PUSHDATA4, offset);
buffer.writeUInt32LE(num, offset + 1);
}
return size;
}
exports.encode = encode;
function decode(buffer, offset) {
const opcode = buffer.readUInt8(offset);
let num;
let size;
// ~6 bit
if (opcode < ops_1.OPS.OP_PUSHDATA1) {
num = opcode;
size = 1;
// 8 bit
} else if (opcode === ops_1.OPS.OP_PUSHDATA1) {
if (offset + 2 > buffer.length) return null;
num = buffer.readUInt8(offset + 1);
size = 2;
// 16 bit
} else if (opcode === ops_1.OPS.OP_PUSHDATA2) {
if (offset + 3 > buffer.length) return null;
num = buffer.readUInt16LE(offset + 1);
size = 3;
// 32 bit
} else {
if (offset + 5 > buffer.length) return null;
if (opcode !== ops_1.OPS.OP_PUSHDATA4) throw new Error('Unexpected opcode');
num = buffer.readUInt32LE(offset + 1);
size = 5;
}
return {
opcode,
number: num,
size,
};
}
exports.decode = decode;