forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultisig-cosigner.js
152 lines (132 loc) · 3.61 KB
/
multisig-cosigner.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
import b58 from 'bs58check';
const HDNode = require('bip32');
export class MultisigCosigner {
constructor(data) {
this._data = data;
this._fp = false;
this._xpub = false;
this._path = false;
this._valid = false;
this._cosigners = [];
// is it plain simple Zpub/Ypub/xpub?
if (data.startsWith('Zpub') && MultisigCosigner.isXpubValid(data)) {
this._fp = '00000000';
this._xpub = data;
this._path = "m/48'/0'/0'/2'";
this._valid = true;
this._cosigners = [true];
return;
} else if (data.startsWith('Ypub') && MultisigCosigner.isXpubValid(data)) {
this._fp = '00000000';
this._xpub = data;
this._path = "m/48'/0'/0'/1'";
this._valid = true;
this._cosigners = [true];
return;
} else if (data.startsWith('xpub') && MultisigCosigner.isXpubValid(data)) {
this._fp = '00000000';
this._xpub = data;
this._path = "m/45'";
this._valid = true;
this._cosigners = [true];
return;
}
// is it wallet descriptor?
if (data.startsWith('[')) {
const end = data.indexOf(']');
const part = data.substr(1, end - 1).replace(/[h]/g, "'");
this._fp = part.split('/')[0];
const xpub = data.substr(end + 1);
if (MultisigCosigner.isXpubValid(xpub)) {
this._xpub = xpub;
this._path = 'm';
for (let c = 0; c < part.split('/').length; c++) {
if (c === 0) continue;
this._path += '/' + part.split('/')[c];
}
this._cosigners = [true];
this._valid = true;
return;
}
}
// is it cobo json?
try {
const json = JSON.parse(data);
if (json.xfp && json.xpub && json.path) {
this._fp = json.xfp;
this._xpub = json.xpub;
this._path = json.path;
this._cosigners = [true];
this._valid = true;
return;
}
} catch (_) {
this._valid = false;
}
// is it coldcard json?
try {
const json = JSON.parse(data);
if (json.p2sh && json.p2sh_deriv && json.xfp) {
const cc = new MultisigCosigner(MultisigCosigner.exportToJson(json.xfp, json.p2sh, json.p2sh_deriv));
this._valid = true;
this._cosigners.push(cc);
}
if (json.p2wsh_p2sh && json.p2wsh_p2sh_deriv && json.xfp) {
const cc = new MultisigCosigner(MultisigCosigner.exportToJson(json.xfp, json.p2wsh_p2sh, json.p2wsh_p2sh_deriv));
this._valid = true;
this._cosigners.push(cc);
}
if (json.p2wsh && json.p2wsh_deriv && json.xfp) {
const cc = new MultisigCosigner(MultisigCosigner.exportToJson(json.xfp, json.p2wsh, json.p2wsh_deriv));
this._valid = true;
this._cosigners.push(cc);
}
} catch (_) {
this._valid = false;
}
}
static _zpubToXpub(zpub) {
let data = b58.decode(zpub);
data = data.slice(4);
data = Buffer.concat([Buffer.from('0488b21e', 'hex'), data]);
return b58.encode(data);
}
static isXpubValid(key) {
let xpub;
try {
xpub = MultisigCosigner._zpubToXpub(key);
HDNode.fromBase58(xpub);
return true;
} catch (_) {}
return false;
}
static exportToJson(xfp, xpub, path) {
return JSON.stringify({
xfp: xfp,
xpub: xpub,
path: path,
});
}
isValid() {
return this._valid;
}
getFp() {
return this._fp;
}
getXpub() {
return this._xpub;
}
getPath() {
return this._path;
}
howManyCosignersWeHave() {
return this._cosigners.length;
}
/**
*
* @returns {Array.<MultisigCosigner>}
*/
getAllCosigners() {
return this._cosigners;
}
}