@teleportdao/bitcoin 1.5.9 → 1.6.1
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.js +1 -1
- package/dist/bitcoin-base.js.map +1 -1
- package/dist/bitcoin-interface.d.ts.map +1 -1
- package/dist/bitcoin-interface.js +8 -2
- package/dist/bitcoin-interface.js.map +1 -1
- package/dist/bundle.js +7 -7
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/sign/index.d.ts +2 -0
- package/dist/sign/index.d.ts.map +1 -0
- package/dist/sign/index.js +9 -0
- package/dist/sign/index.js.map +1 -0
- package/dist/sign/sign-transaction.d.ts +4 -1
- package/dist/sign/sign-transaction.d.ts.map +1 -1
- package/dist/sign/sign-transaction.js +25 -48
- package/dist/sign/sign-transaction.js.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/bitcoin-transaction-builder.js +2 -5
- package/dist/transaction-builder/bitcoin-transaction-builder.js.map +1 -1
- package/dist/transaction-builder/index.d.ts +3 -0
- package/dist/transaction-builder/index.d.ts.map +1 -0
- package/dist/transaction-builder/index.js +24 -0
- package/dist/transaction-builder/index.js.map +1 -0
- package/dist/transaction-builder/transaction-builder.d.ts +27 -4
- package/dist/transaction-builder/transaction-builder.d.ts.map +1 -1
- package/dist/transaction-builder/transaction-builder.js +58 -52
- package/dist/transaction-builder/transaction-builder.js.map +1 -1
- package/dist/utils/tools.d.ts.map +1 -1
- package/dist/utils/tools.js +1 -0
- package/dist/utils/tools.js.map +1 -1
- package/package.json +3 -6
- package/src/bitcoin-base.ts +1 -1
- package/src/bitcoin-interface.ts +11 -3
- package/src/helper/burn-request-helper.js +27 -27
- package/src/index.ts +2 -0
- package/src/sign/index.ts +1 -0
- package/src/sign/sign-transaction.ts +39 -37
- package/src/transaction-builder/bitcoin-transaction-builder.ts +1 -1
- package/src/transaction-builder/index.ts +2 -0
- package/src/transaction-builder/transaction-builder.ts +97 -63
- package/src/utils/tools.ts +2 -0
- package/.tmp/check.ts +0 -101
- package/.tmp/rbf.ts +0 -22
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/utils/tools.ts"],"names":[],"mappings":"AAEA,iBAAS,KAAK,CAAC,EAAE,EAAE,MAAM,oBAExB;AAED,iBAAe,cAAc,CAC3B,MAAM,EAAE,MAAM,GAAG,EACjB,MAAM;;;CAGL,
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/utils/tools.ts"],"names":[],"mappings":"AAEA,iBAAS,KAAK,CAAC,EAAE,EAAE,MAAM,oBAExB;AAED,iBAAe,cAAc,CAC3B,MAAM,EAAE,MAAM,GAAG,EACjB,MAAM;;;CAGL,gBAgBF;AAED,iBAAS,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,UAEjD;AAED,iBAAS,gBAAgB,CAAC,EACxB,OAAO,EACP,OAAe,EACf,OAAY,EACZ,IAAI,GACL,EAAE;IACD,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAA;IACnC,IAAI,CAAC,EAAE;QACL,QAAQ,EAAE,MAAM,CAAA;QAChB,QAAQ,EAAE,MAAM,CAAA;KACjB,CAAA;CACF,iCAkCA;AAED,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,CAAA"}
|
package/dist/utils/tools.js
CHANGED
package/dist/utils/tools.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/ut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|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "teleswap bitcoin package",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"browser": "dist/bundle.js",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@bitcoinerlab/secp256k1": "^1.0.5",
|
|
18
18
|
"@teleportdao/configs": "^1.5.6",
|
|
19
|
-
"@teleportdao/providers": "^1.
|
|
19
|
+
"@teleportdao/providers": "^1.6.0",
|
|
20
20
|
"axios": "^0.27.2",
|
|
21
21
|
"bignumber.js": "^9.1.1",
|
|
22
22
|
"bip32": "^4.0.0",
|
|
@@ -26,15 +26,12 @@
|
|
|
26
26
|
"coinselect": "^3.1.13",
|
|
27
27
|
"ecpair": "^2.0.1",
|
|
28
28
|
"merkle-lib": "^2.0.10",
|
|
29
|
-
"merkle-patricia-tree": "^2.3.2",
|
|
30
|
-
"npm": "^10.2.3",
|
|
31
|
-
"uninstall": "^0.0.0",
|
|
32
29
|
"varuint-bitcoin": "^1.1.2"
|
|
33
30
|
},
|
|
34
31
|
"publishConfig": {
|
|
35
32
|
"access": "public"
|
|
36
33
|
},
|
|
37
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "739b0206e1024364e8f7ee19f9ecaf2f48889553",
|
|
38
35
|
"devDependencies": {
|
|
39
36
|
"@babel/preset-react": "^7.23.3"
|
|
40
37
|
}
|
package/src/bitcoin-base.ts
CHANGED
|
@@ -280,7 +280,7 @@ class BitcoinBase {
|
|
|
280
280
|
}
|
|
281
281
|
|
|
282
282
|
let changeIndex = transaction.vout.findIndex((vo) =>
|
|
283
|
-
transaction.vin.find((vi) => vo.address === vi.address),
|
|
283
|
+
transaction.vin.find((vi) => vo.address === vi.address || vo.address === changeAddress),
|
|
284
284
|
)
|
|
285
285
|
|
|
286
286
|
const feeRate = staticFeeRate || (await this.transactionBuilder._getFeeRate("fast"))
|
package/src/bitcoin-interface.ts
CHANGED
|
@@ -174,7 +174,10 @@ export class BitcoinInterface extends BitcoinInterfaceUtils {
|
|
|
174
174
|
if (!this.rpcProvider) {
|
|
175
175
|
throw new Error("RPC provider not set")
|
|
176
176
|
}
|
|
177
|
-
let txIds = await this.rpcProvider
|
|
177
|
+
let txIds = await runWithRetries(() => this.rpcProvider!.getBlockTransactionIds(blockHash), {
|
|
178
|
+
maxTries: 10,
|
|
179
|
+
retrySleep: 2000,
|
|
180
|
+
})
|
|
178
181
|
// let a = await this.provider.getMerkleProof(txId)
|
|
179
182
|
let proof = calculateMerkleProof(txIds, txId)
|
|
180
183
|
// console.log(a.intermediateNodes === proof.intermediateNodes)
|
|
@@ -265,7 +268,13 @@ export class BitcoinInterface extends BitcoinInterfaceUtils {
|
|
|
265
268
|
if (!this.rpcProvider) {
|
|
266
269
|
throw new Error("RPC provider not set")
|
|
267
270
|
}
|
|
268
|
-
let rawBlockHex = await
|
|
271
|
+
let rawBlockHex = await runWithRetries(
|
|
272
|
+
() => this.rpcProvider!.getBlockByBlockNumber(blockNumber, 0),
|
|
273
|
+
{
|
|
274
|
+
maxTries: 10,
|
|
275
|
+
retrySleep: 2000,
|
|
276
|
+
},
|
|
277
|
+
)
|
|
269
278
|
let { withdrawTxs, depositTxs } = extractTransactionsAndBlockInfoFromRawBlock(
|
|
270
279
|
rawBlockHex,
|
|
271
280
|
blockNumber,
|
|
@@ -289,7 +298,6 @@ export class BitcoinInterface extends BitcoinInterfaceUtils {
|
|
|
289
298
|
let blockTxs = []
|
|
290
299
|
for (let blockNumber = +startBlockNumber + 1; blockNumber <= endBlockNumber; blockNumber += 1) {
|
|
291
300
|
console.log(blockNumber)
|
|
292
|
-
|
|
293
301
|
const response = await this.getBlockTransactions(addresses, blockNumber, inputTxIds)
|
|
294
302
|
blockTxs.push(response)
|
|
295
303
|
// await sleep(200)
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
function getBurnTransactionInfo(address, vin = [], vouts = []) {
|
|
2
|
-
let lockerVinIndex = vin.findIndex((vi) => vi.address === address)
|
|
3
|
-
if (lockerVinIndex >= 0) {
|
|
4
|
-
let lockerVin = vin[lockerVinIndex]
|
|
5
|
-
lockerVin.vinIndex = lockerVinIndex
|
|
6
|
-
let totalInputValue = vin.reduce((acc, current) => +acc + +current.value, 0)
|
|
7
|
-
let receivers = []
|
|
8
|
-
let changes = []
|
|
9
|
-
for (let i in vouts) {
|
|
10
|
-
let vout = {
|
|
11
|
-
...vouts[i],
|
|
12
|
-
index: i,
|
|
13
|
-
}
|
|
14
|
-
if (vout.address === address) {
|
|
15
|
-
changes.push(vout)
|
|
16
|
-
} else {
|
|
17
|
-
receivers.push(vout)
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return { receivers, changes, totalInputValue, lockerVin }
|
|
21
|
-
}
|
|
22
|
-
return null
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
module.exports = {
|
|
26
|
-
getBurnTransactionInfo,
|
|
27
|
-
}
|
|
1
|
+
function getBurnTransactionInfo(address, vin = [], vouts = []) {
|
|
2
|
+
let lockerVinIndex = vin.findIndex((vi) => vi.address === address)
|
|
3
|
+
if (lockerVinIndex >= 0) {
|
|
4
|
+
let lockerVin = vin[lockerVinIndex]
|
|
5
|
+
lockerVin.vinIndex = lockerVinIndex
|
|
6
|
+
let totalInputValue = vin.reduce((acc, current) => +acc + +current.value, 0)
|
|
7
|
+
let receivers = []
|
|
8
|
+
let changes = []
|
|
9
|
+
for (let i in vouts) {
|
|
10
|
+
let vout = {
|
|
11
|
+
...vouts[i],
|
|
12
|
+
index: i,
|
|
13
|
+
}
|
|
14
|
+
if (vout.address === address) {
|
|
15
|
+
changes.push(vout)
|
|
16
|
+
} else {
|
|
17
|
+
receivers.push(vout)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return { receivers, changes, totalInputValue, lockerVin }
|
|
21
|
+
}
|
|
22
|
+
return null
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = {
|
|
26
|
+
getBurnTransactionInfo,
|
|
27
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as BaseBitcoinSigner } from "./sign-transaction"
|
|
@@ -1,56 +1,58 @@
|
|
|
1
1
|
import { Psbt, crypto, Network } from "bitcoinjs-lib"
|
|
2
2
|
// import BIP32Factory from "bip32"
|
|
3
3
|
import ecc from "@bitcoinerlab/secp256k1"
|
|
4
|
-
import
|
|
5
|
-
|
|
4
|
+
import ECPairFactory from "ecpair"
|
|
5
|
+
|
|
6
|
+
const ECPair = ECPairFactory(ecc)
|
|
6
7
|
|
|
7
8
|
function tapTweakHash(pubKey: Buffer, h?: Buffer) {
|
|
8
9
|
return crypto.taggedHash("TapTweak", Buffer.concat(h ? [pubKey, h] : [pubKey]))
|
|
9
10
|
}
|
|
10
11
|
|
|
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
|
-
}
|
|
37
|
-
|
|
38
|
-
return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
|
|
39
|
-
network: network,
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
|
|
43
12
|
class BitcoinLikeSignTransaction {
|
|
44
13
|
network: Network
|
|
45
14
|
constructor(network: Network) {
|
|
46
15
|
this.network = network
|
|
47
16
|
}
|
|
48
17
|
|
|
18
|
+
static tweakSigner(
|
|
19
|
+
privateKey: Buffer,
|
|
20
|
+
opts = {} as {
|
|
21
|
+
[key: string]: Buffer
|
|
22
|
+
},
|
|
23
|
+
network: Network,
|
|
24
|
+
) {
|
|
25
|
+
let newPrv = privateKey
|
|
26
|
+
let keyPair = ECPair.fromPrivateKey(privateKey, {
|
|
27
|
+
network: network,
|
|
28
|
+
compressed: true,
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
if (!keyPair.privateKey) throw new Error("private key not exist")
|
|
32
|
+
|
|
33
|
+
if (keyPair.publicKey.toString("hex").startsWith("03")) {
|
|
34
|
+
newPrv = ecc.privateNegate(keyPair.privateKey) as Buffer
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const tweakedPrivateKey = ecc.privateAdd(
|
|
38
|
+
newPrv,
|
|
39
|
+
tapTweakHash(Buffer.from(keyPair.publicKey.toString("hex").slice(2), "hex"), opts?.tweakHash),
|
|
40
|
+
)
|
|
41
|
+
if (!tweakedPrivateKey) {
|
|
42
|
+
throw new Error("Invalid tweaked private key!")
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
|
|
46
|
+
network: network,
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
49
50
|
async signPsbt(
|
|
50
51
|
unsignedPsbt: {
|
|
51
52
|
unsignedTransaction: string
|
|
52
53
|
},
|
|
53
54
|
privateKey: Buffer,
|
|
55
|
+
sighashTypes?: any[],
|
|
54
56
|
) {
|
|
55
57
|
const { network } = this
|
|
56
58
|
const keyPair = ECPair.fromPrivateKey(privateKey, {
|
|
@@ -66,10 +68,10 @@ class BitcoinLikeSignTransaction {
|
|
|
66
68
|
for (let i = 0; i < numberOfInputs; i += 1) {
|
|
67
69
|
let type = psbt.getInputType(i)
|
|
68
70
|
if (type === "nonstandard") {
|
|
69
|
-
let a = tweakSigner(privateKey, undefined, this.network)
|
|
70
|
-
await psbt.signInputAsync(i, a)
|
|
71
|
+
let a = BitcoinLikeSignTransaction.tweakSigner(privateKey, undefined, this.network)
|
|
72
|
+
await psbt.signInputAsync(i, a, sighashTypes)
|
|
71
73
|
} else {
|
|
72
|
-
await psbt.signInputAsync(i, keyPair)
|
|
74
|
+
await psbt.signInputAsync(i, keyPair, sighashTypes)
|
|
73
75
|
}
|
|
74
76
|
}
|
|
75
77
|
|
|
@@ -8,19 +8,39 @@ const coinselectSplit = require("coinselect/split")
|
|
|
8
8
|
const coinselectAccumulative = require("coinselect/accumulative")
|
|
9
9
|
|
|
10
10
|
// https://bitcoin.stackexchange.com/questions/84004/how-do-virtual-size-stripped-size-and-raw-size-compare-between-legacy-address-f
|
|
11
|
-
const componentBytes = {
|
|
11
|
+
// export const componentBytes = {
|
|
12
|
+
// bytePerInput: {
|
|
13
|
+
// p2pkh: 148,
|
|
14
|
+
// p2wpkh: 70, // 68
|
|
15
|
+
// "p2sh-p2wpkh": 91,
|
|
16
|
+
// p2tr: 60, // actual 58
|
|
17
|
+
// },
|
|
18
|
+
// baseTxBytes: 10 + 5, // +5 extra bytes to be sure
|
|
19
|
+
// bytePerOutput: {
|
|
20
|
+
// p2pkh: 35, // 34
|
|
21
|
+
// p2wpkh: 35, // 31
|
|
22
|
+
// p2sh: 35, // 32
|
|
23
|
+
// p2tr: 45, // 43
|
|
24
|
+
// default: 35,
|
|
25
|
+
// },
|
|
26
|
+
// opReturn: {
|
|
27
|
+
// dataLessThan75: 31,
|
|
28
|
+
// dataMoreThan75: 91,
|
|
29
|
+
// },
|
|
30
|
+
// }
|
|
31
|
+
export const componentBytes = {
|
|
12
32
|
bytePerInput: {
|
|
13
33
|
p2pkh: 148,
|
|
14
|
-
p2wpkh:
|
|
34
|
+
p2wpkh: 68, // 68
|
|
15
35
|
"p2sh-p2wpkh": 91,
|
|
16
|
-
p2tr:
|
|
36
|
+
p2tr: 58, // actual 58
|
|
17
37
|
},
|
|
18
|
-
baseTxBytes: 10 + 5, // +
|
|
38
|
+
baseTxBytes: 10 + 5, // +5 extra bytes to be sure
|
|
19
39
|
bytePerOutput: {
|
|
20
|
-
p2pkh:
|
|
21
|
-
p2wpkh:
|
|
22
|
-
p2sh:
|
|
23
|
-
p2tr:
|
|
40
|
+
p2pkh: 34, // 34
|
|
41
|
+
p2wpkh: 31, // 31
|
|
42
|
+
p2sh: 32, // 32
|
|
43
|
+
p2tr: 43, // 43
|
|
24
44
|
default: 35,
|
|
25
45
|
},
|
|
26
46
|
opReturn: {
|
|
@@ -97,7 +117,7 @@ export type ExtendedUnsignedTransaction = {
|
|
|
97
117
|
change: TargetAddress | undefined
|
|
98
118
|
}
|
|
99
119
|
|
|
100
|
-
class
|
|
120
|
+
export class BaseTransactionBuilder {
|
|
101
121
|
testnet: boolean
|
|
102
122
|
network: bitcoin.Network
|
|
103
123
|
maximumNumberOfOutputsInTransaction: number
|
|
@@ -177,6 +197,52 @@ class BaseBitcoinLikeTransaction {
|
|
|
177
197
|
return extendedUtxo
|
|
178
198
|
}
|
|
179
199
|
|
|
200
|
+
calculateTxSize(
|
|
201
|
+
inputTypes: string[],
|
|
202
|
+
outputs: {
|
|
203
|
+
script?: Buffer
|
|
204
|
+
address?: string
|
|
205
|
+
value: number
|
|
206
|
+
}[],
|
|
207
|
+
changeAddressType = "default",
|
|
208
|
+
) {
|
|
209
|
+
const inputsSizes = inputTypes.map(
|
|
210
|
+
(addressType) =>
|
|
211
|
+
componentBytes.bytePerInput[addressType as keyof typeof componentBytes.bytePerInput],
|
|
212
|
+
)
|
|
213
|
+
const outputSizes = outputs.map((outP: any) => {
|
|
214
|
+
if (outP.address) {
|
|
215
|
+
let addressType = "default"
|
|
216
|
+
try {
|
|
217
|
+
addressType = getAddressType(outP.address, this.network)
|
|
218
|
+
} catch {
|
|
219
|
+
addressType = "default"
|
|
220
|
+
}
|
|
221
|
+
return componentBytes.bytePerOutput[
|
|
222
|
+
addressType as keyof typeof componentBytes.bytePerOutput
|
|
223
|
+
]
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (outP.script) {
|
|
227
|
+
if (outP.script.byteLength < 75) {
|
|
228
|
+
return componentBytes.opReturn.dataLessThan75
|
|
229
|
+
}
|
|
230
|
+
return componentBytes.opReturn.dataMoreThan75
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return componentBytes.bytePerOutput[
|
|
234
|
+
changeAddressType as keyof typeof componentBytes.bytePerOutput
|
|
235
|
+
]
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
const txSize =
|
|
239
|
+
componentBytes.baseTxBytes +
|
|
240
|
+
inputsSizes.reduce((a, c) => a + c, 0) +
|
|
241
|
+
outputSizes.reduce((a, c) => a + c, 0)
|
|
242
|
+
|
|
243
|
+
return txSize
|
|
244
|
+
}
|
|
245
|
+
|
|
180
246
|
helperHandleInputsAndOutputs({
|
|
181
247
|
targets,
|
|
182
248
|
extendedUtxo,
|
|
@@ -247,53 +313,24 @@ class BaseBitcoinLikeTransaction {
|
|
|
247
313
|
fee = inputs.reduce((a, b) => a + b.value, 0) - outputs.reduce((a, b) => a + b.value, 0)
|
|
248
314
|
}
|
|
249
315
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const outputSizes = outputs.map((outP) => {
|
|
257
|
-
console.log("r", outP)
|
|
258
|
-
|
|
259
|
-
if (outP.address) {
|
|
260
|
-
let addressType = "default"
|
|
261
|
-
try {
|
|
262
|
-
addressType = getAddressType(outP.address, this.network)
|
|
263
|
-
} catch {
|
|
264
|
-
addressType = "default"
|
|
265
|
-
}
|
|
266
|
-
return componentBytes.bytePerOutput[
|
|
267
|
-
addressType as keyof typeof componentBytes.bytePerOutput
|
|
268
|
-
]
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (outP.script) {
|
|
272
|
-
if (outP.script.byteLength < 75) {
|
|
273
|
-
return componentBytes.opReturn.dataLessThan75
|
|
274
|
-
}
|
|
275
|
-
return componentBytes.opReturn.dataMoreThan75
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
let addressType = "default"
|
|
279
|
-
try {
|
|
280
|
-
console.log(changeObject?.address)
|
|
281
|
-
|
|
282
|
-
addressType = getAddressType(changeObject?.address || "", this.network)
|
|
283
|
-
} catch {
|
|
284
|
-
addressType = "default"
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return componentBytes.bytePerOutput[addressType as keyof typeof componentBytes.bytePerOutput]
|
|
288
|
-
})
|
|
316
|
+
let changeAddressType = "default"
|
|
317
|
+
try {
|
|
318
|
+
changeAddressType = getAddressType(changeObject?.address || "", this.network)
|
|
319
|
+
} catch {
|
|
320
|
+
changeAddressType = "default"
|
|
321
|
+
}
|
|
289
322
|
|
|
290
323
|
const txSize =
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
324
|
+
this.calculateTxSize(
|
|
325
|
+
inputs.map((i) => i.signerInfo.addressType),
|
|
326
|
+
outputs,
|
|
327
|
+
changeAddressType,
|
|
328
|
+
) + componentBytes.bytePerOutput.default
|
|
329
|
+
|
|
330
|
+
let txFee = Math.round(txSize * feeRate)
|
|
331
|
+
if (Math.round(feeRate) === 1) {
|
|
332
|
+
txFee = Math.round(txFee + txFee * 0.1)
|
|
333
|
+
}
|
|
297
334
|
if (
|
|
298
335
|
inputs.reduce((a, b) => a + b.value, 0) -
|
|
299
336
|
outputs.filter((o) => o.address || o.script).reduce((a, b) => a + b.value, 0) -
|
|
@@ -500,6 +537,7 @@ class BaseBitcoinLikeTransaction {
|
|
|
500
537
|
fee: number
|
|
501
538
|
feeRate: number
|
|
502
539
|
}) {
|
|
540
|
+
const sequence = 0xffffffff - 2
|
|
503
541
|
const { network } = this
|
|
504
542
|
const newPsbt = new bitcoin.Psbt({ network })
|
|
505
543
|
newPsbt.setMaximumFeeRate(+(feeRate + feeRate / 100).toFixed())
|
|
@@ -512,7 +550,7 @@ class BaseBitcoinLikeTransaction {
|
|
|
512
550
|
hash: input.hash,
|
|
513
551
|
index: Number(input.index),
|
|
514
552
|
nonWitnessUtxo: input.nonWitnessUtxo,
|
|
515
|
-
sequence
|
|
553
|
+
sequence,
|
|
516
554
|
bip32Derivation: input.bip32Derivation,
|
|
517
555
|
}
|
|
518
556
|
if (!i.bip32Derivation) delete i.bip32Derivation
|
|
@@ -526,7 +564,7 @@ class BaseBitcoinLikeTransaction {
|
|
|
526
564
|
witnessUtxo: input.witnessUtxo,
|
|
527
565
|
// we dont need nonWitnessUtxo. bud some application force nonWitnessUtxo
|
|
528
566
|
nonWitnessUtxo: input.nonWitnessUtxo,
|
|
529
|
-
sequence
|
|
567
|
+
sequence,
|
|
530
568
|
bip32Derivation: input.bip32Derivation,
|
|
531
569
|
}
|
|
532
570
|
if (!i.bip32Derivation) delete i.bip32Derivation
|
|
@@ -542,7 +580,7 @@ class BaseBitcoinLikeTransaction {
|
|
|
542
580
|
// we dont need nonWitnessUtxo. bud some application force nonWitnessUtxo
|
|
543
581
|
nonWitnessUtxo: input.nonWitnessUtxo,
|
|
544
582
|
redeemScript: input.redeemScript,
|
|
545
|
-
sequence
|
|
583
|
+
sequence,
|
|
546
584
|
bip32Derivation: input.bip32Derivation,
|
|
547
585
|
}
|
|
548
586
|
if (!i.bip32Derivation) delete i.bip32Derivation
|
|
@@ -558,7 +596,7 @@ class BaseBitcoinLikeTransaction {
|
|
|
558
596
|
// we dont need nonWitnessUtxo. bud some application force nonWitnessUtxo
|
|
559
597
|
nonWitnessUtxo: input.nonWitnessUtxo,
|
|
560
598
|
tapInternalKey: input.tapInternalKey,
|
|
561
|
-
sequence
|
|
599
|
+
sequence,
|
|
562
600
|
bip32Derivation: input.bip32Derivation,
|
|
563
601
|
}
|
|
564
602
|
if (!i.bip32Derivation) delete i.bip32Derivation
|
|
@@ -612,7 +650,6 @@ class BaseBitcoinLikeTransaction {
|
|
|
612
650
|
changeAddress = undefined,
|
|
613
651
|
fullAmount = false,
|
|
614
652
|
feeRate,
|
|
615
|
-
selfTransaction = false,
|
|
616
653
|
selectType = "normal",
|
|
617
654
|
}: {
|
|
618
655
|
extendedUtxo: ExtendedUtxo[]
|
|
@@ -621,10 +658,9 @@ class BaseBitcoinLikeTransaction {
|
|
|
621
658
|
|
|
622
659
|
changeAddress?: string | SignerInfo
|
|
623
660
|
fullAmount?: boolean
|
|
624
|
-
selfTransaction?: boolean
|
|
625
661
|
selectType?: "normal" | "accumulative" | "full"
|
|
626
662
|
}) {
|
|
627
|
-
if (!
|
|
663
|
+
if (!changeAddress && targets.length === 0) throw new Error("no target")
|
|
628
664
|
let changeObject =
|
|
629
665
|
typeof changeAddress === "string"
|
|
630
666
|
? {
|
|
@@ -649,5 +685,3 @@ class BaseBitcoinLikeTransaction {
|
|
|
649
685
|
return unsignedTransaction
|
|
650
686
|
}
|
|
651
687
|
}
|
|
652
|
-
|
|
653
|
-
export default BaseBitcoinLikeTransaction
|
package/src/utils/tools.ts
CHANGED
package/.tmp/check.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { BitcoinBase, TeleportDaoPayment, TransferRequest } from "@teleportdao/bitcoin"
|
|
2
|
-
import { parseRawTransaction } from "../dist/bitcoin-utils"
|
|
3
|
-
;(async () => {
|
|
4
|
-
const testnet = true
|
|
5
|
-
let address = "tb1psfytxls4urhycq63xjrfmseq73wwuql2syn3n8398rqkyf5m3z8shy5sl7"
|
|
6
|
-
let btc = new TeleportDaoPayment("bitcoin_testnet")
|
|
7
|
-
let deadline = Math.ceil(new Date().getTime() / 1000 + 3600)
|
|
8
|
-
let transferRequest: TransferRequest = {
|
|
9
|
-
changeAddress: address,
|
|
10
|
-
lockerAddress: testnet
|
|
11
|
-
? "2MzQA2boKkWPkooDkN9dKfqzFndSvjHw5kg"
|
|
12
|
-
: "3CAQAw7m95axbY761Xq8d9DADhjNaX9b8o",
|
|
13
|
-
amount: +(0.0001 * 1e8).toFixed(),
|
|
14
|
-
recipientAddress: "0x7C124a845BC48D957748661452dc996FF487B252",
|
|
15
|
-
percentageFee: 4, // 2 bytes in satoshi
|
|
16
|
-
|
|
17
|
-
chainId: 3,
|
|
18
|
-
appId: 0,
|
|
19
|
-
speed: 0, // 1 byte
|
|
20
|
-
|
|
21
|
-
// exchange
|
|
22
|
-
isExchange: false,
|
|
23
|
-
exchangeTokenAddress: undefined,
|
|
24
|
-
outputAmount: undefined,
|
|
25
|
-
isFixedToken: false,
|
|
26
|
-
deadline,
|
|
27
|
-
feeSpeed: "normal",
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
let extendedUtxo = await btc.transactionBuilder.getExtendedUtxo({
|
|
31
|
-
address: address,
|
|
32
|
-
addressType: "p2tr",
|
|
33
|
-
publicKey: "021f6adece8789d520aaea90a2ca9faff45bdd2dca02fd819815673953dc674d54",
|
|
34
|
-
includeHex: false,
|
|
35
|
-
})
|
|
36
|
-
console.log(extendedUtxo.reduce((a, b) => a + b.value, 0) / 1e8)
|
|
37
|
-
extendedUtxo = extendedUtxo.filter((u) => u.value > 145 * 68)
|
|
38
|
-
console.log(extendedUtxo.reduce((a, b) => a + b.value, 0) / 1e8)
|
|
39
|
-
console.log(extendedUtxo.length)
|
|
40
|
-
|
|
41
|
-
// const extendedUtxo = [
|
|
42
|
-
// {
|
|
43
|
-
// hash: "1a69625b1fe4392e4130f30fa1e6794dd8b81f54bc9d7ccf7a3479d8ccf419a6",
|
|
44
|
-
// value: 99787,
|
|
45
|
-
// index: 1,
|
|
46
|
-
// signerInfo: {
|
|
47
|
-
// address: "tb1psfytxls4urhycq63xjrfmseq73wwuql2syn3n8398rqkyf5m3z8shy5sl7",
|
|
48
|
-
// publicKey: "021f6adece8789d520aaea90a2ca9faff45bdd2dca02fd819815673953dc674d54",
|
|
49
|
-
// addressType: "p2tr",
|
|
50
|
-
// },
|
|
51
|
-
// },
|
|
52
|
-
// {
|
|
53
|
-
// hash: "93f0b41f26480068a219a8620313f6fa146f076e4ef03ab781638965a2a1c7c4",
|
|
54
|
-
// value: 8000,
|
|
55
|
-
// index: 1,
|
|
56
|
-
// signerInfo: {
|
|
57
|
-
// address: "tb1psfytxls4urhycq63xjrfmseq73wwuql2syn3n8398rqkyf5m3z8shy5sl7",
|
|
58
|
-
// publicKey: "021f6adece8789d520aaea90a2ca9faff45bdd2dca02fd819815673953dc674d54",
|
|
59
|
-
// addressType: "p2tr",
|
|
60
|
-
// },
|
|
61
|
-
// },
|
|
62
|
-
// {
|
|
63
|
-
// hash: "6c666a7036fdff9f4c1cce2f9233eac3c4b56601cdc5ea82554b8af90e962ced",
|
|
64
|
-
// value: 8000,
|
|
65
|
-
// index: 1,
|
|
66
|
-
// signerInfo: {
|
|
67
|
-
// address: "tb1psfytxls4urhycq63xjrfmseq73wwuql2syn3n8398rqkyf5m3z8shy5sl7",
|
|
68
|
-
// publicKey: "021f6adece8789d520aaea90a2ca9faff45bdd2dca02fd819815673953dc674d54",
|
|
69
|
-
// addressType: "p2tr",
|
|
70
|
-
// },
|
|
71
|
-
// },
|
|
72
|
-
// {
|
|
73
|
-
// hash: "2437ddbde4bb469c5c1f8dcb09959962a9e844ba9b56420bb5391fb29f50400c",
|
|
74
|
-
// value: 8000,
|
|
75
|
-
// index: 0,
|
|
76
|
-
// signerInfo: {
|
|
77
|
-
// address: "tb1psfytxls4urhycq63xjrfmseq73wwuql2syn3n8398rqkyf5m3z8shy5sl7",
|
|
78
|
-
// publicKey: "021f6adece8789d520aaea90a2ca9faff45bdd2dca02fd819815673953dc674d54",
|
|
79
|
-
// addressType: "p2tr",
|
|
80
|
-
// },
|
|
81
|
-
// },
|
|
82
|
-
// ]
|
|
83
|
-
|
|
84
|
-
// normal transfer
|
|
85
|
-
// let tx = await btc.transactionBuilder.processUnsignedTransaction({
|
|
86
|
-
// extendedUtxo,
|
|
87
|
-
// targets: [{
|
|
88
|
-
// address: addressObj.address!,
|
|
89
|
-
// value: 0.0001 * 1e8
|
|
90
|
-
// }],
|
|
91
|
-
// feeRate:1,
|
|
92
|
-
// changeAddress: addressObj.address!
|
|
93
|
-
// })
|
|
94
|
-
|
|
95
|
-
let tx = await btc.getBitcoinToEthUnsignedPsbt({
|
|
96
|
-
...transferRequest,
|
|
97
|
-
extendedUtxo,
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
console.log("tx", tx)
|
|
101
|
-
})()
|
package/.tmp/rbf.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { BitcoinBase, TeleportDaoPayment, TransferRequest } from "@teleportdao/bitcoin"
|
|
2
|
-
import { parseRawTransaction } from "../dist/bitcoin-utils"
|
|
3
|
-
require("dotenv").config()
|
|
4
|
-
;(async () => {
|
|
5
|
-
let btc = new TeleportDaoPayment("bitcoin_testnet")
|
|
6
|
-
btc.setAccountPrivateKeyByMnemonic({
|
|
7
|
-
mnemonic: process.env.ACCOUNT__MNEMONIC!,
|
|
8
|
-
index: 2,
|
|
9
|
-
addressType: "p2wpkh",
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
console.log(btc.bitcoinAddress)
|
|
13
|
-
let balance = await btc.btcInterface.getBalance(btc.bitcoinAddress!)
|
|
14
|
-
|
|
15
|
-
console.log(balance)
|
|
16
|
-
|
|
17
|
-
let txId = await btc.send({
|
|
18
|
-
receiverAddress: btc.bitcoinAddress!,
|
|
19
|
-
fullAmount: true,
|
|
20
|
-
amount: 0,
|
|
21
|
-
})
|
|
22
|
-
})()
|