-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecoder.js
106 lines (94 loc) · 3.73 KB
/
Decoder.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
const { URDecoder } = require('@ngraveio/bc-ur')
const {
CryptoAccount,
CryptoPSBT,
ScriptExpressions
} = require('@keystonehq/bc-ur-registry')
const b58 = require('bs58check')
const Util = require('./Util')
const PATH_NATIVE_SEGWIT = "m/48'/0'/0'/2'"
const PATH_WRAPPED_SEGWIT = "m/48'/0'/0'/1'"
const PATH_LEGACY = "m/45'"
class Decoder {
/**
* Decodes XPUB Uniform Resource into a structured object
* @param {string} payload UR from scanned xpub qr code
* @returns {object} with the following structure:
* {
* xpubKey: 'Zpub756tPrxwHiYkYiT12G2WUD2cpAHyVWhjvKPbXoY5jDZSyo71yG5C14LCuwhycTTAzgTUcQfddR8FFTQ1bSWR6kzmNbMEaVzUrj4Lhxbonjo',
* masterFingerprint: '21EBDA7D',
* derivationPath: "m/48'/0'/0'/2'",
* fullXpub: [21EBDA7D/48'/0'/0'/2']Zpub756tPrxwHiYkYiT12G2WUD2cpAHyVWhjvKPbXoY5jDZSyo71yG5C14LCuwhycTTAzgTUcQfddR8FFTQ1bSWR6kzmNbMEaVzUrj4Lhxbonjo
* }
*/
decodeXPub (payload) {
const decoded = this._decode(payload, 'crypto-account')
const cryptoAccount = CryptoAccount.fromCBOR(decoded.cbor)
const hdKey = cryptoAccount.outputDescriptors[0].getCryptoKey()
const derivationPath = 'm/' + hdKey.getOrigin().getPath()
const script = cryptoAccount.outputDescriptors[0].getScriptExpressions()[0].getExpression()
const isMultisig =
script === ScriptExpressions.WITNESS_SCRIPT_HASH.getExpression() ||
derivationPath === PATH_LEGACY ||
derivationPath === PATH_WRAPPED_SEGWIT ||
derivationPath === PATH_NATIVE_SEGWIT
const version = Util.fromHex(isMultisig ? '02aa7ed3' : '04b24746')
const parentFingerprint = hdKey.getParentFingerprint()
const depth = hdKey.getOrigin().getDepth()
const depthBuf = Buffer.alloc(1)
depthBuf.writeUInt8(depth)
const components = hdKey.getOrigin().getComponents()
const lastComponents = components[components.length - 1]
const index = lastComponents.isHardened() ? lastComponents.getIndex() + 0x80000000 : lastComponents.getIndex()
const indexBuf = Buffer.alloc(4)
indexBuf.writeUInt32BE(index)
const chainCode = hdKey.getChainCode()
const key = hdKey.getKey()
const data = Buffer.concat([version, depthBuf, parentFingerprint, indexBuf, chainCode, key])
const xpubKey = b58.encode(data)
const masterFingerprint = Util.toHex(cryptoAccount.getMasterFingerprint()).toUpperCase()
let fullXpub = xpubKey
if (
masterFingerprint != null && masterFingerprint !== '' &&
derivationPath != null && derivationPath !== ''
) {
fullXpub = `[${masterFingerprint}${derivationPath.substring(1)}]${xpubKey}`
}
const result = {
xpubKey,
masterFingerprint,
derivationPath,
fullXpub
}
return result
}
/**
* Decodes PSBT Uniform Resources into a hex encoded psbt
* @param {Array<string>} fragments An array of PSBT UR from scanned PSBT QR Codes
* @returns {string} the psbt as a base64 encoded string
*/
decodePSBT (fragments) {
const decoded = this._decode(fragments, 'crypto-psbt')
const cryptoPsbt = CryptoPSBT.fromCBOR(decoded.cbor)
return Util.toBase64(cryptoPsbt.getPSBT())
}
_decode (fragments, expectedType) {
fragments = Array.isArray(fragments) ? fragments : [fragments]
const decoder = new URDecoder()
for (const fragment of fragments) {
decoder.receivePart(fragment)
}
if (!decoder.isComplete()) {
throw new Error('provided fragments do not form a complete UR')
}
if (!decoder.isSuccess()) {
throw new Error(decoder.resultError())
}
const decoded = decoder.resultUR()
if (decoded.type !== expectedType) {
throw new Error(`Data is not of expected type ${expectedType}, found type: ${decoded.type}`)
}
return decoded
}
}
module.exports = Decoder