1
|
|
|
'use strict' |
2
|
1 |
|
let base32 = require('./base32') |
3
|
|
|
|
4
|
1 |
|
let P2SH = "scripthash"; |
5
|
1 |
|
let P2PKH = "pubkeyhash"; |
6
|
|
|
|
7
|
1 |
|
let hashBitMap = {}; |
8
|
1 |
|
hashBitMap[160] = 0; |
9
|
1 |
|
hashBitMap[192] = 1; |
10
|
1 |
|
hashBitMap[224] = 2; |
11
|
1 |
|
hashBitMap[256] = 3; |
12
|
1 |
|
hashBitMap[320] = 4; |
13
|
1 |
|
hashBitMap[384] = 5; |
14
|
1 |
|
hashBitMap[448] = 6; |
15
|
1 |
|
hashBitMap[512] = 7; |
16
|
|
|
|
17
|
1 |
|
let versionBitMap = {}; |
18
|
1 |
|
versionBitMap[P2PKH] = 0; |
19
|
1 |
|
versionBitMap[P2SH] = 1; |
20
|
|
|
|
21
|
|
|
function checkMap(mapObj, value) { |
22
|
21 |
|
let keys = Object.keys(mapObj); |
23
|
21 |
|
for (let i = 0; i < keys.length; i++) { |
24
|
30 |
|
if (mapObj[keys[i]] === value) { |
25
|
20 |
|
return keys[i]; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
return null; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
function createVersion(scriptType, hashLengthBits) { |
33
|
9 |
|
if ((scriptType === P2PKH || scriptType === P2SH) && hashLengthBits !== 160) { |
34
|
1 |
|
throw new Error("Invalid hash length for scriptType"); |
35
|
|
|
} |
36
|
|
|
|
37
|
8 |
|
return (versionBitMap[scriptType] << 3) | hashBitMap[hashLengthBits]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function encodePayload(scriptType, hash) { |
41
|
9 |
|
let hashLength = hash.length |
42
|
9 |
|
let version = createVersion(scriptType, hashLength * 8) |
43
|
8 |
|
let payload = Buffer.allocUnsafe(1 + hash.length) |
|
|
|
|
44
|
8 |
|
payload.writeUInt8(version, 0) |
45
|
8 |
|
hash.copy(payload, 1) |
46
|
8 |
|
return payload |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
function decodeVersion (version) { |
50
|
12 |
|
let last = (version >>> 7); |
51
|
12 |
|
if (last & 1 || last > 0) { |
|
|
|
|
52
|
1 |
|
throw new Error("Invalid version, most significant bit is reserved"); |
53
|
|
|
} |
54
|
|
|
|
55
|
11 |
|
let scriptType = checkMap(versionBitMap, (version >> 3) & 0x0f) |
56
|
11 |
|
if (scriptType === null) { |
57
|
1 |
|
throw new Error("Invalid script type"); |
58
|
|
|
} |
59
|
|
|
// all possible values return |
60
|
10 |
|
let hashSize = parseInt(checkMap(hashBitMap, version & 0x07), 10); |
61
|
10 |
|
if ((scriptType === P2PKH || scriptType === P2SH) && hashSize !== 160) { |
62
|
1 |
|
throw new Error("Mismatch between script type and hash length"); |
63
|
|
|
} |
64
|
|
|
|
65
|
9 |
|
return {scriptType, hashSize} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
function encode (prefix, scriptType, hash) { |
69
|
11 |
|
if (!(hash instanceof Buffer)) { |
|
|
|
|
70
|
1 |
|
throw new Error("Hash should be passed as a Buffer") |
71
|
|
|
} |
72
|
|
|
|
73
|
10 |
|
if (!(scriptType in versionBitMap)) { |
74
|
1 |
|
throw new Error("Unsupported script type") |
75
|
|
|
} |
76
|
|
|
|
77
|
9 |
|
return base32.encode( |
78
|
|
|
prefix, |
79
|
|
|
base32.toWords(encodePayload(scriptType, hash)) |
80
|
|
|
) |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
function decode(address) { |
84
|
13 |
|
let result = base32.decode(address) |
85
|
13 |
|
let data = base32.fromWords(result.words) |
86
|
13 |
|
if (data.length < 1) { |
87
|
1 |
|
throw new Error("Empty payload in address") |
88
|
|
|
} |
89
|
|
|
|
90
|
12 |
|
let versionInfo = decodeVersion(data[0]) |
91
|
9 |
|
if (1 + (versionInfo.hashSize / 8) !== data.length) { |
92
|
1 |
|
throw new Error('Hash length does not match version') |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
return { |
96
|
|
|
version: versionInfo.scriptType, |
97
|
|
|
prefix: result.prefix, |
98
|
|
|
hash: Buffer.from(data.slice(1)) |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
module.exports = { decode: decode, encode: encode }; |
103
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.