@vbyte/btc-dev 1.1.0 → 1.1.2
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/dist/lib/meta/locktime.d.ts +1 -1
- package/dist/lib/meta/locktime.js +5 -5
- package/dist/lib/meta/ref.d.ts +1 -1
- package/dist/lib/meta/ref.js +6 -6
- package/dist/lib/meta/scribe.d.ts +5 -3
- package/dist/lib/meta/scribe.js +21 -18
- package/dist/lib/meta/sequence.d.ts +1 -1
- package/dist/lib/meta/sequence.js +5 -5
- package/dist/lib/script/decode.d.ts +2 -1
- package/dist/lib/script/encode.d.ts +1 -1
- package/dist/lib/script/encode.js +3 -3
- package/dist/lib/tx/util.d.ts +5 -4
- package/dist/lib/tx/util.js +14 -12
- package/dist/main.cjs +61 -56
- package/dist/main.cjs.map +1 -1
- package/dist/module.mjs +61 -56
- package/dist/module.mjs.map +1 -1
- package/dist/package.json +1 -1
- package/dist/script.js +6 -6
- package/dist/script.js.map +1 -1
- package/dist/types/meta.d.ts +3 -3
- package/package.json +1 -1
- package/src/lib/meta/locktime.ts +1 -1
- package/src/lib/meta/ref.ts +1 -1
- package/src/lib/meta/scribe.ts +29 -24
- package/src/lib/meta/sequence.ts +1 -1
- package/src/lib/script/decode.ts +2 -2
- package/src/lib/script/encode.ts +4 -5
- package/src/lib/tx/util.ts +21 -19
- package/src/types/meta.ts +3 -3
package/dist/types/meta.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ import type { WitnessType, WitnessVersion } from './witness.js';
|
|
|
2
2
|
export type LocktimeData = LocktimeStamp | LocktimeHeight;
|
|
3
3
|
export type SequenceConfig = Partial<SequenceData>;
|
|
4
4
|
export type SequenceData = SequenceHeightLock | SequenceStampLock;
|
|
5
|
-
export type TxOutputType =
|
|
5
|
+
export type TxOutputType = 'p2pkh' | 'p2sh' | 'p2w-pkh' | 'p2w-sh' | 'p2tr' | 'opreturn';
|
|
6
6
|
export interface TxOutputInfo {
|
|
7
|
-
type: TxOutputType;
|
|
8
|
-
version: WitnessVersion;
|
|
7
|
+
type: TxOutputType | null;
|
|
8
|
+
version: WitnessVersion | null;
|
|
9
9
|
}
|
|
10
10
|
export interface TxInputInfo {
|
|
11
11
|
type: WitnessType;
|
package/package.json
CHANGED
package/src/lib/meta/locktime.ts
CHANGED
|
@@ -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
|
|
8
|
+
export namespace LocktimeField {
|
|
9
9
|
export const encode = encode_locktime
|
|
10
10
|
export const decode = decode_locktime
|
|
11
11
|
}
|
package/src/lib/meta/ref.ts
CHANGED
package/src/lib/meta/scribe.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Buff, Stream }
|
|
2
|
-
import { Assert }
|
|
3
|
-
import { encode_script }
|
|
4
|
-
import { decode_script }
|
|
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
|
|
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 :
|
|
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[]) :
|
|
25
|
-
return data.map(create_envelope)
|
|
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) :
|
|
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 :
|
|
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 :
|
|
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
|
-
|
|
159
|
+
identifier : Bytes
|
|
155
160
|
) : string {
|
|
156
|
-
const bytes = Buff.
|
|
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
|
-
|
|
174
|
+
bytes : Bytes
|
|
170
175
|
) : number {
|
|
171
|
-
return Buff.
|
|
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
|
-
|
|
186
|
+
label : Bytes
|
|
182
187
|
) : string {
|
|
183
|
-
return Buff.
|
|
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
|
-
|
|
208
|
-
|
|
212
|
+
chunks : Bytes[],
|
|
213
|
+
format : 'hex' | 'utf8' = 'hex'
|
|
209
214
|
) : string {
|
|
210
|
-
const data = Buff.join(
|
|
211
|
-
return (
|
|
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 (
|
|
233
|
+
function decode_rune_label (label: Bytes): string {
|
|
229
234
|
// Convert hex to BigInt, with byte order reversed
|
|
230
|
-
let big = Buff.
|
|
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
|
package/src/lib/meta/sequence.ts
CHANGED
|
@@ -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
|
|
26
|
+
export namespace SequenceField {
|
|
27
27
|
export const encode = encode_sequence
|
|
28
28
|
export const decode = decode_sequence
|
|
29
29
|
}
|
package/src/lib/script/decode.ts
CHANGED
|
@@ -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 :
|
|
13
|
+
script : Bytes
|
|
14
14
|
) : string[] {
|
|
15
15
|
const stream = new Stream(script)
|
|
16
16
|
|
package/src/lib/script/encode.ts
CHANGED
|
@@ -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
|
-
) :
|
|
14
|
-
if (words.length === 0) return
|
|
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'))
|
|
27
|
-
: buffer
|
|
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
|
*/
|
package/src/lib/tx/util.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Buff }
|
|
1
|
+
import { Buff, Bytes } from '@vbyte/buff'
|
|
2
2
|
import { Test } from '@vbyte/micro-lib'
|
|
3
3
|
import { Assert } from '@vbyte/micro-lib/assert'
|
|
4
4
|
import { hash256 } from '@vbyte/micro-lib/hash'
|
|
@@ -17,34 +17,36 @@ import type {
|
|
|
17
17
|
WitnessVersion
|
|
18
18
|
} from '@/types/index.js'
|
|
19
19
|
|
|
20
|
-
export function is_return_script (script :
|
|
21
|
-
|
|
20
|
+
export function is_return_script (script : Bytes) : boolean {
|
|
21
|
+
const bytes = Buff.bytes(script)
|
|
22
|
+
return bytes.at(0) === 0x6a
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
export function
|
|
25
|
+
export function get_vout_script_info (script : Bytes) : TxOutputInfo {
|
|
25
26
|
return {
|
|
26
|
-
type :
|
|
27
|
-
version :
|
|
27
|
+
type : get_vout_script_type(script),
|
|
28
|
+
version : get_vout_script_version(script)
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
export function
|
|
32
|
-
script :
|
|
33
|
-
) : TxOutputType {
|
|
32
|
+
export function get_vout_script_type (
|
|
33
|
+
script : Bytes
|
|
34
|
+
) : TxOutputType | null {
|
|
35
|
+
const hex = Buff.bytes(script).hex
|
|
34
36
|
for (const [ type, regex ] of Object.entries(LOCK_SCRIPT_REGEX)) {
|
|
35
|
-
if (regex.test(
|
|
37
|
+
if (regex.test(hex)) return type as TxOutputType
|
|
36
38
|
}
|
|
37
|
-
return
|
|
39
|
+
return null
|
|
38
40
|
}
|
|
39
41
|
|
|
40
|
-
export function
|
|
41
|
-
script :
|
|
42
|
-
) : WitnessVersion {
|
|
43
|
-
const
|
|
44
|
-
switch (
|
|
45
|
-
case
|
|
46
|
-
case
|
|
47
|
-
default
|
|
42
|
+
export function get_vout_script_version (
|
|
43
|
+
script : Bytes
|
|
44
|
+
) : WitnessVersion | null{
|
|
45
|
+
const version = Buff.bytes(script)
|
|
46
|
+
switch (version.at(0)) {
|
|
47
|
+
case 0x00 : return 0
|
|
48
|
+
case 0x51 : return 1
|
|
49
|
+
default : return null
|
|
48
50
|
}
|
|
49
51
|
}
|
|
50
52
|
|
package/src/types/meta.ts
CHANGED
|
@@ -3,11 +3,11 @@ import type{ WitnessType, WitnessVersion } from './witness.js'
|
|
|
3
3
|
export type LocktimeData = LocktimeStamp | LocktimeHeight
|
|
4
4
|
export type SequenceConfig = Partial<SequenceData>
|
|
5
5
|
export type SequenceData = SequenceHeightLock | SequenceStampLock
|
|
6
|
-
export type TxOutputType =
|
|
6
|
+
export type TxOutputType = 'p2pkh' | 'p2sh' | 'p2w-pkh' | 'p2w-sh' | 'p2tr' | 'opreturn'
|
|
7
7
|
|
|
8
8
|
export interface TxOutputInfo {
|
|
9
|
-
type : TxOutputType
|
|
10
|
-
version : WitnessVersion
|
|
9
|
+
type : TxOutputType | null
|
|
10
|
+
version : WitnessVersion | null
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export interface TxInputInfo {
|