-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutil.js
More file actions
60 lines (60 loc) · 1.48 KB
/
util.js
File metadata and controls
60 lines (60 loc) · 1.48 KB
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
const bsv = require('bsv')
const crypto = require('crypto')
const bip44 = "m/44'/0'/0'/0"
const script = (out) => {
if (out.script) {
return bsv.Script.fromHex(out.script);
} else {
let s = new bsv.Script();
let keys = Object.keys(out).filter((k) => {
return /[obsh][0-9]+/.test(k)
})
let max = -1;
let chunks = [];
keys.forEach((key) => {
let index = parseInt(key.slice(1))
if (index > max) max = index;
chunks[index] = {[key]: out[key]}
})
for(let i=0; i<=max; i++) {
if (chunks[i]) {
let key = Object.keys(chunks[i])[0];
let val = Object.values(chunks[i])[0];
if (key.startsWith("o")) {
s.add(bsv.Opcode[val])
} else if (key.startsWith("s")) {
s.add(Buffer.from(val))
} else if (key.startsWith("h")) {
s.add(Buffer.from(val, "hex"))
} else if (key.startsWith("b")) {
s.add(Buffer.from(val, "base64"))
}
} else {
s.add(bsv.Opcode.OP_FALSE)
}
}
return s;
}
}
const HD = {
id: (o) => {
if (o.xpriv) {
const path = (o && o.path ? o.path : bip44)
return crypto.createHash('sha256').update(o.xpriv + "-" + path).digest('hex');
}
},
path: (o) => {
return (o && o.path ? o.path : bip44)
},
isvalid: (str) => {
try {
let seed = bsv.HDPrivateKey.fromString(o.xpriv)
return true;
} catch (e) {
return false;
}
}
}
module.exports = {
script: script, HD: HD
}