@teleportdao/bitcoin 1.4.4 → 1.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/dist/bitcoin-base.d.ts +62 -50
  2. package/dist/bitcoin-base.d.ts.map +1 -1
  3. package/dist/bitcoin-base.js +55 -28
  4. package/dist/bitcoin-base.js.map +1 -1
  5. package/dist/bitcoin-interface-utils.d.ts +12 -10
  6. package/dist/bitcoin-interface-utils.d.ts.map +1 -1
  7. package/dist/bitcoin-interface-utils.js +16 -10
  8. package/dist/bitcoin-interface-utils.js.map +1 -1
  9. package/dist/bitcoin-interface.d.ts +206 -50
  10. package/dist/bitcoin-interface.d.ts.map +1 -1
  11. package/dist/bitcoin-interface.js +42 -27
  12. package/dist/bitcoin-interface.js.map +1 -1
  13. package/dist/bitcoin-utils.d.ts +111 -41
  14. package/dist/bitcoin-utils.d.ts.map +1 -1
  15. package/dist/bitcoin-utils.js +215 -159
  16. package/dist/bitcoin-utils.js.map +1 -1
  17. package/dist/bundle.js +13 -4
  18. package/dist/index.d.ts +5 -6
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +31 -13
  21. package/dist/index.js.map +1 -1
  22. package/dist/teleport-dao-payments.d.ts +60 -92
  23. package/dist/teleport-dao-payments.d.ts.map +1 -1
  24. package/dist/teleport-dao-payments.js +12 -1
  25. package/dist/teleport-dao-payments.js.map +1 -1
  26. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +8 -4
  27. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  28. package/dist/transaction-builder/bitcoin-transaction-builder.js +3 -3
  29. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  30. package/dist/transaction-builder/transaction-builder.d.ts +63 -13
  31. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  32. package/dist/transaction-builder/transaction-builder.js +72 -19
  33. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  34. package/dist/utils/networks.d.ts +5 -35
  35. package/dist/utils/networks.d.ts.map +1 -1
  36. package/dist/utils/networks.js +26 -2
  37. package/dist/utils/networks.js.map +1 -1
  38. package/dist/utils/tools.d.ts +15 -9
  39. package/dist/utils/tools.d.ts.map +1 -1
  40. package/dist/utils/tools.js +14 -11
  41. package/dist/utils/tools.js.map +1 -1
  42. package/package.json +6 -6
  43. package/src/{bitcoin-base.js → bitcoin-base.ts} +97 -69
  44. package/src/{bitcoin-interface-utils.js → bitcoin-interface-utils.ts} +59 -53
  45. package/src/{bitcoin-interface.js → bitcoin-interface.ts} +115 -45
  46. package/src/{bitcoin-utils.js → bitcoin-utils.ts} +337 -216
  47. package/src/index.ts +5 -8
  48. package/src/{teleport-dao-payments.js → teleport-dao-payments.ts} +67 -6
  49. package/src/transaction-builder/bitcoin-transaction-builder.ts +8 -4
  50. package/src/transaction-builder/transaction-builder.ts +102 -25
  51. package/src/utils/{networks.js → networks.ts} +33 -31
  52. package/src/utils/{tools.js → tools.ts} +80 -72
  53. package/tsconfig.json +10 -9
  54. package/webpack.config.js +16 -0
  55. package/dist/transaction-builder/transaction-builder-common.d.ts +0 -57
  56. package/dist/transaction-builder/transaction-builder-common.d.ts.map +0 -1
  57. package/dist/transaction-builder/transaction-builder-common.js +0 -183
  58. package/dist/transaction-builder/transaction-builder-common.js.map +0 -1
@@ -1,18 +1,52 @@
1
- const bip39 = require("bip39")
2
- const bip32 = require("bip32")
1
+ import * as bip39 from "bip39"
2
+ import * as bip32 from "bip32"
3
+ import networks from "./utils/networks"
4
+
5
+ // import BIP32Factory from "bip32"
6
+ // import ECPairFactory from "ecpair"
7
+ // import * as ecc from "tiny-secp256k1"
8
+ import * as bitcoin from "bitcoinjs-lib"
9
+
10
+ // bitcoin.initEccLib(ecc)
11
+ // const bip32 = BIP32Factory(ecc)
12
+ const ECPair = bitcoin.ECPair
13
+
3
14
  let varUnit = require("varuint-bitcoin")
4
15
  const fastRoot = require("merkle-lib/fastRoot")
5
16
  const merkle = require("merkle-lib")
6
17
  const merkleProof = require("merkle-lib/proof")
7
- const bitcoin = require("bitcoinjs-lib")
8
- const networks = require("./utils/networks")
9
18
 
10
- function generateMnemonic() {
19
+ // const bip32Prefix = {
20
+ // xprv: "0x0488ade4", // Mainnet - P2PKH or P2SH - m/44'/0'
21
+ // yprv: "0x049d7878", // Mainnet - P2WPKH in P2SH - m/49'/0'
22
+ // zprv: "0x04b2430c", // Mainnet - P2WPKH - m/84'/0'
23
+ // Yprv: "0x0295b005", // Mainnet - Multi-signature P2WSH in P2SH
24
+ // Zprv: "0x02aa7a99", // Mainnet - Multi-signature P2WSH
25
+ // tprv: "0x04358394", // Testnet - P2PKH or P2SH - m/44'/1'
26
+ // uprv: "0x044a4e28", // Testnet - P2WPKH in P2SH - m/49'/1'
27
+ // vprv: "0x045f18bc", // Testnet - P2WPKH - m/84'/1'
28
+ // Uprv: "0x024285b5", // Testnet - Multi-signature P2WSH in P2SH
29
+ // Vprv: "0x02575048", // Testnet - Multi-signature P2WSH
30
+
31
+ // xpub: "0x0488b21e", // Mainnet - P2PKH or P2SH - m/44'/0'
32
+ // ypub: "0x049d7cb2", // Mainnet - P2WPKH in P2SH - m/49'/0'
33
+ // zpub: "0x04b24746", // Mainnet - P2WPKH - m/84'/0'
34
+ // Ypub: "0x0295b43f", // Mainnet - Multi-signature P2WSH in P2SH
35
+ // Zpub: "0x02aa7ed3", // Mainnet - Multi-signature P2WSH
36
+ // tpub: "0x043587cf", // Testnet - P2PKH or P2SH - m/44'/1'
37
+ // upub: "0x044a5262", // Testnet - P2WPKH in P2SH - m/49'/1'
38
+ // vpub: "0x045f1cf6", // Testnet - P2WPKH - m/84'/1'
39
+ // Upub: "0x024289ef", // Testnet - Multi-signature P2WSH in P2SH
40
+ // Vpub: "0x02575483", // Testnet - Multi-signature P2WSH
41
+ // }
42
+ export { networks }
43
+
44
+ export function generateMnemonic() {
11
45
  const mnemonic = bip39.generateMnemonic(256)
12
46
  return mnemonic
13
47
  }
14
48
 
15
- function parseRawTransaction(rawTransaction) {
49
+ export function parseRawTransaction(rawTransaction: string) {
16
50
  const size = {
17
51
  version: 4,
18
52
  flag: 2,
@@ -68,147 +102,12 @@ function parseRawTransaction(rawTransaction) {
68
102
  return { version, flag, vin, vout, witness, locktime }
69
103
  }
70
104
 
71
- function reverseBytes(hexInput) {
105
+ export function reverseBytes(hexInput: string) {
72
106
  return Buffer.from(hexInput, "hex").reverse().toString("hex")
73
107
  }
74
108
 
75
- function getAddressType(address, network = bitcoin.networks.bitcoin) {
76
- if (address.startsWith(`${network.bech32}1p`)) {
77
- // todo : check length - it could be p2wsh
78
- return "p2tr"
79
- }
80
- if (address.startsWith(network.bech32)) {
81
- // todo : check length - it could be p2wsh
82
- return "p2wpkh"
83
- }
84
- let base58Data = bitcoin.address.fromBase58Check(address)
85
- if (base58Data.version === Number(network.scriptHash)) {
86
- return "p2sh"
87
- }
88
- if (base58Data.version === Number(network.pubKeyHash)) {
89
- return "p2pkh"
90
- }
91
-
92
- throw new Error("invalid address")
93
- }
94
-
95
- function createAddressObjectByHash({ addressType, hash }, network = bitcoin.networks.bitcoin) {
96
- let addressObject
97
- switch (addressType) {
98
- case "p2pkh":
99
- addressObject = bitcoin.payments.p2pkh({
100
- hash,
101
- network,
102
- })
103
- break
104
- case "p2wpkh":
105
- addressObject = bitcoin.payments.p2wpkh({
106
- hash,
107
- network,
108
- })
109
- break
110
- case "p2sh":
111
- addressObject = bitcoin.payments.p2sh({
112
- hash,
113
- network,
114
- })
115
- break
116
- default:
117
- throw new Error("address type is incorrect")
118
- }
119
- return addressObject
120
- }
121
-
122
- function createAddressObjectByScript({ addressType, script }, network = bitcoin.networks.bitcoin) {
123
- let addressObject
124
- switch (addressType) {
125
- case "p2pkh":
126
- addressObject = bitcoin.payments.p2pkh({
127
- output: script,
128
- network,
129
- })
130
- break
131
- case "p2wpkh":
132
- addressObject = bitcoin.payments.p2wpkh({
133
- output: script,
134
- network,
135
- })
136
- break
137
- case "p2sh":
138
- addressObject = bitcoin.payments.p2sh({
139
- output: script,
140
- network,
141
- })
142
- break
143
- default:
144
- throw new Error("address type is incorrect")
145
- }
146
- return addressObject
147
- }
148
-
149
- function createAddressObjectByPublicKey(
150
- { addressType, publicKey },
151
- network = bitcoin.networks.bitcoin,
152
- ) {
153
- let addressObject
154
-
155
- switch (addressType) {
156
- case "p2pkh":
157
- addressObject = bitcoin.payments.p2pkh({
158
- pubkey: publicKey,
159
- network,
160
- })
161
- break
162
- case "p2wpkh":
163
- addressObject = bitcoin.payments.p2wpkh({
164
- pubkey: publicKey,
165
- network,
166
- })
167
- break
168
- case "p2sh-p2wpkh":
169
- addressObject = bitcoin.payments.p2sh({
170
- redeem: bitcoin.payments.p2wpkh({
171
- pubkey: publicKey,
172
- network,
173
- }),
174
- })
175
- break
176
- default:
177
- throw new Error("address type is incorrect")
178
- }
179
- return addressObject
180
- }
181
-
182
- function createAddressObjectByAddress(address, network = bitcoin.networks.bitcoin) {
183
- let addressType = getAddressType(address, network)
184
- let addressObject
185
- switch (addressType) {
186
- case "p2pkh":
187
- addressObject = bitcoin.payments.p2pkh({
188
- address,
189
- network,
190
- })
191
- break
192
- case "p2wpkh":
193
- addressObject = bitcoin.payments.p2wpkh({
194
- address,
195
- network,
196
- })
197
- break
198
- case "p2sh":
199
- addressObject = bitcoin.payments.p2sh({
200
- address,
201
- network,
202
- })
203
- break
204
- default:
205
- throw new Error("address type is incorrect")
206
- }
207
- return { addressObject, addressType }
208
- }
209
-
210
- function getPublicKeyHexByXpubAndIndex(
211
- xpub,
109
+ export function getPublicKeyHexByXpubAndIndex(
110
+ xpub: string,
212
111
  index = 0,
213
112
  isChange = false,
214
113
  network = networks.bitcoin,
@@ -220,22 +119,32 @@ function getPublicKeyHexByXpubAndIndex(
220
119
  .publicKey.toString("hex")
221
120
  }
222
121
 
223
- function getPubKeyFromPrivateKeyWIF(privateKeyWIF, network = bitcoin.networks.bitcoin) {
122
+ export function getPubKeyFromPrivateKeyWIF(
123
+ privateKeyWIF: string,
124
+ network = bitcoin.networks.bitcoin,
125
+ ) {
224
126
  let key = bitcoin.ECPair.fromWIF(privateKeyWIF, network)
225
127
  return key.publicKey
226
128
  }
227
129
 
228
- function getPubKeyFromPrivateKeyHex(privateKeyHex, network = bitcoin.networks.bitcoin) {
229
- let key = bitcoin.ECPair.fromPrivateKey(Buffer.from(privateKeyHex, "hex"), network)
130
+ export function getPubKeyFromPrivateKeyHex(
131
+ privateKeyHex: string,
132
+ network = bitcoin.networks.bitcoin,
133
+ ) {
134
+ let key = bitcoin.ECPair.fromPrivateKey(Buffer.from(privateKeyHex, "hex"), { network })
230
135
  return key.publicKey
231
136
  }
232
137
 
233
- function getPrivateKeyHexFromWIF(privateKeyWIF, network = bitcoin.networks.bitcoin) {
138
+ export function getPrivateKeyHexFromWIF(privateKeyWIF: string, network = bitcoin.networks.bitcoin) {
234
139
  let key = bitcoin.ECPair.fromWIF(privateKeyWIF, network)
235
- return key.privateKey.toString("hex")
140
+ return key.privateKey!.toString("hex")
236
141
  }
237
142
 
238
- function calculateMerkleProof(blockTransactions, txId, blockMerkleRoot = undefined) {
143
+ export function calculateMerkleProof(
144
+ blockTransactions: string[],
145
+ txId: string,
146
+ blockMerkleRoot?: string,
147
+ ) {
239
148
  let transactionIndex = blockTransactions.findIndex((tx) => tx === txId)
240
149
  if (transactionIndex < 0) throw new Error("txId is not in this tree")
241
150
  let data = blockTransactions.map((a) => Buffer.from(a, "hex").reverse())
@@ -244,17 +153,17 @@ function calculateMerkleProof(blockTransactions, txId, blockMerkleRoot = undefin
244
153
  blockMerkleRoot &&
245
154
  blockMerkleRoot !== fastRoot(data, bitcoin.crypto.hash256).toString("hex")
246
155
  ) {
247
- throw new Error("calculated anf block merkleRoot not matched")
156
+ throw new Error("calculated merkleRoot and block merkleRoot not matched")
248
157
  }
249
158
 
250
159
  let tree = merkle(data, bitcoin.crypto.hash256)
251
160
  let proof = merkleProof(tree, data[transactionIndex])
252
161
 
253
162
  let intermediateNodesArray = proof
254
- .map((_id) => _id && _id.toString("hex"))
255
- .filter((_id) => _id != null)
256
- let intermediateNodes = intermediateNodesArray.reduce(
257
- (a, value, index) =>
163
+ .map((_id?: Buffer) => _id && _id.toString("hex"))
164
+ .filter((_id?: Buffer) => _id != null)
165
+ let intermediateNodes: string = intermediateNodesArray.reduce(
166
+ (a: string, value: string, index: number) =>
258
167
  index !== transactionIndex % 2 && index < intermediateNodesArray.length - 1 ? a + value : a,
259
168
  "0x",
260
169
  )
@@ -264,8 +173,10 @@ function calculateMerkleProof(blockTransactions, txId, blockMerkleRoot = undefin
264
173
  }
265
174
  }
266
175
 
267
- function parseBlockHeader(headerHex) {
268
- const size = {
176
+ export function parseBlockHeader(headerHex: string) {
177
+ const size: {
178
+ [key: string]: number
179
+ } = {
269
180
  version: 4,
270
181
  previousBlockHash: 32,
271
182
  merkleRoot: 32,
@@ -274,32 +185,45 @@ function parseBlockHeader(headerHex) {
274
185
  nonce: 4,
275
186
  }
276
187
  let offset = 0
277
- let result = {}
188
+ let result: {
189
+ [key: string]: string
190
+ } = {}
278
191
  for (let key in size) {
279
192
  result[key] = headerHex.slice(offset, offset + size[key] * 2)
280
193
  offset += size[key] * 2
281
194
  }
282
- return result
195
+ return result as {
196
+ version: string
197
+ previousBlockHash: string
198
+ merkleRoot: string
199
+ timestamp: string
200
+ difficulty: string
201
+ nonce: string
202
+ }
283
203
  }
284
204
 
285
- function convertBitcoinScriptToAddress(script, network = bitcoin.networks.bitcoin) {
205
+ export function convertBitcoinScriptToAddress(script: Buffer, network = bitcoin.networks.bitcoin) {
286
206
  try {
287
- return bitcoin.address?.fromOutputScript(script, network)
207
+ return bitcoin.address.fromOutputScript(script, network)
288
208
  } catch (error) {
289
209
  return null
290
210
  }
291
211
  }
292
212
 
293
- function parseRawBlock(rawBlockHex, blockNumber = undefined, network = bitcoin.networks.network) {
213
+ export function parseRawBlock(
214
+ rawBlockHex: string,
215
+ blockNumber?: number,
216
+ network = bitcoin.networks.bitcoin,
217
+ ) {
294
218
  let block = bitcoin.Block.fromBuffer(Buffer.from(rawBlockHex, "hex"))
295
219
  let blockHash = block.getHash().toString("hex")
296
- let merkleRoot = block.merkleRoot.toString("hex")
297
- let prvBlockHash = block.prevHash.toString("hex")
220
+ let merkleRoot = block.merkleRoot!.toString("hex")
221
+ let prvBlockHash = block.prevHash!.toString("hex")
298
222
  return {
299
223
  blockNumber,
300
224
  merkleRoot,
301
225
  prvBlockHash,
302
- transactions: block.transactions.map((tx) => ({
226
+ transactions: block.transactions?.map((tx) => ({
303
227
  txId: tx.getId(),
304
228
  version: tx.version,
305
229
  locktime: tx.locktime,
@@ -319,23 +243,23 @@ function parseRawBlock(rawBlockHex, blockNumber = undefined, network = bitcoin.n
319
243
  }
320
244
  }
321
245
 
322
- function extractTransactionsAndBlockInfoFromRawBlock(
323
- rawBlockHex,
324
- blockNumber,
325
- addresses = [],
326
- /**
327
- * inputTxIds = {
328
- * txId : string
329
- * index: number
330
- * }[]
331
- */
332
- inputTxIds = [],
246
+ export function extractTransactionsAndBlockInfoFromRawBlock(
247
+ rawBlockHex: string,
248
+ blockNumber: number,
249
+ addresses: string[] = [],
250
+ inputTxIds: {
251
+ txId: string
252
+ index: number
253
+ address?: string
254
+ script?: string
255
+ value?: number
256
+ }[] = [],
333
257
  network = bitcoin.networks.bitcoin,
334
258
  ) {
335
259
  let block = bitcoin.Block.fromBuffer(Buffer.from(rawBlockHex, "hex"))
336
260
  let blockHash = block.getHash().reverse().toString("hex")
337
- let merkleRoot = block.merkleRoot.toString("hex")
338
- let prvBlockHash = block.prevHash.toString("hex")
261
+ let merkleRoot = block.merkleRoot!.toString("hex")
262
+ let prvBlockHash = block.prevHash!.toString("hex")
339
263
 
340
264
  let blockInfo = {
341
265
  blockNumber,
@@ -345,17 +269,43 @@ function extractTransactionsAndBlockInfoFromRawBlock(
345
269
  }
346
270
 
347
271
  let addressScript = addresses.map((address) =>
348
- createAddressObjectByAddress(address, network).addressObject.output.toString("hex"),
272
+ createAddressObjectByAddress(address, network).addressObject.output!.toString("hex"),
349
273
  )
350
274
 
351
- let blockTxIds = block.transactions.map((tx) => tx.getId())
275
+ let blockTxIds = block.transactions!.map((tx) => tx.getId())
276
+
277
+ type Transaction = {
278
+ txId: string
279
+ version: number
280
+ locktime: number
281
+ blockNumber: number
282
+ blockHash: string
283
+ merkleProof: {
284
+ intermediateNodes: string
285
+ transactionIndex: number
286
+ }
287
+ vout: {
288
+ address: string | null
289
+ script: string
290
+ value: number
291
+ }[]
292
+ vin: {
293
+ txId: string
294
+ index: number
295
+ address?: string | null
296
+ script?: string | null
297
+ value?: number | null
298
+ }[]
299
+ address: string
300
+ addressScript: string
301
+ }
352
302
 
353
- let withdrawTxs = []
354
- let depositTxs = []
355
- block.transactions.forEach((tx) => {
303
+ let withdrawTxs: Transaction[] = []
304
+ let depositTxs: Transaction[] = []
305
+ block.transactions?.forEach((tx) => {
356
306
  let txId = tx.getId()
357
307
 
358
- let inputTxAddress
308
+ let inputTxAddress: string | undefined
359
309
  let isWithdraw = tx.ins.find((vi) => {
360
310
  let viTxId = Buffer.from(vi.hash).reverse().toString("hex")
361
311
  let viInput = inputTxIds.find((vin) => vin.txId === viTxId && vin.index === vi.index)
@@ -383,20 +333,20 @@ function extractTransactionsAndBlockInfoFromRawBlock(
383
333
  return {
384
334
  txId: Buffer.from(vi.hash).reverse().toString("hex"),
385
335
  index: vi.index,
386
- address: viInput.address || null,
387
- script: viInput.script || null,
388
- value: viInput.value || null,
336
+ address: viInput?.address || null,
337
+ script: viInput?.script || null,
338
+ value: viInput?.value || null,
389
339
  }
390
340
  }),
391
341
  address: inputTxAddress,
392
342
  addressScript: createAddressObjectByAddress(
393
343
  inputTxAddress,
394
344
  network,
395
- ).addressObject.output.toString("hex"),
345
+ ).addressObject.output!.toString("hex"),
396
346
  })
397
347
  }
398
348
 
399
- let addressIndex
349
+ let addressIndex: number = -1
400
350
  let isDeposit = tx.outs.find((blockTxVo) => {
401
351
  let sIndex = addressScript.findIndex(
402
352
  (addScript) => addScript === blockTxVo.script.toString("hex"),
@@ -439,7 +389,7 @@ function extractTransactionsAndBlockInfoFromRawBlock(
439
389
  }
440
390
  }
441
391
 
442
- function validateAddress(address, network = bitcoin.networks.bitcoin) {
392
+ export function validateAddress(address: string, network = bitcoin.networks.bitcoin) {
443
393
  try {
444
394
  let isValid = false
445
395
  let isAddressSegwit = address.startsWith(network.bech32)
@@ -458,30 +408,201 @@ function validateAddress(address, network = bitcoin.networks.bitcoin) {
458
408
  }
459
409
  }
460
410
 
461
- module.exports = {
462
- parseRawTransaction,
463
- calculateMerkleProof,
464
- createAddressObjectByHash,
465
- createAddressObjectByPublicKey,
466
- createAddressObjectByAddress,
467
- createAddressObjectByScript,
468
-
469
- // ------------------------
470
- getAddressType,
471
- getPubKeyFromPrivateKeyWIF,
472
- getPubKeyFromPrivateKeyHex,
473
- getPublicKeyHexByXpubAndIndex,
474
- getPrivateKeyHexFromWIF,
475
- reverseBytes,
476
-
477
- parseBlockHeader,
478
- generateMnemonic,
479
-
480
- // ---------------------------
481
- parseRawBlock,
482
- extractTransactionsAndBlockInfoFromRawBlock,
483
- // -----------------------------
484
- validateAddress,
485
-
486
- networks,
411
+ function toXOnlyPublicKey(pubKey: Buffer) {
412
+ return pubKey.length === 32 ? pubKey : pubKey.slice(1, 33)
413
+ }
414
+
415
+ export function getAddressType(address: string, network = bitcoin.networks.bitcoin) {
416
+ if (address.startsWith(`${network.bech32}1p`)) {
417
+ // todo : check length - it could be p2wsh
418
+ return "p2tr"
419
+ }
420
+ if (address.startsWith(network.bech32)) {
421
+ // todo : check length - it could be p2wsh
422
+ return "p2wpkh"
423
+ }
424
+ let base58Data = bitcoin.address.fromBase58Check(address)
425
+ if (base58Data.version === Number(network.scriptHash)) {
426
+ return "p2sh"
427
+ }
428
+ if (base58Data.version === Number(network.pubKeyHash)) {
429
+ return "p2pkh"
430
+ }
431
+
432
+ throw new Error("invalid address")
433
+ }
434
+
435
+ export function createAddressObjectByScript(
436
+ { addressType, script }: { addressType: string; script: Buffer },
437
+ network: bitcoin.Network = bitcoin.networks.bitcoin,
438
+ ) {
439
+ let addressObject
440
+ switch (addressType) {
441
+ case "p2pkh":
442
+ addressObject = bitcoin.payments.p2pkh({
443
+ output: script,
444
+ network,
445
+ })
446
+ break
447
+ case "p2wpkh":
448
+ addressObject = bitcoin.payments.p2wpkh({
449
+ output: script,
450
+ network,
451
+ })
452
+ break
453
+ case "p2sh":
454
+ addressObject = bitcoin.payments.p2sh({
455
+ output: script,
456
+ network,
457
+ })
458
+ break
459
+ case "p2wsh":
460
+ addressObject = bitcoin.payments.p2wsh({
461
+ output: script,
462
+ network,
463
+ })
464
+ break
465
+ // case "p2tr":
466
+ // addressObject = bitcoin.payments.p2tr({
467
+ // output: script,
468
+ // network,
469
+ // })
470
+ // break
471
+ default:
472
+ throw new Error("address type is incorrect")
473
+ }
474
+ return addressObject
475
+ }
476
+
477
+ export function createAddressObjectByHash(
478
+ { addressType, hash }: { addressType: string; hash: Buffer },
479
+ network: bitcoin.Network = bitcoin.networks.bitcoin,
480
+ ) {
481
+ let addressObject
482
+ switch (addressType) {
483
+ case "p2pkh":
484
+ addressObject = bitcoin.payments.p2pkh({
485
+ hash,
486
+ network,
487
+ })
488
+ break
489
+ case "p2wpkh":
490
+ addressObject = bitcoin.payments.p2wpkh({
491
+ hash,
492
+ network,
493
+ })
494
+ break
495
+ case "p2sh":
496
+ addressObject = bitcoin.payments.p2sh({
497
+ hash,
498
+ network,
499
+ })
500
+ break
501
+ case "p2wsh":
502
+ addressObject = bitcoin.payments.p2wsh({
503
+ hash,
504
+ network,
505
+ })
506
+ break
507
+ // case "p2tr":
508
+ // // here hash is the public key of tweaked private key
509
+ // addressObject = bitcoin.payments.p2tr({
510
+ // pubkey: hash,
511
+ // network,
512
+ // })
513
+ // break
514
+ default:
515
+ throw new Error("address type is incorrect")
516
+ }
517
+ return addressObject
518
+ }
519
+
520
+ export function createAddressObjectByPublicKey(
521
+ { addressType, publicKey }: { addressType: string; publicKey: Buffer },
522
+ network: bitcoin.Network = bitcoin.networks.bitcoin,
523
+ ): bitcoin.payments.Payment {
524
+ let addressObject
525
+ switch (addressType) {
526
+ case "p2pkh":
527
+ addressObject = bitcoin.payments.p2pkh({
528
+ pubkey: publicKey,
529
+ network,
530
+ })
531
+ break
532
+ case "p2wpkh":
533
+ addressObject = bitcoin.payments.p2wpkh({
534
+ pubkey: publicKey,
535
+ network,
536
+ })
537
+ break
538
+ case "p2sh-p2wpkh":
539
+ addressObject = bitcoin.payments.p2sh({
540
+ redeem: bitcoin.payments.p2wpkh({
541
+ pubkey: publicKey,
542
+ network,
543
+ }),
544
+ })
545
+ break
546
+ case "p2tr":
547
+ // // this public key is public key of main private key
548
+ // addressObject = bitcoin.payments.p2tr({
549
+ // internalPubkey: toXOnlyPublicKey(publicKey),
550
+ // network,
551
+ // })
552
+ // break
553
+ default:
554
+ throw new Error("address type is incorrect")
555
+ }
556
+ return addressObject
557
+ }
558
+
559
+ export function createAddressObjectByAddress(address: string, network = bitcoin.networks.bitcoin) {
560
+ let addressType = getAddressType(address, network)
561
+ let addressObject
562
+ switch (addressType) {
563
+ case "p2pkh":
564
+ addressObject = bitcoin.payments.p2pkh({
565
+ address,
566
+ network,
567
+ })
568
+ break
569
+ case "p2wpkh":
570
+ addressObject = bitcoin.payments.p2wpkh({
571
+ address,
572
+ network,
573
+ })
574
+ break
575
+ case "p2sh":
576
+ addressObject = bitcoin.payments.p2sh({
577
+ address,
578
+ network,
579
+ })
580
+ break
581
+ // case "p2tr":
582
+ // addressObject = bitcoin.payments.p2tr({
583
+ // address,
584
+ // network,
585
+ // })
586
+ // break
587
+ default:
588
+ throw new Error("address type is incorrect")
589
+ }
590
+ return { addressObject, addressType }
591
+ }
592
+
593
+ export function publicKeyConvertor(publicKeyHex: string, compressed = true) {
594
+ let pubkey = ECPair.fromPublicKey(Buffer.from(publicKeyHex, "hex"), {
595
+ compressed,
596
+ })
597
+ return pubkey.publicKey.toString("hex")
598
+ }
599
+
600
+ export const addressTypeHelper: {
601
+ addressTypesNumber: {
602
+ [key: string]: number
603
+ }
604
+ addressTypes: string[]
605
+ } = {
606
+ addressTypesNumber: { p2pk: 0, p2pkh: 1, p2sh: 2, p2wpkh: 3, p2wsh: 4, p2tr: 5 },
607
+ addressTypes: ["p2pk", "p2pkh", "p2sh", "p2wpkh", "p2wsh", "p2tr"],
487
608
  }
package/src/index.ts CHANGED
@@ -1,8 +1,5 @@
1
- // eslint-disable-next-line global-require
2
- import { default as TeleportDaoPayment } from "./teleport-dao-payments"
3
- import bitcoinUtils from "./bitcoin-utils"
4
- import BitcoinInterface from "./bitcoin-interface"
5
- import BitcoinInterfaceUtils from "./bitcoin-interface-utils"
6
- import { BitcoinBase } from "./bitcoin-base"
7
-
8
- export { TeleportDaoPayment, BitcoinInterface, BitcoinInterfaceUtils, bitcoinUtils, BitcoinBase }
1
+ export * as bitcoinUtils from "./bitcoin-utils"
2
+ export * from "./teleport-dao-payments"
3
+ export * from "./bitcoin-interface"
4
+ export * from "./bitcoin-interface-utils"
5
+ export * from "./bitcoin-base"