@vbyte/btc-dev 1.1.0 → 1.1.1

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.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Batteries-included toolset for plebian bitcoin development",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -5,7 +5,7 @@ import type { LocktimeData } from '@/types/index.js'
5
5
  // The threshold between block height and timestamp.
6
6
  const LOCKTIME_THRESHOLD = 500000000
7
7
 
8
- export namespace LocktimeUtil {
8
+ export namespace LocktimeField {
9
9
  export const encode = encode_locktime
10
10
  export const decode = decode_locktime
11
11
  }
@@ -1,4 +1,4 @@
1
- export namespace RefEncoder {
1
+ export namespace RefPointer {
2
2
  export const outpoint = {
3
3
  encode : encode_outpoint,
4
4
  decode : decode_outpoint,
@@ -1,7 +1,7 @@
1
- import { Buff, Stream } from '@vbyte/buff'
2
- import { Assert } from '@vbyte/micro-lib'
3
- import { encode_script } from '@/lib/script/encode.js'
4
- import { decode_script } from '@/lib/script/decode.js'
1
+ import { Buff, Bytes, Stream } from '@vbyte/buff'
2
+ import { Assert } from '@vbyte/micro-lib'
3
+ import { encode_script } from '@/lib/script/encode.js'
4
+ import { decode_script } from '@/lib/script/decode.js'
5
5
 
6
6
  import type { InscriptionData } from '@/types/index.js'
7
7
 
@@ -9,23 +9,24 @@ const _0n = BigInt(0)
9
9
  const _1n = BigInt(1)
10
10
  const _26n = BigInt(26)
11
11
 
12
- export namespace ScribeEncoder {
12
+ export namespace InscriptionUtil {
13
+ export type Type = InscriptionData
13
14
  export const encode = encode_inscription
14
15
  export const decode = decode_inscription
15
16
  }
16
17
 
17
18
  export function decode_inscription (
18
- script : string
19
+ script : Bytes
19
20
  ) : InscriptionData[] {
20
21
  const envelopes = parse_envelopes(script)
21
22
  return envelopes.map(parse_record)
22
23
  }
23
24
 
24
- export function encode_inscription (data : InscriptionData[]) : string {
25
- return data.map(create_envelope).join('')
25
+ export function encode_inscription (data : InscriptionData[]) : Buff {
26
+ return Buff.join(data.map(create_envelope))
26
27
  }
27
28
 
28
- function create_envelope (data : InscriptionData) : string {
29
+ function create_envelope (data : InscriptionData) : Buff {
29
30
  let asm : string[] = [ 'OP_0', 'OP_IF', '6f7264' ]
30
31
 
31
32
  if (typeof data.delegate === 'string') {
@@ -73,7 +74,7 @@ function create_envelope (data : InscriptionData) : string {
73
74
  }
74
75
 
75
76
  function parse_envelopes (
76
- script : string
77
+ script : Bytes
77
78
  ) : string[][] {
78
79
 
79
80
  const words = decode_script(script)
@@ -98,7 +99,7 @@ function parse_envelopes (
98
99
  return envelopes
99
100
  }
100
101
 
101
- function parse_record (envelope : string[]) {
102
+ function parse_record (envelope : Bytes[]) {
102
103
  const record : InscriptionData = {}
103
104
 
104
105
  for (let i = 0; i < envelope.length; i++) {
@@ -124,7 +125,7 @@ function parse_record (envelope : string[]) {
124
125
  i += 1
125
126
  break
126
127
  case 'OP_WITHIN':
127
- record.ref = envelope[i+1]
128
+ record.ref = decode_bytes(envelope[i+1])
128
129
  i += 1
129
130
  break;
130
131
  case 'OP_NOP':
@@ -139,6 +140,10 @@ function parse_record (envelope : string[]) {
139
140
  return record
140
141
  }
141
142
 
143
+ function decode_bytes (bytes : Bytes) : string {
144
+ return Buff.bytes(bytes).hex
145
+ }
146
+
142
147
  function encode_id (
143
148
  identifier : string
144
149
  ) : string {
@@ -151,9 +156,9 @@ function encode_id (
151
156
  }
152
157
 
153
158
  function decode_id (
154
- hexstr : string
159
+ identifier : Bytes
155
160
  ) : string {
156
- const bytes = Buff.hex(hexstr)
161
+ const bytes = Buff.bytes(identifier)
157
162
  const idx = bytes.at(-1) ?? 0
158
163
  const txid = bytes.slice(0, -1).reverse().hex
159
164
  return txid + 'i' + String(idx)
@@ -166,9 +171,9 @@ function encode_pointer (
166
171
  }
167
172
 
168
173
  function decode_pointer (
169
- hexstr : string
174
+ bytes : Bytes
170
175
  ) : number {
171
- return Buff.hex(hexstr).reverse().num
176
+ return Buff.bytes(bytes).reverse().num
172
177
  }
173
178
 
174
179
  function encode_label (
@@ -178,9 +183,9 @@ function encode_label (
178
183
  }
179
184
 
180
185
  function decode_label (
181
- hexstr : string
186
+ label : Bytes
182
187
  ) : string {
183
- return Buff.hex(hexstr).str
188
+ return Buff.bytes(label).str
184
189
  }
185
190
 
186
191
  function encode_content (
@@ -204,11 +209,11 @@ function encode_content (
204
209
  }
205
210
 
206
211
  function decode_content (
207
- hexstrs : string[],
208
- type : 'hex' | 'utf8' = 'hex'
212
+ chunks : Bytes[],
213
+ format : 'hex' | 'utf8' = 'hex'
209
214
  ) : string {
210
- const data = Buff.join(hexstrs)
211
- return (type === 'hex')
215
+ const data = Buff.join(chunks)
216
+ return (format === 'hex')
212
217
  ? data.hex
213
218
  : data.str
214
219
  }
@@ -225,9 +230,9 @@ function encode_rune_label (label : string) : string {
225
230
  return Buff.big(big).reverse().hex
226
231
  }
227
232
 
228
- function decode_rune_label (hex: string): string {
233
+ function decode_rune_label (label: Bytes): string {
229
234
  // Convert hex to BigInt, with byte order reversed
230
- let big = Buff.hex(hex).reverse().big
235
+ let big = Buff.bytes(label).reverse().big
231
236
  // Add 1 as per the encoding algorithm
232
237
  big = big + _1n
233
238
  // Initialize result string
@@ -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 SequenceUtil {
26
+ export namespace SequenceField {
27
27
  export const encode = encode_sequence
28
28
  export const decode = decode_sequence
29
29
  }
@@ -1,4 +1,4 @@
1
- import { Stream } from '@vbyte/buff'
1
+ import { Bytes, Stream } from '@vbyte/buff'
2
2
 
3
3
  import {
4
4
  get_op_code,
@@ -10,7 +10,7 @@ import {
10
10
  * Decode a bitcoin script into asm instructions.
11
11
  */
12
12
  export function decode_script (
13
- script : string | Uint8Array
13
+ script : Bytes
14
14
  ) : string[] {
15
15
  const stream = new Stream(script)
16
16
 
@@ -10,8 +10,8 @@ const MAX_WORD_SIZE = 520
10
10
  export function encode_script (
11
11
  words : (string | number | Uint8Array)[],
12
12
  varint = false
13
- ) : string {
14
- if (words.length === 0) return '00'
13
+ ) : Buff {
14
+ if (words.length === 0) return Buff.num(0, 1)
15
15
 
16
16
  const bytes = []
17
17
 
@@ -23,11 +23,10 @@ export function encode_script (
23
23
  const buffer = Buff.join(bytes)
24
24
 
25
25
  return (varint)
26
- ? buffer.prepend(Buff.varint(buffer.length, 'le')).hex
27
- : buffer.hex
26
+ ? buffer.prepend(Buff.varint(buffer.length, 'le'))
27
+ : buffer
28
28
  }
29
29
 
30
-
31
30
  /** Check if the word is a valid opcode,
32
31
  * and return its integer value.
33
32
  */