/
xpubConvert.js
45 lines (40 loc) · 1.36 KB
/
xpubConvert.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
/*
This script uses version bytes as described in SLIP-132
https://github.com/satoshilabs/slips/blob/master/slip-0132.md
*/
var b58 = require('bs58check');
const prefixes = new Map(
[
['xpub', '0488b21e'],
['ypub', '049d7cb2'],
['Ypub', '0295b43f'],
['zpub', '04b24746'],
['Zpub', '02aa7ed3'],
['tpub', '043587cf'],
['upub', '044a5262'],
['Upub', '024289ef'],
['vpub', '045f1cf6'],
['Vpub', '02575483'],
]
);
/*
* This function takes an extended public key (with any version bytes, it doesn't need to be an xpub)
* and converts it to an extended public key formatted with the desired version bytes
* @param xpub: an extended public key in base58 format. Example: xpub6CpihtY9HVc1jNJWCiXnRbpXm5BgVNKqZMsM4XqpDcQigJr6AHNwaForLZ3kkisDcRoaXSUms6DJNhxFtQGeZfWAQWCZQe1esNetx5Wqe4M
* @param targetFormat: a string representing the desired prefix; must exist in the "prefixes" mapping defined above. Example: Zpub
*/
global.changeVersionBytes = function(xpub, targetFormat) {
if (!prefixes.has(targetFormat)) {
return "Invalid target version";
}
// trim whitespace
xpub = xpub.trim();
try {
var data = b58.decode(xpub);
data = data.slice(4);
data = Buffer.concat([Buffer.from(prefixes.get(targetFormat),'hex'), data]);
return b58.encode(data);
} catch (err) {
return "Invalid extended public key";
}
}