@teleportdao/bitcoin 1.4.3 → 1.4.6

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 (69) hide show
  1. package/dist/bitcoin-base.d.ts +66 -53
  2. package/dist/bitcoin-base.d.ts.map +1 -1
  3. package/dist/bitcoin-base.js +47 -38
  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 -156
  16. package/dist/bitcoin-utils.js.map +1 -1
  17. package/dist/bundle.js +13 -0
  18. package/dist/index.d.ts +5 -6
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +32 -12
  21. package/dist/index.js.map +1 -1
  22. package/dist/sign/sign-transaction.d.ts +9 -5
  23. package/dist/sign/sign-transaction.d.ts.map +1 -1
  24. package/dist/sign/sign-transaction.js +14 -11
  25. package/dist/sign/sign-transaction.js.map +1 -1
  26. package/dist/teleport-dao-payments.d.ts +68 -89
  27. package/dist/teleport-dao-payments.d.ts.map +1 -1
  28. package/dist/teleport-dao-payments.js +16 -4
  29. package/dist/teleport-dao-payments.js.map +1 -1
  30. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +30 -11
  31. package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
  32. package/dist/transaction-builder/bitcoin-transaction-builder.js +37 -9
  33. package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
  34. package/dist/transaction-builder/transaction-builder.d.ts +198 -9
  35. package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
  36. package/dist/transaction-builder/transaction-builder.js +291 -38
  37. package/dist/transaction-builder/transaction-builder.js.map +1 -1
  38. package/dist/utils/networks.d.ts +5 -35
  39. package/dist/utils/networks.d.ts.map +1 -1
  40. package/dist/utils/networks.js +26 -2
  41. package/dist/utils/networks.js.map +1 -1
  42. package/dist/utils/tools.d.ts +15 -9
  43. package/dist/utils/tools.d.ts.map +1 -1
  44. package/dist/utils/tools.js +14 -11
  45. package/dist/utils/tools.js.map +1 -1
  46. package/package.json +8 -6
  47. package/src/{bitcoin-base.js → bitcoin-base.ts} +248 -219
  48. package/src/{bitcoin-interface-utils.js → bitcoin-interface-utils.ts} +59 -53
  49. package/src/{bitcoin-interface.js → bitcoin-interface.ts} +420 -350
  50. package/src/{bitcoin-utils.js → bitcoin-utils.ts} +608 -483
  51. package/src/helper/teleport-request-helper.js +179 -179
  52. package/src/index.ts +5 -0
  53. package/src/sign/sign-transaction.ts +96 -0
  54. package/src/{teleport-dao-payments.js → teleport-dao-payments.ts} +341 -280
  55. package/src/transaction-builder/bitcoin-transaction-builder.ts +61 -0
  56. package/src/transaction-builder/transaction-builder.ts +567 -0
  57. package/src/utils/{networks.js → networks.ts} +33 -31
  58. package/src/utils/{tools.js → tools.ts} +80 -72
  59. package/tsconfig.json +10 -9
  60. package/webpack.config.js +16 -0
  61. package/dist/transaction-builder/transaction-builder-common.d.ts +0 -57
  62. package/dist/transaction-builder/transaction-builder-common.d.ts.map +0 -1
  63. package/dist/transaction-builder/transaction-builder-common.js +0 -183
  64. package/dist/transaction-builder/transaction-builder-common.js.map +0 -1
  65. package/src/index.js +0 -15
  66. package/src/sign/sign-transaction.js +0 -36
  67. package/src/transaction-builder/bitcoin-transaction-builder.js +0 -37
  68. package/src/transaction-builder/transaction-builder-common.js +0 -236
  69. package/src/transaction-builder/transaction-builder.js +0 -159
@@ -1,236 +0,0 @@
1
- /* eslint-disable no-underscore-dangle */
2
-
3
- const bitcoin = require("bitcoinjs-lib")
4
- const coinselect = require("coinselect")
5
- const coinselectSplit = require("coinselect/split")
6
- const coinselectAccumulative = require("coinselect/accumulative")
7
-
8
- const { createAddressObjectByPublicKey } = require("../bitcoin-utils")
9
-
10
- const TX_EMPTY_SIZE = 4 + 1 + 1 + 4
11
- const TX_INPUT_BASE = 32 + 4 + 1 + 4
12
- const TX_INPUT_P2PKH = 107
13
- const TX_INPUT_P2SH_P2PKH = 50
14
- const TX_INPUT_P2WPKH = 47
15
- const TX_OUTPUT_BASE = 8 + 1
16
- const TX_OUTPUT_P2PKH = 25
17
-
18
- const componentBytes = {
19
- bytePerInput: {
20
- p2pkh: TX_INPUT_BASE + TX_INPUT_P2PKH,
21
- p2wpkh: TX_INPUT_BASE + TX_INPUT_P2WPKH,
22
- p2shp2wpkh: TX_INPUT_BASE + TX_INPUT_P2SH_P2PKH,
23
- },
24
- baseTxBytes: TX_EMPTY_SIZE,
25
- bytePerOutput: TX_OUTPUT_BASE + TX_OUTPUT_P2PKH,
26
- }
27
-
28
- class BaseBitcoinLikeTransaction {
29
- // abstract
30
- constructor({
31
- network,
32
- testnet,
33
- feeMin = 0,
34
- dustLimit,
35
- maximumNumberOfOutputsInTransaction = 50,
36
- }) {
37
- this.testnet = testnet
38
- this.network = network
39
- this.maximumNumberOfOutputsInTransaction = maximumNumberOfOutputsInTransaction
40
- this.feeMin = feeMin
41
- this.dustLimit = dustLimit || 1 * 2 * componentBytes.bytePerInput.p2pkh
42
- }
43
-
44
- // eslint-disable-next-line no-unused-vars, class-methods-use-this
45
- async _getUtxo(userAddress) {
46
- // The child has implemented this method.
47
- throw new Error("Do not call abstract method directly")
48
- // return utxo
49
- }
50
-
51
- // eslint-disable-next-line no-unused-vars, class-methods-use-this
52
- async _getTransactionHex(transactionId) {
53
- // The child has implemented this method.
54
- throw new Error("Do not call abstract method directly")
55
- // return utxo
56
- }
57
-
58
- // eslint-disable-next-line no-unused-vars, class-methods-use-this
59
- async convertBaseInputsToInputs(baseInputs) {
60
- // The child has implemented this method.
61
- throw new Error("Do not call abstract method directly")
62
- // return utxo
63
- }
64
-
65
- // eslint-disable-next-line no-unused-vars, class-methods-use-this
66
- async createUnsignedTransaction(baseInputs) {
67
- // The child has implemented this method.
68
- throw new Error("Do not call abstract method directly")
69
- // return utxo
70
- }
71
-
72
- // eslint-disable-next-line no-unused-vars, class-methods-use-this
73
- createAddressObject({ addressType, publicKey }) {
74
- return createAddressObjectByPublicKey({ addressType, publicKey }, this.network)
75
- }
76
-
77
- validateAddress(address) {
78
- try {
79
- let isValid = false
80
- let network = this.network
81
- let isAddressSegwit = address.startsWith(network.bech32)
82
- if (isAddressSegwit) {
83
- bitcoin.address.fromBech32(address)
84
- isValid = true
85
- } else {
86
- let base58Data = bitcoin.address.fromBase58Check(address)
87
- isValid =
88
- base58Data.version === Number(network.scriptHash) ||
89
- base58Data.version === Number(network.pubKeyHash)
90
- }
91
- return isValid
92
- } catch (error) {
93
- return false
94
- }
95
- }
96
-
97
- static helperHandleInputsAndOutputs({
98
- targets,
99
- extendedUtxo,
100
- feeRate,
101
- changeAddress,
102
- selectType = "normal", // "accumulative" | "normal" | "full"
103
- }) {
104
- let selectResponse
105
- switch (selectType) {
106
- case "normal":
107
- selectResponse = coinselect(extendedUtxo, targets, Math.round(feeRate))
108
-
109
- break
110
- case "accumulative":
111
- selectResponse = coinselectAccumulative(extendedUtxo, targets, Math.round(feeRate))
112
- break
113
- case "full":
114
- selectResponse = coinselectSplit(
115
- extendedUtxo,
116
- [{ address: targets[0].address }],
117
- Math.round(feeRate),
118
- )
119
- break
120
-
121
- default:
122
- break
123
- }
124
- let { inputs, outputs, fee } = selectResponse
125
-
126
- if (!inputs || !outputs) {
127
- throw new Error("not enough balance")
128
- }
129
- let changeIndex = outputs.findIndex((x) => !x.address && !x.script && x.value > 0)
130
- let change
131
- if (changeIndex >= 0) {
132
- change = {
133
- address: changeAddress,
134
- value: outputs[changeIndex].value,
135
- }
136
- outputs.splice(changeIndex, 1)
137
- }
138
-
139
- return {
140
- inputs,
141
- fee,
142
- outputs,
143
- change,
144
- }
145
- }
146
-
147
- async getExtendedUtxo(signerInfo) {
148
- let utxo = await this._getUtxo(signerInfo.address)
149
- const extendedUtxo = utxo.map((input) => ({
150
- ...input,
151
- signerInfo,
152
- }))
153
- if (!extendedUtxo || extendedUtxo.length === 0) {
154
- throw new Error("no utxo found")
155
- }
156
- return extendedUtxo
157
- }
158
-
159
- /**
160
- *
161
- * @param {*} param0
162
- * @returns
163
- */
164
- async convertUtxoToInput({ extendedUtxo, targets, changeAddress, feeRate, selectType }) {
165
- let {
166
- inputs: filteredInputs,
167
- outputs,
168
- change,
169
- fee,
170
- } = BaseBitcoinLikeTransaction.helperHandleInputsAndOutputs({
171
- targets,
172
- extendedUtxo,
173
- feeRate,
174
- changeAddress,
175
- selectType,
176
- })
177
-
178
- let inputs = await this.convertBaseInputsToInputs(filteredInputs)
179
-
180
- return {
181
- inputs,
182
- outputs,
183
- change,
184
- fee,
185
- feeRate,
186
- }
187
- }
188
-
189
- /**
190
- *
191
- * @param {*} param0
192
- * @returns
193
- */
194
- async processUnsignedTransaction({
195
- extendedUtxo,
196
- targets = [],
197
- changeAddress = undefined,
198
- fullAmount = false,
199
- feeRate,
200
- selfTransaction = false,
201
- selectType = "normal",
202
- }) {
203
- if (!selfTransaction && targets.length === 0) throw new Error("no target")
204
-
205
- const { inputs, outputs, change, fee } = await this.convertUtxoToInput({
206
- extendedUtxo,
207
- targets,
208
- changeAddress,
209
- feeRate,
210
- selectType: fullAmount ? "full" : selectType,
211
- })
212
- let unsignedTransaction = await this.createUnsignedTransaction({
213
- inputs,
214
- outputs,
215
- change,
216
- fee,
217
- feeRate,
218
- })
219
-
220
- return unsignedTransaction
221
- }
222
-
223
- getOpReturnTarget(dataHex) {
224
- if (!dataHex.length > 0) throw new Error("invalid data in hex")
225
- const embed = bitcoin.payments.embed({
226
- data: [Buffer.from(dataHex, "hex")],
227
- network: this.network,
228
- })
229
- return {
230
- script: embed.output,
231
- value: 0,
232
- }
233
- }
234
- }
235
-
236
- module.exports = BaseBitcoinLikeTransaction
@@ -1,159 +0,0 @@
1
- const bitcoin = require("bitcoinjs-lib")
2
- const BaseBitcoinLikeTransactionBuilderCommon = require("./transaction-builder-common")
3
-
4
- class BaseBitcoinLikeTransaction extends BaseBitcoinLikeTransactionBuilderCommon {
5
- async convertBaseInputsToInputs(baseInputs = []) {
6
- let inputs = baseInputs
7
- let transactionId = null
8
- let transactionHex = null
9
- for (let i in inputs) {
10
- let { address, publicKey, derivationPath, masterFingerprint, addressType } =
11
- inputs[i].signerInfo
12
- let addressObject = this.createAddressObject({
13
- address,
14
- publicKey: publicKey ? Buffer.from(publicKey, "hex") : null,
15
- addressType,
16
- })
17
- if (derivationPath && masterFingerprint && publicKey && true) {
18
- inputs[i].bip32Derivation = [
19
- {
20
- path: derivationPath,
21
- pubkey: addressObject.pubkey,
22
- masterFingerprint: Buffer.from(masterFingerprint, "hex"),
23
- },
24
- ]
25
- }
26
- if (addressType === "p2pkh") {
27
- // add p2pkh data
28
- if (transactionId === inputs[i].hash) {
29
- inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex")
30
- } else {
31
- transactionHex = await this._getTransactionHex(inputs[i].hash)
32
- inputs[i].nonWitnessUtxo = Buffer.from(transactionHex, "hex")
33
- transactionId = inputs[i].hash
34
- }
35
- } else if (addressType === "p2wpkh") {
36
- // add p2wpkh data
37
- inputs[i].witnessUtxo = {
38
- script: addressObject.output,
39
- value: inputs[i].value,
40
- }
41
- } else if (addressType === "p2sh-p2wpkh") {
42
- // add p2sh-p2wpkh data
43
- inputs[i].witnessUtxo = {
44
- script: addressObject.output,
45
- value: inputs[i].value,
46
- }
47
- inputs[i].redeemScript = addressObject.redeem.output
48
- }
49
- }
50
-
51
- return inputs
52
- }
53
-
54
- /**
55
- *
56
- * @param {*} param0
57
- * @returns
58
- */
59
- createUnsignedTransaction({
60
- inputs,
61
- outputs,
62
- change,
63
- fee, // not used in this section - just returned
64
- feeRate,
65
- }) {
66
- const { network } = this
67
- const newPsbt = new bitcoin.Psbt({ network })
68
- newPsbt.setMaximumFeeRate = feeRate + feeRate / 100
69
- // add input
70
- for (const input of inputs) {
71
- let { addressType } = input.signerInfo
72
- switch (addressType) {
73
- case "p2pkh": {
74
- let i = {
75
- hash: input.hash,
76
- index: Number(input.index),
77
- nonWitnessUtxo: input.nonWitnessUtxo,
78
- sequence: 0xffffffff - 1,
79
- }
80
- if (input.bip32Derivation) i.bip32Derivation = input.bip32Derivation
81
- newPsbt.addInput(i)
82
- break
83
- }
84
- case "p2wpkh": {
85
- let i = {
86
- hash: input.hash,
87
- index: Number(input.index),
88
- witnessUtxo: input.witnessUtxo,
89
- sequence: 0xffffffff - 1,
90
- }
91
- if (input.bip32Derivation) i.bip32Derivation = input.bip32Derivation
92
- newPsbt.addInput(i)
93
- break
94
- }
95
- case "p2sh-p2wpkh": {
96
- let i = {
97
- hash: input.hash,
98
- index: Number(input.index),
99
- witnessUtxo: input.witnessUtxo,
100
- redeemScript: input.redeemScript,
101
- sequence: 0xffffffff - 1,
102
- }
103
- if (input.bip32Derivation) i.bip32Derivation = input.bip32Derivation
104
- newPsbt.addInput(i)
105
- break
106
- }
107
- default:
108
- throw new Error("address type is incorrect")
109
- }
110
- }
111
-
112
- // add outputs
113
- for (const target of outputs) {
114
- newPsbt.addOutput(target)
115
- }
116
-
117
- // add changeAddress
118
- if (change && Object.keys(change).length !== 0) {
119
- newPsbt.addOutput({
120
- address: change.address,
121
- value: Number(change.value),
122
- })
123
- }
124
-
125
- // check created outputs with targets
126
- for (let i in outputs) {
127
- if (newPsbt.txOutputs[i].address !== outputs[i].address) {
128
- throw new Error("error address")
129
- }
130
- if (newPsbt.txOutputs[i].value !== outputs[i].value) {
131
- throw new Error("error value")
132
- }
133
- }
134
- if (change && Object.keys(change).length !== 0) {
135
- if (newPsbt.txOutputs[outputs.length].address !== change.address) {
136
- throw new Error("error change address")
137
- }
138
- if (newPsbt.txOutputs[outputs.length].value !== change.value) {
139
- throw new Error("error change value")
140
- }
141
- }
142
-
143
- const unsignedPsbtBaseText = newPsbt.toBase64()
144
- return {
145
- unsignedTransaction: unsignedPsbtBaseText,
146
- outputs,
147
- inputs: inputs.map((tx) => ({
148
- hash: tx.hash,
149
- value: Number(tx.value),
150
- index: tx.index,
151
- signerInfo: tx.signerInfo,
152
- })),
153
- fee,
154
- change,
155
- }
156
- }
157
- }
158
-
159
- module.exports = BaseBitcoinLikeTransaction