1
- import { Buffer } from 'buffer'
1
+ import { Buffer } from 'buffer' ;
2
2
import { decodeObj , encodeAddress } from 'algosdk' ;
3
- import { Encoding } from '../enums.js' ;
4
3
import axios from 'axios' ;
4
+
5
+ import { Encoding } from '../enums.js' ;
5
6
import { isAddress } from './strings.js' ;
6
- export { decodeAddress , encodeAddress , getApplicationAddress , Transaction } from 'algosdk' ;
7
+
8
+ export {
9
+ decodeAddress ,
10
+ encodeAddress ,
11
+ getApplicationAddress ,
12
+ Transaction ,
13
+ } from 'algosdk' ;
7
14
8
15
export interface DecodedB64 {
9
- encoding : Encoding ,
10
- original : string ,
11
- decoded : Partial <
12
- Record < Encoding , string >
13
- > ,
16
+ encoding : Encoding ;
17
+ original : string ;
18
+ decoded : Partial < Record < Encoding , string > > ;
14
19
}
15
20
16
21
/**
17
22
* Convert between UTF8 and base64
18
23
* ==================================================
19
24
*/
20
25
21
- export function utf8ToB64 ( str : string ) {
26
+ export function utf8ToB64 ( str : string ) {
22
27
return Buffer . from ( str ) . toString ( 'base64' ) ;
23
28
}
24
- export function hexToB64 ( str : string ) {
29
+ export function hexToB64 ( str : string ) {
25
30
return Buffer . from ( str . replace ( / ^ 0 x / , '' ) , 'hex' ) . toString ( 'base64' ) ;
26
31
}
27
- export function b64ToUtf8 ( str : string ) {
32
+ export function b64ToUtf8 ( str : string ) {
28
33
return Buffer . from ( str , 'base64' ) . toString ( ) ;
29
34
}
30
35
31
-
32
-
33
36
/**
34
37
* Convert object values to strings
35
38
* ==================================================
36
39
*/
37
40
export function objectValuesToString ( params : object ) {
38
- const obj : { [ k :string ] : string } = { } ;
39
- Object . entries ( params )
40
- . forEach ( ( [ key , value ] ) => {
41
- obj [ key ] = String ( value ) ;
42
- } ) ;
41
+ const obj : { [ k : string ] : string } = { } ;
42
+ Object . entries ( params ) . forEach ( ( [ key , value ] ) => {
43
+ obj [ key ] = String ( value ) ;
44
+ } ) ;
43
45
return obj ;
44
46
}
45
47
46
-
47
-
48
48
export async function decompileTeal ( b64 : string ) {
49
-
50
49
const programBuffer = Buffer . from ( b64 , 'base64' ) ;
51
50
const result = await axios ( {
52
51
method : 'post' ,
53
- url : 'https://mainnet-api.algonode.cloud /v2/teal/disassemble' ,
52
+ url : 'https://mainnet-api.4160.nodely.dev/x2/assetautorank /v2/teal/disassemble' ,
54
53
headers : {
55
54
'Content-Type' : 'application/x-binary' ,
56
55
} ,
57
56
data : programBuffer ,
58
- } )
57
+ } ) ;
59
58
// const append = Buffer.from([0xa4, 0x61, 0x70, 0x61, 0x70]);
60
59
// const prepend = Buffer.from([ 0x8]);
61
60
// const buffer = Buffer.concat([append, programBuffer]);
@@ -64,30 +63,30 @@ export async function decompileTeal(b64: string) {
64
63
return result ;
65
64
}
66
65
67
-
68
-
69
66
/**
70
67
* Decode base 64
71
68
* Check for encoding returning latin chars
72
69
* ==================================================
73
70
*/
74
71
export class B64Decoder {
75
72
public readonly original : string ;
76
- public readonly parsed : Partial < Record < Encoding , string | number | Record < string , any > > > ;
73
+ public readonly parsed : Partial <
74
+ Record < Encoding , string | number | Record < string , any > >
75
+ > ;
77
76
public encoding : Encoding ;
78
-
79
- constructor ( str : string ) {
77
+
78
+ constructor ( str : string ) {
80
79
this . original = str ;
81
80
this . encoding = Encoding . B64 ;
82
81
const buffer = Buffer . from ( str , 'base64' ) ;
83
- const decodedStr = buffer . toString ( 'utf8' ) ;
84
- const decodedHex = '0x' + buffer . toString ( 'hex' ) . toUpperCase ( )
82
+ const decodedStr = buffer . toString ( 'utf8' ) ;
83
+ const decodedHex = '0x' + buffer . toString ( 'hex' ) . toUpperCase ( ) ;
85
84
const decodedNumber = parseInt ( decodedHex , 16 ) ;
86
85
this . parsed = {
87
86
[ Encoding . B64 ] : str ,
88
87
[ Encoding . HEX ] : decodedHex ,
89
88
} ;
90
-
89
+
91
90
// UTF8 - Latin char only
92
91
if ( / \p{ L} + / gu. test ( decodedStr ) ) {
93
92
this . parsed [ Encoding . UTF8 ] = decodedStr . replace ( / [ \x00 - \x1F ] / g, ' ' ) ;
@@ -97,15 +96,17 @@ export class B64Decoder {
97
96
// Number
98
97
if ( ! isNaN ( decodedNumber ) && decodedNumber < Number . MAX_SAFE_INTEGER ) {
99
98
this . parsed [ Encoding . NUMBER ] = decodedNumber ;
100
- if ( ! / ^ [ \a - zA- Z 0 - 9 \s \. - _ ] + $ / i. test ( decodedStr ) ) {
99
+ if ( ! / ^ [ \a - zA- Z 0 - 9 \s \. - _ ] + $ / i. test ( decodedStr ) ) {
101
100
this . encoding = Encoding . NUMBER ;
102
- }
103
- const hexBytesLength = decodedHex . length - 2 ;
104
- if ( hexBytesLength <= 32 && new Date ( decodedNumber * 1000 ) . getFullYear ( ) > 2000 ) {
101
+ }
102
+ const hexBytesLength = decodedHex . length - 2 ;
103
+ if (
104
+ hexBytesLength <= 32 &&
105
+ new Date ( decodedNumber * 1000 ) . getFullYear ( ) > 2000
106
+ ) {
105
107
this . parsed [ Encoding . TIMESTAMP ] = decodedNumber * 1000 ;
106
108
}
107
109
}
108
-
109
110
110
111
// JSON
111
112
if ( decodedStr . startsWith ( '{' ) ) {
@@ -133,7 +134,7 @@ export class B64Decoder {
133
134
}
134
135
}
135
136
136
- get decoded ( ) {
137
+ get decoded ( ) {
137
138
return this . parsed ?. [ this . encoding ] ;
138
139
}
139
140
}
0 commit comments