8
8
disassembleBytecodeBCH ,
9
9
AuthenticationInstructions ,
10
10
hexToBin ,
11
+ utf8ToBin ,
12
+ binToUtf8 ,
11
13
} from '@bitauth/libauth' ;
12
14
import { ANTLRInputStream , CommonTokenStream } from 'antlr4ts' ;
13
15
import fs from 'fs' ;
@@ -25,11 +27,11 @@ import ReplaceBytecodeNop from './generation/ReplaceBytecodeNop';
25
27
import VerifyCovenantTraversal from './semantic/VerifyCovenantTraversal' ;
26
28
27
29
export const Data = {
28
- encodeBool ( bool : boolean ) : Buffer {
30
+ encodeBool ( bool : boolean ) : Uint8Array {
29
31
return bool ? this . encodeInt ( 1 ) : this . encodeInt ( 0 ) ;
30
32
} ,
31
33
32
- decodeBool ( encodedBool : Buffer ) : boolean {
34
+ decodeBool ( encodedBool : Uint8Array ) : boolean {
33
35
// Any encoding of 0 is false, else true
34
36
for ( let i = 0 ; i < encodedBool . byteLength ; i += 1 ) {
35
37
if ( encodedBool [ i ] !== 0 ) {
@@ -41,11 +43,11 @@ export const Data = {
41
43
return false ;
42
44
} ,
43
45
44
- encodeInt ( int : number ) : Buffer {
45
- return Buffer . from ( bigIntToScriptNumber ( BigInt ( int ) ) ) ;
46
+ encodeInt ( int : number ) : Uint8Array {
47
+ return bigIntToScriptNumber ( BigInt ( int ) ) ;
46
48
} ,
47
49
48
- decodeInt ( encodedInt : Buffer , maxLength ?: number ) : number {
50
+ decodeInt ( encodedInt : Uint8Array , maxLength ?: number ) : number {
49
51
const options = { maximumScriptNumberByteLength : maxLength } ;
50
52
const result = parseBytesAsScriptNumber ( encodedInt , options ) ;
51
53
@@ -56,12 +58,12 @@ export const Data = {
56
58
return Number ( result ) ;
57
59
} ,
58
60
59
- encodeString ( str : string ) : Buffer {
60
- return Buffer . from ( str , 'ascii' ) ;
61
+ encodeString ( str : string ) : Uint8Array {
62
+ return utf8ToBin ( str ) ;
61
63
} ,
62
64
63
- decodeString ( encodedString : Buffer ) : string {
64
- return encodedString . toString ( 'ascii' ) ;
65
+ decodeString ( encodedString : Uint8Array ) : string {
66
+ return binToUtf8 ( encodedString ) ;
65
67
} ,
66
68
67
69
scriptToAsm ( script : Script ) : string {
@@ -75,7 +77,7 @@ export const Data = {
75
77
scriptToBytecode ( script : Script ) : Uint8Array {
76
78
// Convert the script elements to AuthenticationInstructions
77
79
const instructions = script . map ( ( opOrData ) => {
78
- if ( opOrData instanceof Buffer ) {
80
+ if ( opOrData instanceof Uint8Array ) {
79
81
return parseBytecode ( encodeDataPush ( opOrData ) ) [ 0 ] ;
80
82
} else {
81
83
return { opcode : opOrData } ;
@@ -93,7 +95,7 @@ export const Data = {
93
95
// Convert the AuthenticationInstructions to script elements
94
96
const script = instructions . map ( ( instruction ) => {
95
97
if ( 'data' in instruction ) {
96
- return Buffer . from ( instruction . data ) ;
98
+ return instruction . data ;
97
99
} else {
98
100
return instruction . opcode ;
99
101
}
0 commit comments