@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vbyte/btc-dev",
3
- "version": "1.1.2",
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.2",
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",
@@ -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.get_ecdsa_sig(seckey, msg).hex
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.get_bip340_sig(seckey, msg).hex
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
  }
@@ -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
- script : Bytes
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
- script : Bytes
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
- txdata : TxData
55
- ) : string {
56
- assert_tx_template(txdata)
57
- const data = encode_tx(txdata, false)
58
- return hash256(data).reverse().hex
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
- txdata : TxData
63
- ) : string {
64
- assert_tx_template(txdata)
65
- const data = encode_tx(txdata, true)
66
- return hash256(data).reverse().hex
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
- txdata : TxData
71
- ) : TxValue {
72
- const vin = txdata.vin.reduce((acc, txin) => acc + (txin.prevout?.value ?? 0n), 0n)
73
- const vout = txdata.vout.reduce((acc, txout) => acc + txout.value, 0n)
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