@vbyte/btc-dev 1.0.1 → 1.0.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.
Files changed (100) hide show
  1. package/dist/class/signer.js +1 -1
  2. package/dist/class/tx.js +5 -4
  3. package/dist/class/txin.js +3 -2
  4. package/dist/class/witness.js +1 -1
  5. package/dist/index.d.ts +8 -6
  6. package/dist/index.js +8 -6
  7. package/dist/lib/meta/index.d.ts +3 -1
  8. package/dist/lib/meta/index.js +3 -1
  9. package/dist/lib/{tx → meta}/locktime.d.ts +1 -1
  10. package/dist/lib/{tx → meta}/locktime.js +5 -5
  11. package/dist/lib/meta/{pointer.d.ts → ref.d.ts} +1 -1
  12. package/dist/lib/meta/{pointer.js → ref.js} +6 -6
  13. package/dist/lib/meta/scribe.d.ts +1 -1
  14. package/dist/lib/meta/scribe.js +5 -5
  15. package/dist/lib/{tx → meta}/sequence.d.ts +1 -1
  16. package/dist/lib/{tx → meta}/sequence.js +5 -5
  17. package/dist/lib/sighash/index.d.ts +0 -2
  18. package/dist/lib/sighash/index.js +0 -2
  19. package/dist/lib/sighash/segwit.js +2 -2
  20. package/dist/lib/sighash/taproot.js +2 -2
  21. package/dist/lib/signer/index.d.ts +2 -0
  22. package/dist/lib/signer/index.js +2 -0
  23. package/dist/lib/{sighash → signer}/sign.js +6 -6
  24. package/dist/lib/signer/verify.d.ts +4 -0
  25. package/dist/lib/signer/verify.js +8 -0
  26. package/dist/lib/taproot/parse.js +1 -1
  27. package/dist/lib/tx/create.d.ts +4 -4
  28. package/dist/lib/tx/create.js +1 -1
  29. package/dist/lib/tx/decode.d.ts +1 -1
  30. package/dist/lib/tx/decode.js +1 -1
  31. package/dist/lib/tx/encode.d.ts +1 -1
  32. package/dist/lib/tx/encode.js +3 -3
  33. package/dist/lib/tx/index.d.ts +1 -4
  34. package/dist/lib/tx/index.js +1 -4
  35. package/dist/lib/tx/parse.d.ts +1 -1
  36. package/dist/lib/tx/parse.js +5 -5
  37. package/dist/lib/tx/size.d.ts +1 -2
  38. package/dist/lib/tx/size.js +5 -12
  39. package/dist/lib/tx/{meta.js → util.js} +7 -7
  40. package/dist/lib/tx/validate.d.ts +2 -2
  41. package/dist/lib/witness/index.d.ts +2 -0
  42. package/dist/lib/witness/index.js +2 -0
  43. package/dist/lib/witness/util.d.ts +3 -0
  44. package/dist/lib/witness/util.js +8 -0
  45. package/dist/main.cjs +1002 -980
  46. package/dist/main.cjs.map +1 -1
  47. package/dist/module.mjs +993 -973
  48. package/dist/module.mjs.map +1 -1
  49. package/dist/package.json +11 -7
  50. package/dist/script.js +1 -1
  51. package/dist/script.js.map +1 -1
  52. package/dist/types/address.d.ts +1 -1
  53. package/dist/types/index.d.ts +0 -1
  54. package/dist/types/index.js +0 -1
  55. package/dist/types/meta.d.ts +19 -0
  56. package/dist/types/transaction.d.ts +1 -1
  57. package/dist/types/txdata.d.ts +4 -4
  58. package/package.json +11 -7
  59. package/src/class/signer.ts +1 -1
  60. package/src/class/tx.ts +6 -6
  61. package/src/class/txin.ts +2 -2
  62. package/src/class/witness.ts +1 -1
  63. package/src/index.ts +8 -6
  64. package/src/lib/meta/index.ts +3 -1
  65. package/src/lib/{tx → meta}/locktime.ts +1 -1
  66. package/src/lib/meta/{pointer.ts → ref.ts} +1 -1
  67. package/src/lib/meta/scribe.ts +1 -1
  68. package/src/lib/{tx → meta}/sequence.ts +1 -1
  69. package/src/lib/sighash/index.ts +0 -2
  70. package/src/lib/sighash/segwit.ts +2 -2
  71. package/src/lib/sighash/taproot.ts +2 -2
  72. package/src/lib/signer/index.ts +2 -0
  73. package/src/lib/{sighash → signer}/sign.ts +6 -6
  74. package/src/lib/{sighash → signer}/verify.ts +22 -0
  75. package/src/lib/taproot/parse.ts +1 -1
  76. package/src/lib/tx/create.ts +5 -5
  77. package/src/lib/tx/decode.ts +7 -7
  78. package/src/lib/tx/encode.ts +3 -3
  79. package/src/lib/tx/index.ts +1 -4
  80. package/src/lib/tx/parse.ts +5 -5
  81. package/src/lib/tx/size.ts +7 -16
  82. package/src/lib/tx/{meta.ts → util.ts} +7 -7
  83. package/src/lib/tx/validate.ts +2 -2
  84. package/src/lib/witness/index.ts +2 -0
  85. package/src/lib/witness/util.ts +12 -0
  86. package/src/types/address.ts +1 -1
  87. package/src/types/index.ts +0 -1
  88. package/src/types/meta.ts +26 -0
  89. package/src/types/transaction.ts +2 -2
  90. package/src/types/txdata.ts +4 -4
  91. package/dist/lib/sighash/verify.d.ts +0 -1
  92. package/dist/lib/sighash/verify.js +0 -1
  93. package/dist/types/txmeta.d.ts +0 -19
  94. package/dist/types/txmeta.js +0 -1
  95. package/src/types/txmeta.ts +0 -25
  96. /package/dist/lib/{sighash → signer}/sign.d.ts +0 -0
  97. /package/dist/lib/tx/{meta.d.ts → util.d.ts} +0 -0
  98. /package/dist/lib/{tx/witness.d.ts → witness/parse.d.ts} +0 -0
  99. /package/dist/lib/{tx/witness.js → witness/parse.js} +0 -0
  100. /package/src/lib/{tx/witness.ts → witness/parse.ts} +0 -0
@@ -1,6 +1,6 @@
1
1
  export type AddressFormat = 'base58' | 'bech32' | 'bech32m';
2
2
  export type AddressType = 'p2pkh' | 'p2sh' | 'p2w-pkh' | 'p2w-sh' | 'p2tr';
3
- export type ChainNetwork = 'main' | 'testnet' | 'regtest';
3
+ export type ChainNetwork = 'main' | 'testnet' | 'regtest' | string;
4
4
  export type AddressData = AddressContext & ScriptData;
5
5
  export type AddressConfigEntry = [
6
6
  prefix: string,
@@ -5,5 +5,4 @@ export * from './sighash.js';
5
5
  export * from './taproot.js';
6
6
  export * from './transaction.js';
7
7
  export * from './txdata.js';
8
- export * from './txmeta.js';
9
8
  export * from './witness.js';
@@ -5,5 +5,4 @@ export * from './sighash.js';
5
5
  export * from './taproot.js';
6
6
  export * from './transaction.js';
7
7
  export * from './txdata.js';
8
- export * from './txmeta.js';
9
8
  export * from './witness.js';
@@ -1,3 +1,22 @@
1
+ export type LocktimeInfo = LocktimeStamp | LocktimeHeight;
2
+ export type SequenceConfig = Partial<SequenceInfo>;
3
+ export type SequenceInfo = SequenceHeightLock | SequenceStampLock;
4
+ export interface LocktimeStamp {
5
+ type: 'timelock';
6
+ stamp: number;
7
+ }
8
+ export interface LocktimeHeight {
9
+ type: 'heightlock';
10
+ height: number;
11
+ }
12
+ export interface SequenceStampLock {
13
+ stamp: number;
14
+ mode: 'stamp';
15
+ }
16
+ export interface SequenceHeightLock {
17
+ height: number;
18
+ mode: 'height';
19
+ }
1
20
  export interface InscriptionData {
2
21
  content?: string;
3
22
  delegate?: string;
@@ -1,5 +1,5 @@
1
+ import { LocktimeInfo, SequenceInfo } from './meta.js';
1
2
  import { TxOutput } from './txdata.js';
2
- import { LocktimeInfo, SequenceInfo } from './txmeta.js';
3
3
  import type { WitnessType, WitnessVersion } from './witness.js';
4
4
  export type TxOutputType = WitnessType | 'p2pkh' | 'p2sh' | 'opreturn';
5
5
  export interface TxOutputInfo {
@@ -1,4 +1,4 @@
1
- export type TxInput = CoinbaseInput | SpendInput | VirtualInput;
1
+ export type TxInput = TxCoinbaseInput | TxSpendInput | TxVirtualInput;
2
2
  export interface TxOutpoint {
3
3
  txid: string;
4
4
  vout: number;
@@ -10,21 +10,21 @@ export interface TxInputTemplate extends TxOutpoint {
10
10
  sequence?: number;
11
11
  witness?: string[];
12
12
  }
13
- export interface CoinbaseInput extends TxOutpoint {
13
+ export interface TxCoinbaseInput extends TxOutpoint {
14
14
  coinbase: string;
15
15
  prevout: null;
16
16
  script_sig: null;
17
17
  sequence: number;
18
18
  witness: string[];
19
19
  }
20
- export interface VirtualInput extends TxOutpoint {
20
+ export interface TxVirtualInput extends TxOutpoint {
21
21
  coinbase: null;
22
22
  prevout: TxOutput | null;
23
23
  script_sig: string | null;
24
24
  sequence: number;
25
25
  witness: string[];
26
26
  }
27
- export interface SpendInput extends VirtualInput {
27
+ export interface TxSpendInput extends TxVirtualInput {
28
28
  prevout: TxOutput;
29
29
  }
30
30
  export interface TxOutput {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vbyte/btc-dev",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Batteries-included toolset for plebian bitcoin development",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -41,13 +41,9 @@
41
41
  "types": "./dist/lib/address/index.d.ts",
42
42
  "import": "./dist/lib/address/index.js"
43
43
  },
44
- "./class": {
45
- "types": "./dist/class/index.d.ts",
46
- "import": "./dist/class/index.js"
47
- },
48
44
  "./meta": {
49
- "types": "./dist/lib/meta.d.ts",
50
- "import": "./dist/lib/meta.js"
45
+ "types": "./dist/lib/meta/index.d.ts",
46
+ "import": "./dist/lib/meta/index.js"
51
47
  },
52
48
  "./psbt": {
53
49
  "types": "./dist/lib/psbt/index.d.ts",
@@ -61,6 +57,10 @@
61
57
  "types": "./dist/lib/sighash/index.d.ts",
62
58
  "import": "./dist/lib/sighash/index.js"
63
59
  },
60
+ "./signer": {
61
+ "types": "./dist/lib/signer/index.d.ts",
62
+ "import": "./dist/lib/signer/index.js"
63
+ },
64
64
  "./taproot": {
65
65
  "types": "./dist/lib/taproot/index.d.ts",
66
66
  "import": "./dist/lib/taproot/index.js"
@@ -68,6 +68,10 @@
68
68
  "./tx": {
69
69
  "types": "./dist/lib/tx/index.d.ts",
70
70
  "import": "./dist/lib/tx/index.js"
71
+ },
72
+ "./witness": {
73
+ "types": "./dist/lib/witness/index.d.ts",
74
+ "import": "./dist/lib/witness/index.js"
71
75
  }
72
76
  },
73
77
  "scripts": {
@@ -4,7 +4,7 @@ import { ECC } from '@vbyte/micro-lib'
4
4
  import {
5
5
  sign_segwit_tx,
6
6
  sign_taproot_tx
7
- } from '@/lib/sighash/sign.js'
7
+ } from '@/lib/signer/sign.js'
8
8
 
9
9
  import type {
10
10
  SigHashOptions,
package/src/class/tx.ts CHANGED
@@ -1,12 +1,12 @@
1
+ import { LocktimeUtil } from '@/lib/meta/index.js'
1
2
  import { TransactionInput } from './txin.js'
2
3
  import { TransactionOutput } from './txout.js'
3
4
 
4
5
  import {
5
- decode_tx_data,
6
+ decode_tx,
6
7
  get_txid,
7
8
  is_return_script,
8
- parse_tx_data,
9
- Locktime,
9
+ parse_tx,
10
10
  get_txsize,
11
11
  get_tx_value,
12
12
  get_txhash,
@@ -32,8 +32,8 @@ export class Transaction {
32
32
 
33
33
  constructor (txdata : string | TxData | TxTemplate) {
34
34
  this._tx = (typeof txdata !== 'string')
35
- ? parse_tx_data(txdata)
36
- : decode_tx_data(txdata)
35
+ ? parse_tx(txdata)
36
+ : decode_tx(txdata)
37
37
 
38
38
  this._vin = this._tx.vin.map(txin => new TransactionInput(txin))
39
39
  this._vout = this._tx.vout.map(txout => new TransactionOutput(txout))
@@ -65,7 +65,7 @@ export class Transaction {
65
65
  get locktime () {
66
66
  return {
67
67
  hex : encode_tx_locktime(this._tx.locktime).hex,
68
- data : Locktime.decode(this._tx.locktime),
68
+ data : LocktimeUtil.decode(this._tx.locktime),
69
69
  value : this._tx.locktime
70
70
  }
71
71
  }
package/src/class/txin.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { decode_script } from '@/lib/script/index.js'
2
+ import { SequenceUtil } from '@/lib/meta/index.js'
2
3
  import { TransactionOutput } from './txout.js'
3
4
  import { TransactionWitness } from './witness.js'
4
5
 
5
6
  import {
6
7
  encode_txin_sequence,
7
8
  get_txin_size,
8
- Sequence
9
9
  } from '@/lib/tx/index.js'
10
10
 
11
11
  import type {
@@ -69,7 +69,7 @@ export class TransactionInput {
69
69
  get sequence () {
70
70
  return {
71
71
  hex : encode_txin_sequence(this._txin.sequence).hex,
72
- data : Sequence.decode(this._txin.sequence),
72
+ data : SequenceUtil.decode(this._txin.sequence),
73
73
  value : this._txin.sequence
74
74
  }
75
75
  }
@@ -4,7 +4,7 @@ import { decode_script } from '@/lib/script/index.js'
4
4
  import {
5
5
  parse_witness_data,
6
6
  get_witness_size,
7
- } from '@/lib/tx/index.js'
7
+ } from '@/lib/witness/index.js'
8
8
 
9
9
  import type {
10
10
  ScriptField,
package/src/index.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  export * from './class/index.js'
2
2
 
3
- export * as Address from './lib/address/index.js'
4
- export * as Meta from './lib/meta/index.js'
3
+ export * as ADDRESS from './lib/address/index.js'
4
+ export * as META from './lib/meta/index.js'
5
5
  export * as PSBT from './lib/psbt/index.js'
6
- export * as Script from './lib/script/index.js'
7
- export * as Sighash from './lib/sighash/index.js'
8
- export * as Taproot from './lib/taproot/index.js'
9
- export * as Tx from './lib/tx/index.js'
6
+ export * as SCRIPT from './lib/script/index.js'
7
+ export * as SIGHASH from './lib/sighash/index.js'
8
+ export * as SIGNER from './lib/signer/index.js'
9
+ export * as TAPROOT from './lib/taproot/index.js'
10
+ export * as TX from './lib/tx/index.js'
11
+ export * as WITNESS from './lib/witness/index.js'
10
12
 
11
13
  export * as CONST from './const.js'
12
14
  export * as SCHEMA from './schema/index.js'
@@ -1,2 +1,4 @@
1
- export * from './pointer.js'
1
+ export * from './locktime.js'
2
+ export * from './ref.js'
2
3
  export * from './scribe.js'
4
+ export * from './sequence.js'
@@ -5,7 +5,7 @@ import type { LocktimeInfo } from '@/types/index.js'
5
5
  // The threshold between block height and timestamp.
6
6
  const LOCKTIME_THRESHOLD = 500000000
7
7
 
8
- export namespace Locktime {
8
+ export namespace LocktimeUtil {
9
9
  export const encode = encode_locktime
10
10
  export const decode = decode_locktime
11
11
  }
@@ -1,4 +1,4 @@
1
- export namespace TxPointer {
1
+ export namespace RefEncoder {
2
2
  export const outpoint = {
3
3
  encode : encode_outpoint,
4
4
  decode : decode_outpoint,
@@ -9,7 +9,7 @@ const _0n = BigInt(0)
9
9
  const _1n = BigInt(1)
10
10
  const _26n = BigInt(26)
11
11
 
12
- export namespace Inscription {
12
+ export namespace ScribeEncoder {
13
13
  export const encode = encode_inscription
14
14
  export const decode = decode_inscription
15
15
  }
@@ -23,7 +23,7 @@ const TIMELOCK_GRANULARITY = 512 // Seconds per timestamp unit (BIP-68 s
23
23
 
24
24
  /* ===== [ API ] ============================================================ */
25
25
 
26
- export namespace Sequence {
26
+ export namespace SequenceUtil {
27
27
  export const encode = encode_sequence
28
28
  export const decode = decode_sequence
29
29
  }
@@ -1,5 +1,3 @@
1
1
  export * from './segwit.js'
2
- export * from './sign.js'
3
2
  export * from './taproot.js'
4
- export * from './verify.js'
5
3
  export * from './util.js'
@@ -15,7 +15,7 @@ import {
15
15
  encode_txin_txid,
16
16
  encode_vout_value,
17
17
  encode_tx_version,
18
- parse_tx_data
18
+ parse_tx
19
19
  } from '@/lib/tx/index.js'
20
20
 
21
21
  import {
@@ -34,7 +34,7 @@ export function hash_segwit_tx (
34
34
  // Unpack the sigflag from our config object.
35
35
  const { sigflag = 0x01, txindex } = options
36
36
  // Normalize the tx into JSON format.
37
- const tx = parse_tx_data(txdata)
37
+ const tx = parse_tx(txdata)
38
38
  // Check if the ANYONECANPAY flag is set.
39
39
  const is_anypay = (sigflag & 0x80) === 0x80
40
40
  // Save a normalized version of the sigflag.
@@ -3,7 +3,7 @@ import { Assert } from '@vbyte/micro-lib'
3
3
  import { hash340, sha256 } from '@vbyte/micro-lib/hash'
4
4
  import { prefix_script_size } from '@/lib/script/util.js'
5
5
  import { encode_tapscript } from '@/lib/taproot/encode.js'
6
- import { parse_tx_data } from '@/lib/tx/parse.js'
6
+ import { parse_tx } from '@/lib/tx/parse.js'
7
7
  import * as CONST from '@/const.js'
8
8
  import { parse_txinput } from './util.js'
9
9
 
@@ -37,7 +37,7 @@ export function hash_taproot_tx (
37
37
  separator_pos = 0xFFFFFFFF
38
38
  } = config
39
39
  // Normalize the txdata object.
40
- const tx = parse_tx_data(template)
40
+ const tx = parse_tx(template)
41
41
  // Unpack the txdata object.
42
42
  const { version, vin: input, vout: output, locktime } = tx
43
43
  // Parse the input we are signing from the config.
@@ -0,0 +1,2 @@
1
+ export * from './sign.js'
2
+ export * from './verify.js'
@@ -1,9 +1,9 @@
1
1
  import { ECC } from '@vbyte/micro-lib'
2
- import { parse_tx_data } from '@/lib/tx/parse.js'
2
+ import { parse_tx } from '@/lib/tx/parse.js'
3
3
  import { SIGHASH_DEFAULT } from '@/const.js'
4
- import { hash_segwit_tx } from './segwit.js'
5
- import { hash_taproot_tx } from './taproot.js'
6
- import { format_sigflag } from './util.js'
4
+ import { hash_segwit_tx } from '../sighash/segwit.js'
5
+ import { hash_taproot_tx } from '../sighash/taproot.js'
6
+ import { format_sigflag } from '../sighash/util.js'
7
7
 
8
8
  import type {
9
9
  SigHashOptions,
@@ -15,7 +15,7 @@ export function sign_segwit_tx (
15
15
  txdata : TxData,
16
16
  options : SigHashOptions,
17
17
  ) {
18
- const tx = parse_tx_data(txdata)
18
+ const tx = parse_tx(txdata)
19
19
  const msg = hash_segwit_tx(tx, options)
20
20
  const sig = ECC.sign_ecdsa(seckey, msg).hex
21
21
  const flag = format_sigflag(options.sigflag ?? SIGHASH_DEFAULT)
@@ -27,7 +27,7 @@ export function sign_taproot_tx (
27
27
  txdata : TxData,
28
28
  options : SigHashOptions,
29
29
  ) {
30
- const tx = parse_tx_data(txdata)
30
+ const tx = parse_tx(txdata)
31
31
  const msg = hash_taproot_tx(tx, options)
32
32
  const sig = ECC.sign_bip340(seckey, msg).hex
33
33
  const flag = format_sigflag(options.sigflag ?? 0)
@@ -7,6 +7,28 @@
7
7
  // import { HashConfig, TxData } from '@/types/index.js'
8
8
  // import { TxTemplate } from '@/schema/types.js'
9
9
  // import { hashTx } from './segwit.js'
10
+ import { Bytes } from '@vbyte/buff'
11
+
12
+ import type {
13
+ SigHashOptions,
14
+ TxData
15
+ } from '@/types/index.js'
16
+
17
+ export function verify_segwit_tx (
18
+ _txdata : TxData | Bytes,
19
+ _config : SigHashOptions = {}
20
+ ) : boolean {
21
+ console.warn('verify_segwit_tx is not implemented')
22
+ return true
23
+ }
24
+
25
+ export function verify_taproot_tx (
26
+ _txdata : TxData | Bytes,
27
+ _config : SigHashOptions = {}
28
+ ) : boolean {
29
+ console.warn('verify_taproot_tx is not implemented')
30
+ return true
31
+ }
10
32
 
11
33
  // export function verify_signature (
12
34
  // txdata : TxData | Bytes,
@@ -1,6 +1,6 @@
1
1
  import { Buff, Stream } from '@vbyte/buff'
2
2
  import { Assert, ECC } from '@vbyte/micro-lib'
3
- import { parse_witness_data } from '@/lib/tx/witness.js'
3
+ import { parse_witness_data } from '@/lib/witness/parse.js'
4
4
 
5
5
  import {
6
6
  encode_tapbranch,
@@ -13,13 +13,13 @@ import type {
13
13
  TxInput,
14
14
  TxOutput,
15
15
  TxTemplate,
16
- SpendInput,
17
- CoinbaseInput
16
+ TxSpendInput,
17
+ TxCoinbaseInput
18
18
  } from '@/types/index.js'
19
19
 
20
20
  export function create_coinbase_input (
21
21
  config : TxInputTemplate | TxInput
22
- ) : CoinbaseInput {
22
+ ) : TxCoinbaseInput {
23
23
  assert_vin_template(config)
24
24
  Assert.exists(config.coinbase, 'coinbase is required')
25
25
  const coinbase = config.coinbase
@@ -34,7 +34,7 @@ export function create_coinbase_input (
34
34
 
35
35
  export function create_spend_input (
36
36
  config : TxInputTemplate | TxInput
37
- ) : SpendInput {
37
+ ) : TxSpendInput {
38
38
  assert_vin_template(config)
39
39
  Assert.exists(config.prevout, 'prevout is required')
40
40
  const prevout = config.prevout
@@ -69,7 +69,7 @@ export function create_tx_output (
69
69
  return { script_pk, value : BigInt(value) }
70
70
  }
71
71
 
72
- export function create_tx_data (
72
+ export function create_tx (
73
73
  config: TxTemplate | TxData
74
74
  ) : TxData {
75
75
  assert_tx_template(config)
@@ -7,9 +7,9 @@ import {
7
7
  TxData,
8
8
  TxInput,
9
9
  TxOutput,
10
- CoinbaseInput,
11
- VirtualInput,
12
- SpendInput
10
+ TxCoinbaseInput,
11
+ TxVirtualInput,
12
+ TxSpendInput
13
13
  } from '@/types/index.js'
14
14
 
15
15
  interface TxEncoderConfig {
@@ -22,7 +22,7 @@ const DEFAULT_CONFIG : TxEncoderConfig = {
22
22
  segwit : true
23
23
  }
24
24
 
25
- export function decode_tx_data (
25
+ export function decode_tx (
26
26
  txbytes : Bytes,
27
27
  options : Partial<TxEncoderConfig> = {}
28
28
  ) : TxData {
@@ -87,11 +87,11 @@ function read_vin (stream : Stream, prevout : TxOutput | null = null) : TxInput
87
87
  const sequence = stream.read(4).reverse().num
88
88
  const witness : string[] = []
89
89
  if (txid === COINBASE.TXID && vout === COINBASE.VOUT) {
90
- return { coinbase : script_sig, prevout: null, script_sig : null, sequence, txid, vout, witness } as CoinbaseInput
90
+ return { coinbase : script_sig, prevout: null, script_sig : null, sequence, txid, vout, witness } as TxCoinbaseInput
91
91
  } else if (prevout !== null) {
92
- return { coinbase : null, prevout, script_sig, sequence, txid, vout, witness } as SpendInput
92
+ return { coinbase : null, prevout, script_sig, sequence, txid, vout, witness } as TxSpendInput
93
93
  } else {
94
- return { coinbase : null, prevout, script_sig, sequence, txid, vout, witness } as VirtualInput
94
+ return { coinbase : null, prevout, script_sig, sequence, txid, vout, witness } as TxVirtualInput
95
95
  }
96
96
  }
97
97
 
@@ -1,6 +1,6 @@
1
1
  import { Buff } from '@vbyte/buff'
2
2
  import { Assert } from '@vbyte/micro-lib'
3
- import { parse_tx_data } from './parse.js'
3
+ import { parse_tx } from './parse.js'
4
4
  import { COINBASE } from '@/const.js'
5
5
 
6
6
  import {
@@ -9,11 +9,11 @@ import {
9
9
  TxData
10
10
  } from '@/types/index.js'
11
11
 
12
- export function encode_tx_data (
12
+ export function encode_tx (
13
13
  txdata : TxData,
14
14
  segwit = true
15
15
  ) : Buff {
16
- const tx = parse_tx_data(txdata)
16
+ const tx = parse_tx(txdata)
17
17
  // Unpack the transaction data.
18
18
  const { version, vin, vout, locktime } = tx
19
19
  // Create a buffer for the transaction.
@@ -1,10 +1,7 @@
1
1
  export * from './create.js'
2
2
  export * from './decode.js'
3
3
  export * from './encode.js'
4
- export * from './locktime.js'
5
- export * from './meta.js'
4
+ export * from './util.js'
6
5
  export * from './parse.js'
7
- export * from './sequence.js'
8
6
  export * from './size.js'
9
7
  export * from './validate.js'
10
- export * from './witness.js'
@@ -1,16 +1,16 @@
1
- import { decode_tx_data } from './decode.js'
2
- import { create_tx_data } from './create.js'
1
+ import { decode_tx } from './decode.js'
2
+ import { create_tx } from './create.js'
3
3
  import { assert_tx_template } from './validate.js'
4
4
 
5
5
  import type { TxData } from '@/types/index.js'
6
6
 
7
- export function parse_tx_data (
7
+ export function parse_tx (
8
8
  txdata : unknown
9
9
  ) : TxData {
10
10
  if (typeof txdata === 'string') {
11
- return decode_tx_data(txdata)
11
+ return decode_tx(txdata)
12
12
  } else {
13
13
  assert_tx_template(txdata)
14
- return create_tx_data(txdata)
14
+ return create_tx(txdata)
15
15
  }
16
16
  }
@@ -1,8 +1,8 @@
1
1
  import { Buff, Bytes } from '@vbyte/buff'
2
- import { parse_tx_data } from './parse.js'
2
+ import { parse_tx } from './parse.js'
3
3
 
4
4
  import {
5
- encode_tx_data,
5
+ encode_tx,
6
6
  encode_tx_inputs,
7
7
  encode_tx_outputs,
8
8
  encode_tx_vout,
@@ -14,12 +14,10 @@ import type {
14
14
  TxData,
15
15
  TxInput,
16
16
  TxOutput,
17
- TxSize,
18
- WitnessSize,
17
+ TxSize
19
18
  } from '@/types/index.js'
20
19
 
21
- const WIT_FLAG_BYTES = 2
22
- const WIT_LENGTH_BYTE = 1
20
+ const WIT_FLAG_BYTES = 2
23
21
 
24
22
  export function get_vsize (
25
23
  bytes : Bytes
@@ -32,9 +30,9 @@ export function get_vsize (
32
30
  export function get_txsize (
33
31
  txdata : string | TxData
34
32
  ) : TxSize {
35
- const json = parse_tx_data(txdata)
36
- const base = encode_tx_data(json, false).length
37
- const size = encode_tx_data(json, true).length
33
+ const json = parse_tx(txdata)
34
+ const base = encode_tx(json, false).length
35
+ const size = encode_tx(json, true).length
38
36
  const weight = base * 3 + size
39
37
  const remain = (weight % 4 > 0) ? 1 : 0
40
38
  const vsize = Math.floor(weight / 4) + remain
@@ -68,10 +66,3 @@ export function get_txout_size (txoutput : TxOutput) : number {
68
66
  const bytes = encode_tx_vout(txoutput)
69
67
  return bytes.length
70
68
  }
71
-
72
- export function get_witness_size (witness : Bytes[]) : WitnessSize {
73
- const stack = witness.map(e => Buff.bytes(e))
74
- const size = stack.reduce((prev, next) => prev + next.length, 0)
75
- const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4)
76
- return { size, vsize }
77
- }
@@ -1,7 +1,7 @@
1
1
  import { hash256 } from '@vbyte/micro-lib/hash'
2
2
  import { LOCK_SCRIPT_REGEX } from '@/const.js'
3
- import { encode_tx_data } from './encode.js'
4
- import { parse_tx_data } from './parse.js'
3
+ import { encode_tx } from './encode.js'
4
+ import { parse_tx } from './parse.js'
5
5
 
6
6
  import type {
7
7
  TxData,
@@ -49,23 +49,23 @@ export function get_vout_version (
49
49
  export function get_txid (
50
50
  txdata : string | TxData
51
51
  ) : string {
52
- const json = parse_tx_data(txdata)
53
- const data = encode_tx_data(json, false)
52
+ const json = parse_tx(txdata)
53
+ const data = encode_tx(json, false)
54
54
  return hash256(data).reverse().hex
55
55
  }
56
56
 
57
57
  export function get_txhash (
58
58
  txdata : string | TxData
59
59
  ) : string {
60
- const json = parse_tx_data(txdata)
61
- const data = encode_tx_data(json, true)
60
+ const json = parse_tx(txdata)
61
+ const data = encode_tx(json, true)
62
62
  return hash256(data).reverse().hex
63
63
  }
64
64
 
65
65
  export function get_tx_value (
66
66
  txdata : string | TxData
67
67
  ) : TxValue {
68
- const tx = parse_tx_data(txdata)
68
+ const tx = parse_tx(txdata)
69
69
  const vin = tx.vin.reduce((acc, txin) => acc + (txin.prevout?.value ?? 0n), 0n)
70
70
  const vout = tx.vout.reduce((acc, txout) => acc + txout.value, 0n)
71
71
  const fees = (vin > vout) ? (vin - vout) : 0n
@@ -1,7 +1,7 @@
1
1
  import * as Schema from '@/schema/index.js'
2
2
 
3
3
  import {
4
- SpendInput,
4
+ TxSpendInput,
5
5
  TxData,
6
6
  TxInput,
7
7
  TxInputTemplate,
@@ -13,7 +13,7 @@ export function assert_tx_template (txdata : unknown) : asserts txdata is TxTemp
13
13
  Schema.tx.tx_template.parse(txdata)
14
14
  }
15
15
 
16
- export function assert_has_prevouts (vin : TxInput[]) : asserts vin is SpendInput[] {
16
+ export function assert_has_prevouts (vin : TxInput[]) : asserts vin is TxSpendInput[] {
17
17
  if (vin.some(txin => txin.prevout === null)) {
18
18
  throw new Error('transaction missing prevouts')
19
19
  }
@@ -0,0 +1,2 @@
1
+ export * from './parse.js'
2
+ export * from './util.js'
@@ -0,0 +1,12 @@
1
+ import { Buff, Bytes } from '@vbyte/buff'
2
+
3
+ import type { WitnessSize } from '@/types/index.js'
4
+
5
+ const WIT_LENGTH_BYTE = 1
6
+
7
+ export function get_witness_size (witness : Bytes[]) : WitnessSize {
8
+ const stack = witness.map(e => Buff.bytes(e))
9
+ const size = stack.reduce((prev, next) => prev + next.length, 0)
10
+ const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4)
11
+ return { size, vsize }
12
+ }