@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.
@@ -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 = WitnessType | 'p2pkh' | 'p2sh' | 'opreturn';
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.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.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
  }
@@ -1,8 +1,9 @@
1
- import { Buff } from '@vbyte/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 : string) : boolean {
21
- return script.startsWith('6a')
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 get_vout_info (txout : TxOutput) : TxOutputInfo {
26
+ export function get_vout_script_info (script : Bytes) : TxOutputInfo {
25
27
  return {
26
- type : get_vout_type(txout.script_pk),
27
- version : get_vout_version(txout.script_pk)
28
+ type : get_vout_script_type(script),
29
+ version : get_vout_script_version(script)
28
30
  }
29
31
  }
30
32
 
31
- export function get_vout_type (
32
- script : string
33
- ) : TxOutputType {
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
- if (regex.test(script)) return type as TxOutputType
38
+ // If the script matches the regex, return the type.
39
+ if (regex.test(hex)) return type as TxOutputType
36
40
  }
37
- return 'unknown'
41
+ // If the script does not match any regex, return null.
42
+ return null
38
43
  }
39
44
 
40
- export function get_vout_version (
41
- script : string
42
- ) : WitnessVersion {
43
- const wit_ver = script.slice(0, 4)
44
- switch (wit_ver) {
45
- case '0014' : return 0
46
- case '5120' : return 1
47
- default : return null
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
- txdata : TxData
53
- ) : string {
54
- assert_tx_template(txdata)
55
- const data = encode_tx(txdata, false)
56
- 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
57
66
  }
58
67
 
59
- export function get_txhash (
60
- txdata : TxData
61
- ) : string {
62
- assert_tx_template(txdata)
63
- const data = encode_tx(txdata, true)
64
- 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
65
78
  }
66
79
 
67
- export function get_tx_value (
68
- txdata : TxData
69
- ) : TxValue {
70
- const vin = txdata.vin.reduce((acc, txin) => acc + (txin.prevout?.value ?? 0n), 0n)
71
- 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)
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 = WitnessType | 'p2pkh' | 'p2sh' | 'opreturn'
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 {