@vbyte/btc-dev 1.0.8 → 1.0.10

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,5 +1,4 @@
1
1
  export * from './address.js';
2
- export * from './class.js';
3
2
  export * from './meta.js';
4
3
  export * from './psbt.js';
5
4
  export * from './sighash.js';
@@ -1,5 +1,4 @@
1
1
  export * from './address.js';
2
- export * from './class.js';
3
2
  export * from './meta.js';
4
3
  export * from './psbt.js';
5
4
  export * from './sighash.js';
@@ -1,6 +1,16 @@
1
+ import type { WitnessType, WitnessVersion } from './witness.js';
1
2
  export type LocktimeData = LocktimeStamp | LocktimeHeight;
2
3
  export type SequenceConfig = Partial<SequenceData>;
3
4
  export type SequenceData = SequenceHeightLock | SequenceStampLock;
5
+ export type TxOutputType = WitnessType | 'p2pkh' | 'p2sh' | 'opreturn';
6
+ export interface TxOutputInfo {
7
+ type: TxOutputType;
8
+ version: WitnessVersion;
9
+ }
10
+ export interface TxInputInfo {
11
+ type: WitnessType;
12
+ version: WitnessVersion;
13
+ }
4
14
  export interface LocktimeStamp {
5
15
  type: 'timelock';
6
16
  stamp: number;
@@ -17,6 +27,20 @@ export interface SequenceHeightLock {
17
27
  height: number;
18
28
  mode: 'height';
19
29
  }
30
+ export interface LocktimeField {
31
+ hex: string;
32
+ data: LocktimeData | null;
33
+ value: number;
34
+ }
35
+ export interface SequenceField {
36
+ hex: string;
37
+ data: SequenceData | null;
38
+ value: number;
39
+ }
40
+ export interface ScriptField {
41
+ asm: string[];
42
+ hex: string;
43
+ }
20
44
  export interface InscriptionData {
21
45
  content?: string;
22
46
  delegate?: string;
@@ -1,14 +1,4 @@
1
- import type { WitnessType, WitnessVersion } from './witness.js';
2
1
  export type TxInput = TxCoinbaseInput | TxSpendInput | TxVirtualInput;
3
- export type TxOutputType = WitnessType | 'p2pkh' | 'p2sh' | 'opreturn';
4
- export interface TxOutputInfo {
5
- type: TxOutputType;
6
- version: WitnessVersion;
7
- }
8
- export interface TxInputInfo {
9
- type: WitnessType;
10
- version: WitnessVersion;
11
- }
12
2
  export interface TxOutpoint {
13
3
  txid: string;
14
4
  vout: number;
@@ -56,8 +46,8 @@ export interface TxData {
56
46
  export interface TxSize {
57
47
  base: number;
58
48
  total: number;
59
- weight: number;
60
49
  vsize: number;
50
+ weight: number;
61
51
  }
62
52
  export interface TxValue {
63
53
  fees: bigint;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vbyte/btc-dev",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "Batteries-included toolset for plebian bitcoin development",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -1,5 +1,5 @@
1
1
  import { Buff, Bytes } from '@vbyte/buff'
2
- import { ECC } from '@vbyte/micro-lib'
2
+ import { Assert, ECC } from '@vbyte/micro-lib'
3
3
 
4
4
  import {
5
5
  sign_segwit_tx,
@@ -15,6 +15,8 @@ export class TxSigner {
15
15
  private readonly _seckey : string
16
16
 
17
17
  constructor (seckey : Bytes) {
18
+ Assert.ok(Buff.is_bytes(seckey), 'seckey must be a string or bytes')
19
+ Assert.size(seckey, 32, 'seckey must be 32 bytes')
18
20
  this._seckey = Buff.bytes(seckey).hex
19
21
  }
20
22
 
package/src/class/tx.ts CHANGED
@@ -5,7 +5,6 @@ import { TransactionInput } from './txin.js'
5
5
  import { TransactionOutput } from './txout.js'
6
6
 
7
7
  import {
8
- decode_tx,
9
8
  get_txid,
10
9
  is_return_script,
11
10
  parse_tx,
@@ -20,7 +19,6 @@ import {
20
19
  import type {
21
20
  TxData,
22
21
  TxTemplate,
23
- TransactionData,
24
22
  TxOutput,
25
23
  TxSize,
26
24
  TxValue,
@@ -39,30 +37,18 @@ export class Transaction {
39
37
  private _vout : TransactionOutput[]
40
38
 
41
39
  constructor (txdata : string | TxData | TxTemplate = {}) {
42
- this._tx = (typeof txdata !== 'string')
43
- ? parse_tx(txdata)
44
- : decode_tx(txdata)
40
+ this._tx = parse_tx(txdata)
41
+ this._vin = this._tx.vin.map(txin => new TransactionInput(txin))
42
+ this._vout = this._tx.vout.map(txout => new TransactionOutput(txout))
43
+
45
44
  this._size = this._get_size()
46
45
  this._hash = get_txhash(this._tx)
47
46
  this._txid = get_txid(this._tx)
48
47
  this._value = get_tx_value(this._tx)
49
- this._vin = this._tx.vin.map(txin => new TransactionInput(txin))
50
- this._vout = this._tx.vout.map(txout => new TransactionOutput(txout))
51
48
  }
52
49
 
53
- get data () : TransactionData {
54
- return {
55
- hash : this.hash,
56
- locktime : this.locktime,
57
- return : this.return,
58
- size : this.size,
59
- spends : this.spends,
60
- txid : this.txid,
61
- value : this.value,
62
- version : this.version,
63
- vin : this.vin.map(txin => txin.data),
64
- vout : this.vout.map(txout => txout.data)
65
- }
50
+ get data () : TxData {
51
+ return this._tx
66
52
  }
67
53
 
68
54
  get hash () : string {
package/src/class/txin.ts CHANGED
@@ -4,20 +4,19 @@ import { TransactionOutput } from './txout.js'
4
4
  import { TransactionWitness } from './witness.js'
5
5
 
6
6
  import {
7
+ assert_tx_input,
7
8
  encode_txin_sequence,
8
9
  get_txin_size,
9
10
  } from '@/lib/tx/index.js'
10
11
 
11
- import type {
12
- TxInput,
13
- TransactionInputData
14
- } from '@/types/index.js'
12
+ import type { TxInput } from '@/types/index.js'
15
13
 
16
14
  export class TransactionInput {
17
15
 
18
16
  private readonly _txin : TxInput
19
17
 
20
18
  constructor (txin : TxInput) {
19
+ assert_tx_input(txin)
21
20
  this._txin = txin
22
21
  }
23
22
 
@@ -25,17 +24,8 @@ export class TransactionInput {
25
24
  return this._txin.coinbase
26
25
  }
27
26
 
28
- get data () : TransactionInputData {
29
- return {
30
- coinbase : this.coinbase,
31
- prevout : this.prevout?.data ?? null,
32
- script_sig : this.script_sig,
33
- sequence : this.sequence,
34
- size : this.size,
35
- txid : this.txid,
36
- vout : this.vout,
37
- witness : this.witness?.data ?? null
38
- }
27
+ get data () : TxInput {
28
+ return this._txin
39
29
  }
40
30
 
41
31
  get has_prevout () : boolean {
@@ -1,29 +1,25 @@
1
1
  import { decode_script } from '@/lib/script/index.js'
2
2
 
3
3
  import {
4
+ assert_tx_output,
4
5
  get_txout_size,
5
6
  get_vout_type,
6
7
  get_vout_version
7
8
  } from '@/lib/tx/index.js'
8
9
 
9
- import type { TxOutput, TransactionOutputData } from '@/types/index.js'
10
+ import type { TxOutput } from '@/types/index.js'
10
11
 
11
12
  export class TransactionOutput {
12
13
 
13
14
  private readonly _txout : TxOutput
14
15
 
15
16
  constructor (txout : TxOutput) {
17
+ assert_tx_output(txout)
16
18
  this._txout = txout
17
19
  }
18
20
 
19
- get data () : TransactionOutputData {
20
- return {
21
- script_pk : this.script_pk,
22
- size : this.size,
23
- type : this.type,
24
- value : this.value,
25
- version : this.version
26
- }
21
+ get data () : TxOutput {
22
+ return this._txout
27
23
  }
28
24
 
29
25
  get script_pk () {
@@ -5,6 +5,7 @@ import { decode_script } from '@/lib/script/index.js'
5
5
  import {
6
6
  parse_witness,
7
7
  get_witness_size,
8
+ assert_witness,
8
9
  } from '@/lib/witness/index.js'
9
10
 
10
11
  import type {
@@ -22,6 +23,7 @@ export class TransactionWitness {
22
23
  private _size : WitnessSize
23
24
 
24
25
  constructor (witness : Bytes[]) {
26
+ assert_witness(witness)
25
27
  this._elems = witness.map(e => Buff.bytes(e))
26
28
  this._data = parse_witness(this._elems)
27
29
  this._size = get_witness_size(this._elems)
@@ -29,7 +29,7 @@ export function decode_tx (
29
29
  // Merge the options with the default config.
30
30
  const config = { ...DEFAULT_CONFIG, ...options }
31
31
  // Assert the txhex is a bytes object.
32
- Assert.is_bytes(txbytes, 'txbytes must be hex or a unit array')
32
+ Assert.is_bytes(txbytes, 'transaction must be hex or a byte-array')
33
33
  // Setup a byte-stream.
34
34
  const stream = new Stream(txbytes)
35
35
  // Parse tx version.
@@ -24,7 +24,7 @@ export function get_vsize (
24
24
  ) : number {
25
25
  const weight = Buff.bytes(bytes).length
26
26
  const remain = (weight % 4 > 0) ? 1 : 0
27
- return Math.floor(weight / 4) + remain
27
+ return Math.ceil(weight / 4) + remain
28
28
  }
29
29
 
30
30
  export function get_txsize (
@@ -35,7 +35,7 @@ export function get_txsize (
35
35
  const total = encode_tx(json, true).length
36
36
  const weight = base * 3 + total
37
37
  const remain = (weight % 4 > 0) ? 1 : 0
38
- const vsize = Math.floor(weight / 4) + remain
38
+ const vsize = Math.ceil(weight / 4) + remain
39
39
  return { base, total, vsize, weight }
40
40
  }
41
41
 
@@ -1,4 +1,5 @@
1
1
  import { Buff, Bytes } from '@vbyte/buff'
2
+ import { Assert } from '@vbyte/micro-lib'
2
3
 
3
4
  import type { WitnessSize } from '@/types/index.js'
4
5
 
@@ -10,3 +11,8 @@ export function get_witness_size (witness : Bytes[]) : WitnessSize {
10
11
  const vsize = Math.ceil(WIT_LENGTH_BYTE + size / 4)
11
12
  return { total: size, vsize }
12
13
  }
14
+
15
+ export function assert_witness (witness : unknown) : asserts witness is Bytes[] {
16
+ Assert.ok(Array.isArray(witness), 'witness must be an array')
17
+ Assert.ok(witness.every(e => Buff.is_bytes(e)), 'witness must be an array of strings or bytes')
18
+ }
@@ -1,5 +1,4 @@
1
1
  export * from './address.js'
2
- export * from './class.js'
3
2
  export * from './meta.js'
4
3
  export * from './psbt.js'
5
4
  export * from './sighash.js'
package/src/types/meta.ts CHANGED
@@ -1,6 +1,19 @@
1
+ import type{ WitnessType, WitnessVersion } from './witness.js'
2
+
1
3
  export type LocktimeData = LocktimeStamp | LocktimeHeight
2
4
  export type SequenceConfig = Partial<SequenceData>
3
5
  export type SequenceData = SequenceHeightLock | SequenceStampLock
6
+ export type TxOutputType = WitnessType | 'p2pkh' | 'p2sh' | 'opreturn'
7
+
8
+ export interface TxOutputInfo {
9
+ type : TxOutputType
10
+ version : WitnessVersion
11
+ }
12
+
13
+ export interface TxInputInfo {
14
+ type : WitnessType
15
+ version : WitnessVersion
16
+ }
4
17
 
5
18
  export interface LocktimeStamp {
6
19
  type : 'timelock' // Discriminator for timelock type
@@ -24,6 +37,23 @@ export interface SequenceHeightLock {
24
37
  mode : 'height' // Discriminator for heightlock mode.
25
38
  }
26
39
 
40
+ export interface LocktimeField {
41
+ hex : string
42
+ data : LocktimeData | null
43
+ value : number
44
+ }
45
+
46
+ export interface SequenceField {
47
+ hex : string
48
+ data : SequenceData | null
49
+ value : number
50
+ }
51
+
52
+ export interface ScriptField {
53
+ asm : string[]
54
+ hex : string
55
+ }
56
+
27
57
  export interface InscriptionData {
28
58
  content ?: string
29
59
  delegate ?: string
@@ -1,17 +1,4 @@
1
- import type { WitnessType, WitnessVersion } from './witness.js'
2
-
3
- export type TxInput = TxCoinbaseInput | TxSpendInput | TxVirtualInput
4
- export type TxOutputType = WitnessType | 'p2pkh' | 'p2sh' | 'opreturn'
5
-
6
- export interface TxOutputInfo {
7
- type : TxOutputType
8
- version : WitnessVersion
9
- }
10
-
11
- export interface TxInputInfo {
12
- type : WitnessType
13
- version : WitnessVersion
14
- }
1
+ export type TxInput = TxCoinbaseInput | TxSpendInput | TxVirtualInput
15
2
 
16
3
  export interface TxOutpoint {
17
4
  txid : string
@@ -68,8 +55,8 @@ export interface TxData {
68
55
  export interface TxSize {
69
56
  base : number
70
57
  total : number
71
- weight : number
72
58
  vsize : number
59
+ weight : number
73
60
  }
74
61
 
75
62
  export interface TxValue {
@@ -1,46 +0,0 @@
1
- import type { LocktimeData, SequenceData } from './meta.js';
2
- import type { TxOutput, TxOutputType, TxSize, TxValue } from './txdata.js';
3
- import type { WitnessData, WitnessVersion } from './witness.js';
4
- export interface LocktimeField {
5
- hex: string;
6
- data: LocktimeData | null;
7
- value: number;
8
- }
9
- export interface SequenceField {
10
- hex: string;
11
- data: SequenceData | null;
12
- value: number;
13
- }
14
- export interface ScriptField {
15
- asm: string[];
16
- hex: string;
17
- }
18
- export interface TransactionData {
19
- hash: string;
20
- locktime: LocktimeField;
21
- return: TxOutput | null;
22
- size: TxSize;
23
- spends: TransactionOutputData[];
24
- txid: string;
25
- value: TxValue;
26
- version: number;
27
- vin: TransactionInputData[];
28
- vout: TransactionOutputData[];
29
- }
30
- export interface TransactionInputData {
31
- coinbase?: string | null;
32
- prevout?: TransactionOutputData | null;
33
- script_sig?: ScriptField | null;
34
- sequence: SequenceField;
35
- size: number;
36
- txid: string;
37
- vout: number;
38
- witness?: WitnessData | null;
39
- }
40
- export interface TransactionOutputData {
41
- script_pk: ScriptField;
42
- size: number;
43
- type: TxOutputType;
44
- value: bigint;
45
- version: WitnessVersion | null;
46
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,52 +0,0 @@
1
- import type { LocktimeData, SequenceData } from './meta.js'
2
- import type { TxOutput, TxOutputType, TxSize, TxValue } from './txdata.js'
3
- import type { WitnessData, WitnessVersion } from './witness.js'
4
-
5
- export interface LocktimeField {
6
- hex : string
7
- data : LocktimeData | null
8
- value : number
9
- }
10
-
11
- export interface SequenceField {
12
- hex : string
13
- data : SequenceData | null
14
- value : number
15
- }
16
-
17
- export interface ScriptField {
18
- asm : string[]
19
- hex : string
20
- }
21
-
22
- export interface TransactionData {
23
- hash : string
24
- locktime : LocktimeField
25
- return : TxOutput | null
26
- size : TxSize
27
- spends : TransactionOutputData[]
28
- txid : string
29
- value : TxValue
30
- version : number
31
- vin : TransactionInputData[]
32
- vout : TransactionOutputData[]
33
- }
34
-
35
- export interface TransactionInputData {
36
- coinbase? : string | null
37
- prevout? : TransactionOutputData | null
38
- script_sig? : ScriptField | null
39
- sequence : SequenceField
40
- size : number
41
- txid : string
42
- vout : number
43
- witness? : WitnessData | null
44
- }
45
-
46
- export interface TransactionOutputData {
47
- script_pk : ScriptField
48
- size : number
49
- type : TxOutputType
50
- value : bigint
51
- version : WitnessVersion | null
52
- }