@vbyte/btc-dev 1.0.7 → 1.0.8

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.
@@ -1,6 +1,6 @@
1
1
  import type { LocktimeData, SequenceData } from './meta.js';
2
2
  import type { TxOutput, TxOutputType, TxSize, TxValue } from './txdata.js';
3
- import type { WitnessSize, WitnessType, WitnessVersion } from './witness.js';
3
+ import type { WitnessData, WitnessVersion } from './witness.js';
4
4
  export interface LocktimeField {
5
5
  hex: string;
6
6
  data: LocktimeData | null;
@@ -27,16 +27,6 @@ export interface TransactionData {
27
27
  vin: TransactionInputData[];
28
28
  vout: TransactionOutputData[];
29
29
  }
30
- export interface WitnessField {
31
- annex: string | null;
32
- cblock: string | null;
33
- params: string[];
34
- script: ScriptField | null;
35
- size: WitnessSize;
36
- stack: string[];
37
- type: WitnessType;
38
- version: WitnessVersion;
39
- }
40
30
  export interface TransactionInputData {
41
31
  coinbase?: string | null;
42
32
  prevout?: TransactionOutputData | null;
@@ -45,7 +35,7 @@ export interface TransactionInputData {
45
35
  size: number;
46
36
  txid: string;
47
37
  vout: number;
48
- witness?: WitnessField | null;
38
+ witness?: WitnessData | null;
49
39
  }
50
40
  export interface TransactionOutputData {
51
41
  script_pk: ScriptField;
@@ -10,6 +10,7 @@ export interface WitnessData {
10
10
  cblock: string | null;
11
11
  params: string[];
12
12
  script: string | null;
13
+ stack: string[];
13
14
  type: WitnessType;
14
15
  version: WitnessVersion;
15
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vbyte/btc-dev",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Batteries-included toolset for plebian bitcoin development",
5
5
  "type": "module",
6
6
  "keywords": [
package/src/class/tx.ts CHANGED
@@ -13,16 +13,18 @@ import {
13
13
  get_tx_value,
14
14
  get_txhash,
15
15
  encode_tx_locktime,
16
+ create_tx_input,
17
+ create_tx_output,
16
18
  } from '@/lib/tx/index.js'
17
19
 
18
20
  import type {
19
21
  TxData,
20
22
  TxTemplate,
21
23
  TransactionData,
22
- TxInput,
23
24
  TxOutput,
24
25
  TxSize,
25
- TxValue
26
+ TxValue,
27
+ TxInputTemplate
26
28
  } from '@/types/index.js'
27
29
 
28
30
  export class Transaction {
@@ -36,7 +38,7 @@ export class Transaction {
36
38
  private _vin : TransactionInput[]
37
39
  private _vout : TransactionOutput[]
38
40
 
39
- constructor (txdata : string | TxData | TxTemplate) {
41
+ constructor (txdata : string | TxData | TxTemplate = {}) {
40
42
  this._tx = (typeof txdata !== 'string')
41
43
  ? parse_tx(txdata)
42
44
  : decode_tx(txdata)
@@ -109,18 +111,21 @@ export class Transaction {
109
111
  return this._vout
110
112
  }
111
113
 
112
- add_vin (txin : TxInput) {
114
+ add_vin (tx_input : TxInputTemplate) {
115
+ const txin = create_tx_input(tx_input)
113
116
  this._tx.vin.push(txin)
114
117
  this._update_vin()
115
118
  }
116
119
 
117
- add_vout (txout : TxOutput) {
120
+ add_vout (tx_output : TxOutput) {
121
+ const txout = create_tx_output(tx_output)
118
122
  this._tx.vout.push(txout)
119
123
  this._update_vout()
120
124
  }
121
125
 
122
- insert_vin (index : number, txin : TxInput) {
126
+ insert_vin (index : number, tx_input : TxInputTemplate) {
123
127
  Assert.ok(index >= 0 && index <= this._tx.vin.length, 'input goes out of bounds')
128
+ const txin = create_tx_input(tx_input)
124
129
  if (index === this._tx.vin.length) {
125
130
  this._tx.vin.push(txin)
126
131
  } else {
@@ -129,8 +134,9 @@ export class Transaction {
129
134
  this._update_vin()
130
135
  }
131
136
 
132
- insert_vout (index : number, txout : TxOutput) {
137
+ insert_vout (index : number, tx_output : TxOutput) {
133
138
  Assert.ok(index >= 0 && index <= this._tx.vout.length, 'output goes out of bounds')
139
+ const txout = create_tx_output(tx_output)
134
140
  if (index === this._tx.vout.length) {
135
141
  this._tx.vout.push(txout)
136
142
  } else {
@@ -9,7 +9,6 @@ import {
9
9
 
10
10
  import type {
11
11
  ScriptField,
12
- WitnessField,
13
12
  WitnessData,
14
13
  WitnessSize,
15
14
  WitnessType
@@ -36,17 +35,8 @@ export class TransactionWitness {
36
35
  return this._data.cblock
37
36
  }
38
37
 
39
- get data () : WitnessField {
40
- return {
41
- annex : this.annex,
42
- cblock : this.cblock,
43
- params : this.params,
44
- script : this.script,
45
- size : this.size,
46
- stack : this.stack,
47
- type : this.type,
48
- version : this.version
49
- }
38
+ get data () : WitnessData {
39
+ return this._data
50
40
  }
51
41
 
52
42
  get params () : string[] {
@@ -12,6 +12,7 @@ export function parse_witness (
12
12
  ) : WitnessData {
13
13
  // Parse the witness data.
14
14
  const elems = witness.map(e => Buff.bytes(e))
15
+ const stack = witness.map(e => Buff.bytes(e).hex)
15
16
  const annex = parse_annex_data(elems)
16
17
  if (annex !== null) elems.pop()
17
18
  const cblock = parse_cblock_data(elems)
@@ -21,7 +22,7 @@ export function parse_witness (
21
22
  const script = parse_witness_script(elems, type)
22
23
  if (script !== null) elems.pop()
23
24
  const params = elems.map(e => e.hex)
24
- return { annex, cblock, params, script, type, version }
25
+ return { annex, cblock, params, script, stack, type, version }
25
26
  }
26
27
 
27
28
  function parse_annex_data (
@@ -1,6 +1,6 @@
1
1
  import type { LocktimeData, SequenceData } from './meta.js'
2
2
  import type { TxOutput, TxOutputType, TxSize, TxValue } from './txdata.js'
3
- import type { WitnessSize, WitnessType, WitnessVersion } from './witness.js'
3
+ import type { WitnessData, WitnessVersion } from './witness.js'
4
4
 
5
5
  export interface LocktimeField {
6
6
  hex : string
@@ -32,17 +32,6 @@ export interface TransactionData {
32
32
  vout : TransactionOutputData[]
33
33
  }
34
34
 
35
- export interface WitnessField {
36
- annex : string | null
37
- cblock : string | null
38
- params : string[]
39
- script : ScriptField | null
40
- size : WitnessSize
41
- stack : string[]
42
- type : WitnessType
43
- version : WitnessVersion
44
- }
45
-
46
35
  export interface TransactionInputData {
47
36
  coinbase? : string | null
48
37
  prevout? : TransactionOutputData | null
@@ -51,7 +40,7 @@ export interface TransactionInputData {
51
40
  size : number
52
41
  txid : string
53
42
  vout : number
54
- witness? : WitnessField | null
43
+ witness? : WitnessData | null
55
44
  }
56
45
 
57
46
  export interface TransactionOutputData {
@@ -12,6 +12,7 @@ export interface WitnessData {
12
12
  cblock : string | null
13
13
  params : string[]
14
14
  script : string | null
15
+ stack : string[]
15
16
  type : WitnessType
16
17
  version : WitnessVersion
17
18
  }