@teleportdao/bitcoin 1.4.7 → 1.4.9
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.
- package/dist/bitcoin-base.d.ts.map +1 -1
- package/dist/bitcoin-base.js +3 -1
- package/dist/bitcoin-base.js.map +1 -1
- package/dist/bitcoin-interface-utils.d.ts +2 -2
- package/dist/bitcoin-interface.d.ts +8 -61
- package/dist/bitcoin-interface.d.ts.map +1 -1
- package/dist/bitcoin-interface.js +8 -8
- package/dist/bitcoin-interface.js.map +1 -1
- package/dist/bitcoin-utils-2.d.ts +2 -0
- package/dist/bitcoin-utils-2.d.ts.map +1 -0
- package/dist/bitcoin-utils-2.js +13 -0
- package/dist/bitcoin-utils-2.js.map +1 -0
- package/dist/bitcoin-utils.d.ts +6 -6
- package/dist/bitcoin-utils.d.ts.map +1 -1
- package/dist/bitcoin-utils.js +58 -31
- package/dist/bitcoin-utils.js.map +1 -1
- package/dist/bundle.js +9 -5
- package/dist/mempool-space.d.ts +69 -0
- package/dist/mempool-space.d.ts.map +1 -0
- package/dist/mempool-space.js +266 -0
- package/dist/mempool-space.js.map +1 -0
- package/dist/sign/sign-transaction.d.ts.map +1 -1
- package/dist/sign/sign-transaction.js +60 -2
- package/dist/sign/sign-transaction.js.map +1 -1
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/transaction-builder.js +23 -21
- package/dist/transaction-builder/transaction-builder.js.map +1 -1
- package/package.json +15 -11
- package/src/bitcoin-base.ts +5 -2
- package/src/bitcoin-interface.ts +16 -15
- package/src/bitcoin-utils.ts +72 -73
- package/src/mempool-space.ts +255 -0
- package/src/sign/sign-transaction.ts +49 -48
- package/src/transaction-builder/transaction-builder.ts +38 -20
|
@@ -1,16 +1,44 @@
|
|
|
1
|
-
import { Psbt, crypto, Network
|
|
2
|
-
// import
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
|
|
1
|
+
import { Psbt, crypto, Network } from "bitcoinjs-lib"
|
|
2
|
+
// import BIP32Factory from "bip32"
|
|
3
|
+
import ecc from "@bitcoinerlab/secp256k1"
|
|
4
|
+
import * as bitcoinEcPair from "bitcoinjs-ecpair"
|
|
5
|
+
const ECPair = bitcoinEcPair.ECPair
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
function tapTweakHash(pubKey: Buffer, h?: Buffer) {
|
|
8
|
+
return crypto.taggedHash("TapTweak", Buffer.concat(h ? [pubKey, h] : [pubKey]))
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function tweakSigner(
|
|
12
|
+
privateKey: Buffer,
|
|
13
|
+
opts = {} as {
|
|
14
|
+
[key: string]: Buffer
|
|
15
|
+
},
|
|
16
|
+
network: Network,
|
|
17
|
+
) {
|
|
18
|
+
let newPrv = privateKey
|
|
19
|
+
let keyPair = ECPair.fromPrivateKey(privateKey, {
|
|
20
|
+
network: network,
|
|
21
|
+
compressed: true,
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
if (!keyPair.privateKey) throw new Error("private key not exist")
|
|
25
|
+
|
|
26
|
+
if (keyPair.publicKey.toString("hex").startsWith("03")) {
|
|
27
|
+
newPrv = ecc.privateNegate(keyPair.privateKey) as Buffer
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const tweakedPrivateKey = ecc.privateAdd(
|
|
31
|
+
newPrv,
|
|
32
|
+
tapTweakHash(Buffer.from(keyPair.publicKey.toString("hex").slice(2), "hex"), opts?.tweakHash),
|
|
33
|
+
)
|
|
34
|
+
if (!tweakedPrivateKey) {
|
|
35
|
+
throw new Error("Invalid tweaked private key!")
|
|
36
|
+
}
|
|
10
37
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
38
|
+
return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
|
|
39
|
+
network: network,
|
|
40
|
+
})
|
|
41
|
+
}
|
|
14
42
|
|
|
15
43
|
class BitcoinLikeSignTransaction {
|
|
16
44
|
network: Network
|
|
@@ -18,38 +46,6 @@ class BitcoinLikeSignTransaction {
|
|
|
18
46
|
this.network = network
|
|
19
47
|
}
|
|
20
48
|
|
|
21
|
-
// tweakSigner(
|
|
22
|
-
// privateKey: Buffer,
|
|
23
|
-
// opts = {} as {
|
|
24
|
-
// [key: string]: Buffer
|
|
25
|
-
// },
|
|
26
|
-
// ) {
|
|
27
|
-
// let newPrv = privateKey
|
|
28
|
-
// let keyPair = ECPair.fromPrivateKey(privateKey, {
|
|
29
|
-
// network: this.network,
|
|
30
|
-
// compressed: true,
|
|
31
|
-
// })
|
|
32
|
-
|
|
33
|
-
// if (!keyPair.privateKey) throw new Error("private key not exist")
|
|
34
|
-
|
|
35
|
-
// if (keyPair.publicKey.toString("hex").startsWith("03")) {
|
|
36
|
-
// newPrv = ecc.privateNegate(keyPair.privateKey) as Buffer
|
|
37
|
-
// }
|
|
38
|
-
|
|
39
|
-
// const tweakedPrivateKey = ecc.privateAdd(
|
|
40
|
-
// newPrv,
|
|
41
|
-
// tapTweakHash(Buffer.from(keyPair.publicKey.toString("hex").slice(2), "hex"), opts?.tweakHash),
|
|
42
|
-
// )
|
|
43
|
-
// if (!tweakedPrivateKey) {
|
|
44
|
-
// throw new Error("Invalid tweaked private key!")
|
|
45
|
-
// }
|
|
46
|
-
|
|
47
|
-
// return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
|
|
48
|
-
// network: this.network,
|
|
49
|
-
// })
|
|
50
|
-
// }
|
|
51
|
-
|
|
52
|
-
// todo : change to psbt
|
|
53
49
|
async signPsbt(
|
|
54
50
|
unsignedPsbt: {
|
|
55
51
|
unsignedTransaction: string
|
|
@@ -68,7 +64,13 @@ class BitcoinLikeSignTransaction {
|
|
|
68
64
|
let numberOfInputs = psbt.inputCount
|
|
69
65
|
|
|
70
66
|
for (let i = 0; i < numberOfInputs; i += 1) {
|
|
71
|
-
|
|
67
|
+
let type = psbt.getInputType(i)
|
|
68
|
+
if (type === "nonstandard") {
|
|
69
|
+
let a = tweakSigner(privateKey, undefined, this.network)
|
|
70
|
+
await psbt.signInputAsync(i, a)
|
|
71
|
+
} else {
|
|
72
|
+
await psbt.signInputAsync(i, keyPair)
|
|
73
|
+
}
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
// psbt.signAllInputs(keyPair)
|
|
@@ -82,13 +84,12 @@ class BitcoinLikeSignTransaction {
|
|
|
82
84
|
)
|
|
83
85
|
const psbt =
|
|
84
86
|
finals.length === 1 ? finals[0] : new Psbt({ network: this.network }).combine(...finals)
|
|
87
|
+
|
|
85
88
|
psbt.finalizeAllInputs()
|
|
86
89
|
|
|
87
|
-
// psbt.finalizeAllInputs()
|
|
88
|
-
// const tx = psbt.extractTransaction()
|
|
89
|
-
// const rawTx = tx.toBuffer()
|
|
90
|
-
// const hex = rawTx.toString("hex")
|
|
91
90
|
let finalizeTx = psbt.extractTransaction()
|
|
91
|
+
// const rawTx = finalizeTx.toBuffer()
|
|
92
|
+
// const hex = rawTx.toString("hex")
|
|
92
93
|
return finalizeTx.toHex()
|
|
93
94
|
}
|
|
94
95
|
}
|
|
@@ -19,7 +19,7 @@ const componentBytes = {
|
|
|
19
19
|
bytePerInput: {
|
|
20
20
|
p2pkh: TX_INPUT_BASE + TX_INPUT_P2PKH,
|
|
21
21
|
p2wpkh: TX_INPUT_BASE + TX_INPUT_P2WPKH,
|
|
22
|
-
|
|
22
|
+
"p2sh-p2wpkh": TX_INPUT_BASE + TX_INPUT_P2SH_P2PKH,
|
|
23
23
|
},
|
|
24
24
|
baseTxBytes: TX_EMPTY_SIZE,
|
|
25
25
|
bytePerOutput: TX_OUTPUT_BASE + TX_OUTPUT_P2PKH,
|
|
@@ -230,29 +230,47 @@ class BaseBitcoinLikeTransaction {
|
|
|
230
230
|
if (!inputs || !outputs) {
|
|
231
231
|
throw new Error("not enough balance")
|
|
232
232
|
}
|
|
233
|
+
// console.log(inputs, outputs, feeRate)
|
|
234
|
+
const inputsSizes = inputs.map(
|
|
235
|
+
(i) =>
|
|
236
|
+
componentBytes.bytePerInput[
|
|
237
|
+
i.signerInfo.addressType as keyof typeof componentBytes.bytePerInput
|
|
238
|
+
],
|
|
239
|
+
)
|
|
240
|
+
const outputSizes = outputs.length * componentBytes.bytePerOutput
|
|
241
|
+
|
|
242
|
+
// console.log(inputsSizes, outputSizes)
|
|
243
|
+
|
|
244
|
+
const txSize =
|
|
245
|
+
componentBytes.baseTxBytes +
|
|
246
|
+
inputsSizes.reduce((a, c) => a + c, 0) +
|
|
247
|
+
outputSizes +
|
|
248
|
+
componentBytes.bytePerOutput
|
|
249
|
+
|
|
250
|
+
const txFee = txSize * feeRate
|
|
251
|
+
let diff = fee - txFee
|
|
252
|
+
// console.log({ txSize, txFee, diff, fee })
|
|
253
|
+
|
|
233
254
|
let changeIndex = outputs.findIndex((x) => !x?.address && !x.script && (x.value || 0) > 0)
|
|
234
255
|
let change: ChangeTarget | undefined
|
|
235
|
-
if (changeIndex >= 0) {
|
|
256
|
+
if (changeIndex >= 0 || diff > 600) {
|
|
257
|
+
if (changeIndex >= 0) {
|
|
258
|
+
diff = diff + componentBytes.bytePerOutput * feeRate
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (diff < 0) {
|
|
262
|
+
diff = 0
|
|
263
|
+
}
|
|
264
|
+
|
|
236
265
|
if (!changeObject) throw new Error("change not exist")
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
pubkey: Buffer.from(changeObject.publicKey, "hex"),
|
|
245
|
-
masterFingerprint: Buffer.from(changeObject.masterFingerprint, "hex"),
|
|
246
|
-
},
|
|
247
|
-
],
|
|
248
|
-
}
|
|
249
|
-
} else {
|
|
250
|
-
change = {
|
|
251
|
-
address: changeObject.address,
|
|
252
|
-
value: outputs[changeIndex].value,
|
|
253
|
-
}
|
|
266
|
+
change = {
|
|
267
|
+
address: changeObject.address,
|
|
268
|
+
value: changeIndex >= 0 ? outputs[changeIndex].value + diff : diff,
|
|
269
|
+
}
|
|
270
|
+
fee = fee - diff
|
|
271
|
+
if (changeIndex >= 0) {
|
|
272
|
+
outputs.splice(changeIndex, 1)
|
|
254
273
|
}
|
|
255
|
-
outputs.splice(changeIndex, 1)
|
|
256
274
|
}
|
|
257
275
|
|
|
258
276
|
return {
|