@vbyte/btc-dev 1.1.2 → 1.1.3
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/lib/signer/sign.js +2 -2
- package/dist/lib/tx/util.d.ts +3 -3
- package/dist/lib/tx/util.js +15 -8
- package/dist/main.cjs +4125 -4309
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +4125 -4309
- package/dist/module.mjs.map +1 -1
- package/dist/package.json +2 -2
- package/dist/schema/taproot.d.ts +5 -15
- package/dist/schema/tx.d.ts +44 -262
- package/dist/script.js +8 -9
- package/dist/script.js.map +1 -1
- package/package.json +2 -2
- package/src/lib/signer/sign.ts +2 -2
- package/src/lib/tx/util.ts +38 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vbyte/btc-dev",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "Batteries-included toolset for plebian bitcoin development",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"test": "npm run script test/src/tape.ts"
|
|
80
80
|
},
|
|
81
81
|
"dependencies": {
|
|
82
|
-
"@noble/curves": "^1.9.
|
|
82
|
+
"@noble/curves": "^1.9.3",
|
|
83
83
|
"@noble/hashes": "^1.8.0",
|
|
84
84
|
"@scure/btc-signer": "^1.8.1",
|
|
85
85
|
"@vbyte/buff": "^1.0.2",
|
package/src/lib/signer/sign.ts
CHANGED
|
@@ -17,7 +17,7 @@ export function sign_segwit_tx (
|
|
|
17
17
|
) {
|
|
18
18
|
const tx = parse_tx(txdata)
|
|
19
19
|
const msg = hash_segwit_tx(tx, options)
|
|
20
|
-
const sig = ECC.
|
|
20
|
+
const sig = ECC.sign_ecdsa(seckey, msg).hex
|
|
21
21
|
const flag = format_sigflag(options.sigflag ?? SIGHASH_DEFAULT)
|
|
22
22
|
return sig + flag
|
|
23
23
|
}
|
|
@@ -29,7 +29,7 @@ export function sign_taproot_tx (
|
|
|
29
29
|
) {
|
|
30
30
|
const tx = parse_tx(txdata)
|
|
31
31
|
const msg = hash_taproot_tx(tx, options)
|
|
32
|
-
const sig = ECC.
|
|
32
|
+
const sig = ECC.sign_bip340(seckey, msg).hex
|
|
33
33
|
const flag = format_sigflag(options.sigflag ?? 0)
|
|
34
34
|
return sig + flag
|
|
35
35
|
}
|
package/src/lib/tx/util.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Test } from '@vbyte/micro-lib'
|
|
|
3
3
|
import { Assert } from '@vbyte/micro-lib/assert'
|
|
4
4
|
import { hash256 } from '@vbyte/micro-lib/hash'
|
|
5
5
|
import { encode_tx } from './encode.js'
|
|
6
|
+
import { parse_tx } from './parse.js'
|
|
6
7
|
import { assert_tx_template } from './validate.js'
|
|
7
8
|
|
|
8
9
|
import { DEFAULT, LOCK_SCRIPT_REGEX } from '@/const.js'
|
|
@@ -29,20 +30,22 @@ export function get_vout_script_info (script : Bytes) : TxOutputInfo {
|
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
export function get_vout_script_type (
|
|
33
|
-
|
|
34
|
-
) : TxOutputType | null {
|
|
33
|
+
export function get_vout_script_type (script : Bytes) : TxOutputType | null {
|
|
34
|
+
// Get the hex string of the script.
|
|
35
35
|
const hex = Buff.bytes(script).hex
|
|
36
|
+
// Iterate over the lock script regexes.
|
|
36
37
|
for (const [ type, regex ] of Object.entries(LOCK_SCRIPT_REGEX)) {
|
|
38
|
+
// If the script matches the regex, return the type.
|
|
37
39
|
if (regex.test(hex)) return type as TxOutputType
|
|
38
40
|
}
|
|
41
|
+
// If the script does not match any regex, return null.
|
|
39
42
|
return null
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
export function get_vout_script_version (
|
|
43
|
-
|
|
44
|
-
) : WitnessVersion | null{
|
|
45
|
+
export function get_vout_script_version (script : Bytes) : WitnessVersion | null {
|
|
46
|
+
// Get the version of the script.
|
|
45
47
|
const version = Buff.bytes(script)
|
|
48
|
+
// Return the version of the script.
|
|
46
49
|
switch (version.at(0)) {
|
|
47
50
|
case 0x00 : return 0
|
|
48
51
|
case 0x51 : return 1
|
|
@@ -50,28 +53,40 @@ export function get_vout_script_version (
|
|
|
50
53
|
}
|
|
51
54
|
}
|
|
52
55
|
|
|
53
|
-
export function get_txid (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
export function get_txid (txdata : string | Uint8Array | TxData) : string {
|
|
57
|
+
// If the transaction data is an object,
|
|
58
|
+
if (typeof txdata === 'object') {
|
|
59
|
+
// Assert the structure of the transaction data is valid.
|
|
60
|
+
assert_tx_template(txdata)
|
|
61
|
+
// Encode the transaction data.
|
|
62
|
+
txdata = encode_tx(txdata, false)
|
|
63
|
+
}
|
|
64
|
+
// Return the txid of the transaction data.
|
|
65
|
+
return hash256(txdata).reverse().hex
|
|
59
66
|
}
|
|
60
67
|
|
|
61
|
-
export function get_txhash (
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
68
|
+
export function get_txhash (txdata : string | Uint8Array | TxData) : string {
|
|
69
|
+
// If the transaction data is an object,
|
|
70
|
+
if (typeof txdata === 'object') {
|
|
71
|
+
// Assert the structure of the transaction data is valid.
|
|
72
|
+
assert_tx_template(txdata)
|
|
73
|
+
// Encode the transaction data.
|
|
74
|
+
txdata = encode_tx(txdata, true)
|
|
75
|
+
}
|
|
76
|
+
// Return the txhash of the transaction data.
|
|
77
|
+
return hash256(txdata).reverse().hex
|
|
67
78
|
}
|
|
68
79
|
|
|
69
|
-
export function get_tx_value (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
80
|
+
export function get_tx_value (txdata : string | Uint8Array | TxData) : TxValue {
|
|
81
|
+
// Parse the transaction data.
|
|
82
|
+
const tx = parse_tx(txdata)
|
|
83
|
+
// Assert the structure of the transaction data is valid.
|
|
84
|
+
assert_tx_template(tx)
|
|
85
|
+
// Calculate the value of the transaction.
|
|
86
|
+
const vin = tx.vin.reduce((acc, txin) => acc + (txin.prevout?.value ?? 0n), 0n)
|
|
87
|
+
const vout = tx.vout.reduce((acc, txout) => acc + txout.value, 0n)
|
|
74
88
|
const fees = (vin > vout) ? (vin - vout) : 0n
|
|
89
|
+
// Return the value of the transaction.
|
|
75
90
|
return { fees, vin, vout }
|
|
76
91
|
}
|
|
77
92
|
|