@vbyte/btc-dev 1.1.1 → 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 +8 -7
- package/dist/lib/tx/util.js +29 -20
- package/dist/main.cjs +4129 -4311
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +4129 -4311
- 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/dist/types/meta.d.ts +3 -3
- package/package.json +2 -2
- package/src/lib/signer/sign.ts +2 -2
- package/src/lib/tx/util.ts +53 -36
- package/src/types/meta.ts +3 -3
package/dist/types/meta.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ import type { WitnessType, WitnessVersion } from './witness.js';
|
|
|
2
2
|
export type LocktimeData = LocktimeStamp | LocktimeHeight;
|
|
3
3
|
export type SequenceConfig = Partial<SequenceData>;
|
|
4
4
|
export type SequenceData = SequenceHeightLock | SequenceStampLock;
|
|
5
|
-
export type TxOutputType =
|
|
5
|
+
export type TxOutputType = 'p2pkh' | 'p2sh' | 'p2w-pkh' | 'p2w-sh' | 'p2tr' | 'opreturn';
|
|
6
6
|
export interface TxOutputInfo {
|
|
7
|
-
type: TxOutputType;
|
|
8
|
-
version: WitnessVersion;
|
|
7
|
+
type: TxOutputType | null;
|
|
8
|
+
version: WitnessVersion | null;
|
|
9
9
|
}
|
|
10
10
|
export interface TxInputInfo {
|
|
11
11
|
type: WitnessType;
|
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
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { Buff }
|
|
1
|
+
import { Buff, Bytes } from '@vbyte/buff'
|
|
2
2
|
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'
|
|
@@ -17,59 +18,75 @@ import type {
|
|
|
17
18
|
WitnessVersion
|
|
18
19
|
} from '@/types/index.js'
|
|
19
20
|
|
|
20
|
-
export function is_return_script (script :
|
|
21
|
-
|
|
21
|
+
export function is_return_script (script : Bytes) : boolean {
|
|
22
|
+
const bytes = Buff.bytes(script)
|
|
23
|
+
return bytes.at(0) === 0x6a
|
|
22
24
|
}
|
|
23
25
|
|
|
24
|
-
export function
|
|
26
|
+
export function get_vout_script_info (script : Bytes) : TxOutputInfo {
|
|
25
27
|
return {
|
|
26
|
-
type :
|
|
27
|
-
version :
|
|
28
|
+
type : get_vout_script_type(script),
|
|
29
|
+
version : get_vout_script_version(script)
|
|
28
30
|
}
|
|
29
31
|
}
|
|
30
32
|
|
|
31
|
-
export function
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
export function get_vout_script_type (script : Bytes) : TxOutputType | null {
|
|
34
|
+
// Get the hex string of the script.
|
|
35
|
+
const hex = Buff.bytes(script).hex
|
|
36
|
+
// Iterate over the lock script regexes.
|
|
34
37
|
for (const [ type, regex ] of Object.entries(LOCK_SCRIPT_REGEX)) {
|
|
35
|
-
|
|
38
|
+
// If the script matches the regex, return the type.
|
|
39
|
+
if (regex.test(hex)) return type as TxOutputType
|
|
36
40
|
}
|
|
37
|
-
return
|
|
41
|
+
// If the script does not match any regex, return null.
|
|
42
|
+
return null
|
|
38
43
|
}
|
|
39
44
|
|
|
40
|
-
export function
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
switch (
|
|
45
|
-
case
|
|
46
|
-
case
|
|
47
|
-
default
|
|
45
|
+
export function get_vout_script_version (script : Bytes) : WitnessVersion | null {
|
|
46
|
+
// Get the version of the script.
|
|
47
|
+
const version = Buff.bytes(script)
|
|
48
|
+
// Return the version of the script.
|
|
49
|
+
switch (version.at(0)) {
|
|
50
|
+
case 0x00 : return 0
|
|
51
|
+
case 0x51 : return 1
|
|
52
|
+
default : return null
|
|
48
53
|
}
|
|
49
54
|
}
|
|
50
55
|
|
|
51
|
-
export function get_txid (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
|
57
66
|
}
|
|
58
67
|
|
|
59
|
-
export function get_txhash (
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
|
65
78
|
}
|
|
66
79
|
|
|
67
|
-
export function get_tx_value (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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)
|
|
72
88
|
const fees = (vin > vout) ? (vin - vout) : 0n
|
|
89
|
+
// Return the value of the transaction.
|
|
73
90
|
return { fees, vin, vout }
|
|
74
91
|
}
|
|
75
92
|
|
package/src/types/meta.ts
CHANGED
|
@@ -3,11 +3,11 @@ import type{ WitnessType, WitnessVersion } from './witness.js'
|
|
|
3
3
|
export type LocktimeData = LocktimeStamp | LocktimeHeight
|
|
4
4
|
export type SequenceConfig = Partial<SequenceData>
|
|
5
5
|
export type SequenceData = SequenceHeightLock | SequenceStampLock
|
|
6
|
-
export type TxOutputType =
|
|
6
|
+
export type TxOutputType = 'p2pkh' | 'p2sh' | 'p2w-pkh' | 'p2w-sh' | 'p2tr' | 'opreturn'
|
|
7
7
|
|
|
8
8
|
export interface TxOutputInfo {
|
|
9
|
-
type : TxOutputType
|
|
10
|
-
version : WitnessVersion
|
|
9
|
+
type : TxOutputType | null
|
|
10
|
+
version : WitnessVersion | null
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export interface TxInputInfo {
|