@vbyte/btc-dev 1.0.9 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vbyte/btc-dev",
3
- "version": "1.0.9",
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,
@@ -38,15 +37,14 @@ export class Transaction {
38
37
  private _vout : TransactionOutput[]
39
38
 
40
39
  constructor (txdata : string | TxData | TxTemplate = {}) {
41
- this._tx = (typeof txdata !== 'string')
42
- ? parse_tx(txdata)
43
- : 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
+
44
44
  this._size = this._get_size()
45
45
  this._hash = get_txhash(this._tx)
46
46
  this._txid = get_txid(this._tx)
47
47
  this._value = get_tx_value(this._tx)
48
- this._vin = this._tx.vin.map(txin => new TransactionInput(txin))
49
- this._vout = this._tx.vout.map(txout => new TransactionOutput(txout))
50
48
  }
51
49
 
52
50
  get data () : TxData {
package/src/class/txin.ts CHANGED
@@ -4,6 +4,7 @@ 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'
@@ -15,6 +16,7 @@ export class TransactionInput {
15
16
  private readonly _txin : TxInput
16
17
 
17
18
  constructor (txin : TxInput) {
19
+ assert_tx_input(txin)
18
20
  this._txin = txin
19
21
  }
20
22
 
@@ -1,6 +1,7 @@
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
@@ -13,6 +14,7 @@ export class TransactionOutput {
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
 
@@ -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
+ }