-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathderivation.js
More file actions
179 lines (169 loc) · 4.86 KB
/
derivation.js
File metadata and controls
179 lines (169 loc) · 4.86 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* This module defines functions for address derivation.
*
* @module derivation
*/
import * as bitcoin from "bitcoinjs-lib"
import * as ecc from 'tiny-secp256k1';
import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371';
import { deriveChildPublicKey, networkData, Network } from "@caravan/bitcoin"
import { fullDerivationPath, partialKeyDerivationPath } from "./paths"
import {
isValidExtPubKey,
isValidIndex,
isValidPurpose,
isValidChainIndex,
} from "./validation"
import { convertToXPUB } from "./conversion"
import { Purpose } from "./purpose"
/**
* Default network to use for address derivation.
*
* @constant
* @type {string}
* @default Network.TESTNET
* */
const DEFAULT_NETWORK = Network.TESTNET
/**
* Default purpose to use for address derivation.
*
* @constant
* @type {string}
* @default Purpose.P2WPKH
* */
const DEFAULT_PURPOSE = Purpose.P2WPKH
bitcoin.initEccLib(ecc);
/**
* Derive a single address from a public key.
*
* @param {module:purpose~Purpose} purpose - the purpose dictates the derived
* address type (P2PKH = 1address, P2SH = 3address, P2WPKH = bc1address, P2TR = bc1paddress)
* @param {Buffer} pubkey - the Buffer representation public key to derive from
* @param {NETWORK} network - the network to use (MAINNET or TESTNET)
*
* @returns {object|undefined} derived address
*/
function deriveAddress({ purpose, pubkey, network }) {
switch (purpose) {
case Purpose.P2PKH: {
const { address: oneAddress } = bitcoin.payments.p2pkh({
pubkey,
network: networkData(network),
})
return oneAddress
}
case Purpose.P2SH: {
const { address: threeAddress } = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({
pubkey,
network: networkData(network),
}),
})
return threeAddress
}
case Purpose.P2WPKH: {
const { address: bc1qAddress } = bitcoin.payments.p2wpkh({
pubkey,
network: networkData(network),
})
return bc1qAddress
}
case Purpose.P2TR: {
// Context: https://bitcoinops.org/en/topics/x-only-public-keys/
const xOnlyPubkey = toXOnly(pubkey)
const { address: bc1pAddress } = bitcoin.payments.p2tr({
internalPubkey: xOnlyPubkey,
network: networkData(network),
})
return bc1pAddress
}
default:
return undefined
}
}
/**
* Derive a single address from a given extended public key. Address type is
* defined by the `purpose` parameter.
*
* @param {string} extPubKey - the extended public key
* @param {number} [change=0] - change (0 = external chain, 1 = internal chain / change)
* @param {number} [keyIndex=0] - the unhardened key index
* @param {module:purpose~Purpose} [purpose=DEFAULT_PURPOSE] - the derivation purpose
* @param {NETWORK} [network=DEFAULT_NETWORK] - the target network (TESTNET or MAINNET)
*
* @returns {object|undefined} derived address
*/
function addressFromExtPubKey({
extPubKey,
change = 0,
keyIndex = 0,
purpose = DEFAULT_PURPOSE,
network = DEFAULT_NETWORK,
}) {
if (
!isValidChainIndex(change) ||
!isValidIndex(keyIndex) ||
!isValidPurpose(purpose) ||
!isValidExtPubKey(extPubKey, network)
) {
return undefined
}
const partialPath = partialKeyDerivationPath({ change, keyIndex })
const convertedExtPubKey = convertToXPUB(extPubKey, network)
const fullPath = fullDerivationPath({
convertedExtPubKey,
purpose,
change,
keyIndex,
network,
})
const childPubKey = deriveChildPublicKey(
convertedExtPubKey,
partialPath,
network
)
const pubkey = Buffer.from(childPubKey, "hex")
return {
path: fullPath,
address: deriveAddress({ purpose, pubkey, network }),
}
}
/**
* Derive multiple addresses from a given extended public key.
* See {@link module:derivation~addressFromExtPubKey}.
*
* @param {string} extPubKey - the extended public key
* @param {string} addressCount - number of key indices to derive
* @param {number} [addressStartIndex=0] - start key index to derive from
* @param {number} [change=0] - change (0 = external chain, 1 = internal chain / change)
* @param {module:purpose~Purpose} [purpose=DEFAULT_PURPOSE] - the derivation purpose
* @param {NETWORK} [network=DEFAULT_NETWORK] - the target network (TESTNET or MAINNET)
*
* @returns {object[]} array of derived addresses
*/
function addressesFromExtPubKey({
extPubKey,
addressCount,
addressStartIndex = 0,
change = 0,
purpose = DEFAULT_PURPOSE,
network = DEFAULT_NETWORK,
}) {
const addresses = []
for (
let keyIndex = addressStartIndex;
keyIndex < addressStartIndex + addressCount;
keyIndex += 1
) {
const { path, address } = addressFromExtPubKey({
extPubKey,
change,
keyIndex,
purpose,
network,
})
addresses.push({ path, address })
}
return addresses
}
export { addressFromExtPubKey, addressesFromExtPubKey }