@beclab/olaresid 0.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/LICENSE.md +49 -0
- package/README.md +93 -0
- package/dist/abi/ABITypeABI.d.ts +88 -0
- package/dist/abi/ABITypeABI.d.ts.map +1 -0
- package/dist/abi/ABITypeABI.js +382 -0
- package/dist/abi/ABITypeABI.js.map +1 -0
- package/dist/abi/RegistryABI.d.ts +77 -0
- package/dist/abi/RegistryABI.d.ts.map +1 -0
- package/dist/abi/RegistryABI.js +462 -0
- package/dist/abi/RegistryABI.js.map +1 -0
- package/dist/abi/RootResolverABI.d.ts +78 -0
- package/dist/abi/RootResolverABI.d.ts.map +1 -0
- package/dist/abi/RootResolverABI.js +372 -0
- package/dist/abi/RootResolverABI.js.map +1 -0
- package/dist/abi/TerminusDIDABI.d.ts +80 -0
- package/dist/abi/TerminusDIDABI.d.ts.map +1 -0
- package/dist/abi/TerminusDIDABI.js +1877 -0
- package/dist/abi/TerminusDIDABI.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +199 -0
- package/dist/cli.js.map +1 -0
- package/dist/debug.d.ts +46 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +129 -0
- package/dist/debug.js.map +1 -0
- package/dist/domain/index.d.ts +59 -0
- package/dist/domain/index.d.ts.map +1 -0
- package/dist/domain/index.js +431 -0
- package/dist/domain/index.js.map +1 -0
- package/dist/index.d.ts +193 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +166 -0
- package/dist/index.js.map +1 -0
- package/dist/tag/address.d.ts +11 -0
- package/dist/tag/address.d.ts.map +1 -0
- package/dist/tag/address.js +44 -0
- package/dist/tag/address.js.map +1 -0
- package/dist/tag/array.d.ts +14 -0
- package/dist/tag/array.d.ts.map +1 -0
- package/dist/tag/array.js +72 -0
- package/dist/tag/array.js.map +1 -0
- package/dist/tag/bool.d.ts +11 -0
- package/dist/tag/bool.d.ts.map +1 -0
- package/dist/tag/bool.js +43 -0
- package/dist/tag/bool.js.map +1 -0
- package/dist/tag/bytes.d.ts +11 -0
- package/dist/tag/bytes.d.ts.map +1 -0
- package/dist/tag/bytes.js +37 -0
- package/dist/tag/bytes.js.map +1 -0
- package/dist/tag/flarray.d.ts +15 -0
- package/dist/tag/flarray.d.ts.map +1 -0
- package/dist/tag/flarray.js +81 -0
- package/dist/tag/flarray.js.map +1 -0
- package/dist/tag/flbytes.d.ts +11 -0
- package/dist/tag/flbytes.d.ts.map +1 -0
- package/dist/tag/flbytes.js +47 -0
- package/dist/tag/flbytes.js.map +1 -0
- package/dist/tag/index.d.ts +32 -0
- package/dist/tag/index.d.ts.map +1 -0
- package/dist/tag/index.js +121 -0
- package/dist/tag/index.js.map +1 -0
- package/dist/tag/int.d.ts +12 -0
- package/dist/tag/int.d.ts.map +1 -0
- package/dist/tag/int.js +49 -0
- package/dist/tag/int.js.map +1 -0
- package/dist/tag/string.d.ts +11 -0
- package/dist/tag/string.d.ts.map +1 -0
- package/dist/tag/string.js +37 -0
- package/dist/tag/string.js.map +1 -0
- package/dist/tag/tag.d.ts +67 -0
- package/dist/tag/tag.d.ts.map +1 -0
- package/dist/tag/tag.js +157 -0
- package/dist/tag/tag.js.map +1 -0
- package/dist/tag/tuple.d.ts +17 -0
- package/dist/tag/tuple.d.ts.map +1 -0
- package/dist/tag/tuple.js +162 -0
- package/dist/tag/tuple.js.map +1 -0
- package/dist/tag/uint.d.ts +12 -0
- package/dist/tag/uint.d.ts.map +1 -0
- package/dist/tag/uint.js +49 -0
- package/dist/tag/uint.js.map +1 -0
- package/dist/test/did.d.ts +2 -0
- package/dist/test/did.d.ts.map +1 -0
- package/dist/test/did.js +177 -0
- package/dist/test/did.js.map +1 -0
- package/examples/index.ts +15 -0
- package/examples/legacy.ts +33 -0
- package/package.json +50 -0
- package/project.json +6 -0
- package/src/abi/ABITypeABI.ts +379 -0
- package/src/abi/RegistryABI.ts +459 -0
- package/src/abi/RootResolverABI.ts +369 -0
- package/src/abi/TerminusDIDABI.ts +1874 -0
- package/src/cli.ts +230 -0
- package/src/debug.ts +170 -0
- package/src/domain/index.ts +659 -0
- package/src/index.ts +302 -0
- package/src/tag/address.ts +48 -0
- package/src/tag/array.ts +80 -0
- package/src/tag/bool.ts +43 -0
- package/src/tag/bytes.ts +38 -0
- package/src/tag/flarray.ts +99 -0
- package/src/tag/flbytes.ts +48 -0
- package/src/tag/index.ts +170 -0
- package/src/tag/int.ts +51 -0
- package/src/tag/string.ts +38 -0
- package/src/tag/tag.ts +229 -0
- package/src/tag/tuple.ts +193 -0
- package/src/tag/uint.ts +51 -0
- package/src/test/did.ts +346 -0
- package/tsconfig.json +9 -0
package/src/tag/index.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Tag,
|
|
3
|
+
TagType,
|
|
4
|
+
TagBase,
|
|
5
|
+
readByte,
|
|
6
|
+
parserHolder,
|
|
7
|
+
read2Byte,
|
|
8
|
+
Box,
|
|
9
|
+
ProviderHolder
|
|
10
|
+
} from './tag';
|
|
11
|
+
import { Int } from './int';
|
|
12
|
+
import Uint from './uint';
|
|
13
|
+
import Bool from './bool';
|
|
14
|
+
import Array from './array';
|
|
15
|
+
import String from './string';
|
|
16
|
+
import FixedLengthArray from './flarray';
|
|
17
|
+
import Tuple from './tuple';
|
|
18
|
+
import Address from './address';
|
|
19
|
+
import FixedLengthBytes from './flbytes';
|
|
20
|
+
import Bytes from './bytes';
|
|
21
|
+
import { AbiCoder, ethers } from 'ethers';
|
|
22
|
+
|
|
23
|
+
const isHex = (hex: string) => {
|
|
24
|
+
return (
|
|
25
|
+
typeof hex === 'string' &&
|
|
26
|
+
hex.length % 2 === 0 &&
|
|
27
|
+
!isNaN(Number('0x' + hex))
|
|
28
|
+
);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const formatType = (typeBytes: string): Tag => {
|
|
32
|
+
if (typeBytes.startsWith('0x')) {
|
|
33
|
+
typeBytes = typeBytes.substring(2);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!isHex(typeBytes)) {
|
|
37
|
+
throw new Error('input is not a valid hex string');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let { cursor, tag } = parse(typeBytes, 0);
|
|
41
|
+
|
|
42
|
+
if (cursor !== typeBytes.length) {
|
|
43
|
+
throw new Error(`input is not a valid typeBytes`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (tag == undefined) {
|
|
47
|
+
throw new Error(`format type error, please check version`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return tag;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const encode = (tag: Tag, data: any) => {
|
|
54
|
+
const paramTypes = [];
|
|
55
|
+
const parameters = [];
|
|
56
|
+
|
|
57
|
+
paramTypes.push(tag.getParamTypes());
|
|
58
|
+
parameters.push(tag.getParameters(data));
|
|
59
|
+
|
|
60
|
+
const coder = new AbiCoder();
|
|
61
|
+
|
|
62
|
+
// console.log('paramTypes', paramTypes);
|
|
63
|
+
// console.dir(parameters, { depth: null });
|
|
64
|
+
|
|
65
|
+
return coder.encode(paramTypes, parameters);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// {cursor: number, tag: Tag}
|
|
69
|
+
const parse = (
|
|
70
|
+
typeBytes: string,
|
|
71
|
+
index: number
|
|
72
|
+
): { cursor: number; tag: Tag | undefined } => {
|
|
73
|
+
let cursor = 0;
|
|
74
|
+
let tag: Tag | undefined = undefined;
|
|
75
|
+
|
|
76
|
+
let typeFlag = readByte(typeBytes, index);
|
|
77
|
+
switch (typeFlag) {
|
|
78
|
+
case TagType.INT:
|
|
79
|
+
tag = new Int(typeBytes, index, false);
|
|
80
|
+
break;
|
|
81
|
+
case TagType.UINT:
|
|
82
|
+
tag = new Uint(typeBytes, index, false);
|
|
83
|
+
break;
|
|
84
|
+
case TagType.BOOL:
|
|
85
|
+
tag = new Bool(typeBytes, index, false);
|
|
86
|
+
break;
|
|
87
|
+
case TagType.STRING:
|
|
88
|
+
tag = new String(typeBytes, index, false);
|
|
89
|
+
break;
|
|
90
|
+
case TagType.ARRAY:
|
|
91
|
+
tag = new Array(typeBytes, index, false);
|
|
92
|
+
break;
|
|
93
|
+
case TagType.FL_ARRAY:
|
|
94
|
+
tag = new FixedLengthArray(typeBytes, index, false);
|
|
95
|
+
break;
|
|
96
|
+
case TagType.TUPLE:
|
|
97
|
+
tag = new Tuple(typeBytes, index, false);
|
|
98
|
+
break;
|
|
99
|
+
case TagType.ADDRESS:
|
|
100
|
+
tag = new Address(typeBytes, index, false);
|
|
101
|
+
break;
|
|
102
|
+
case TagType.FL_BYTES:
|
|
103
|
+
tag = new FixedLengthBytes(typeBytes, index, false);
|
|
104
|
+
break;
|
|
105
|
+
case TagType.BYTES:
|
|
106
|
+
tag = new Bytes(typeBytes, index, false);
|
|
107
|
+
break;
|
|
108
|
+
default:
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
if (tag != undefined && tag.cursor != undefined) {
|
|
112
|
+
// // console.log('cursor', tag)
|
|
113
|
+
cursor = tag.cursor;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
cursor,
|
|
118
|
+
tag
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
parserHolder.setParser(parse);
|
|
123
|
+
|
|
124
|
+
namespace DIDTag {
|
|
125
|
+
export type TagParser = Tag;
|
|
126
|
+
|
|
127
|
+
export type TagParserType = TagType;
|
|
128
|
+
|
|
129
|
+
export type TagParserBase = TagBase;
|
|
130
|
+
|
|
131
|
+
export type TagParserBox = Box;
|
|
132
|
+
|
|
133
|
+
export type ABITypeProviderHolder = ProviderHolder;
|
|
134
|
+
|
|
135
|
+
isHex;
|
|
136
|
+
|
|
137
|
+
readByte;
|
|
138
|
+
|
|
139
|
+
read2Byte;
|
|
140
|
+
|
|
141
|
+
export const doFormatType = (typeBytes: string): TagParser => {
|
|
142
|
+
return formatType(typeBytes);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const doEncode = (tag: TagParser, data: any) => {
|
|
146
|
+
return encode(tag, data);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export const TagValueTypeInt = Int;
|
|
150
|
+
|
|
151
|
+
export const TagValueTypeUint = Uint;
|
|
152
|
+
|
|
153
|
+
export const TagValueTypeBool = Bool;
|
|
154
|
+
|
|
155
|
+
export const TagValueTypeArray = Array;
|
|
156
|
+
|
|
157
|
+
export const TagValueTypeString = String;
|
|
158
|
+
|
|
159
|
+
export const TagValueTypeFlarray = FixedLengthArray;
|
|
160
|
+
|
|
161
|
+
export const TagValueTypeTuple = Tuple;
|
|
162
|
+
|
|
163
|
+
export const TagValueTypeAddress = Address;
|
|
164
|
+
|
|
165
|
+
export const TagValueTypeFlbytes = FixedLengthBytes;
|
|
166
|
+
|
|
167
|
+
export const TagValueTypeBytes = Bytes;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export default DIDTag;
|
package/src/tag/int.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Tag, TagBase, ProviderHolder } from './tag';
|
|
2
|
+
import { ethers } from 'ethers';
|
|
3
|
+
import ABITypeABI from '../abi/ABITypeABI';
|
|
4
|
+
export class Int extends TagBase {
|
|
5
|
+
formatType(typeBytes: any, index: number): number {
|
|
6
|
+
let bit = parseInt(typeBytes, 16);
|
|
7
|
+
bit = bit * 2 * 2 * 2;
|
|
8
|
+
if (bit < 8 || bit % 8 !== 0 || bit > 256) {
|
|
9
|
+
throw new Error(`input is not a valid int type`);
|
|
10
|
+
}
|
|
11
|
+
this.boxData = {
|
|
12
|
+
length: bit
|
|
13
|
+
};
|
|
14
|
+
return index + 4;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
getAbiTypeString(): string {
|
|
18
|
+
return `int${this.boxData.length}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
setSize = (size: number) => {
|
|
22
|
+
this.boxData.size = size;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
getAbiType = (providerHolder: ProviderHolder) =>
|
|
26
|
+
new Promise<void>(async (resolve, reject) => {
|
|
27
|
+
const c = new ethers.Contract(
|
|
28
|
+
providerHolder.contractAbiType,
|
|
29
|
+
ABITypeABI.abi,
|
|
30
|
+
providerHolder.getProvider()
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const resp = await c.intT(this.boxData.size);
|
|
34
|
+
// console.log('getAbiType', 'int', resp);
|
|
35
|
+
resolve(resp);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
getParamTypes(): string {
|
|
39
|
+
return `int${this.boxData.length}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getParameters(data: any) {
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
doHydration(d: any) {
|
|
47
|
+
return (d as BigInt).toString();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default Int;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Tag, TagBase, ProviderHolder } from './tag';
|
|
2
|
+
import { ethers } from 'ethers';
|
|
3
|
+
import ABITypeABI from '../abi/ABITypeABI';
|
|
4
|
+
export class String extends TagBase {
|
|
5
|
+
name: string = 'string';
|
|
6
|
+
|
|
7
|
+
formatType(typeBytes: any, index: number): number {
|
|
8
|
+
this.name = 'string';
|
|
9
|
+
return index + 2;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
getAbiTypeString(): string {
|
|
13
|
+
return 'string';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getAbiType = (providerHolder: ProviderHolder) =>
|
|
17
|
+
new Promise<void>(async (resolve, reject) => {
|
|
18
|
+
const c = new ethers.Contract(
|
|
19
|
+
providerHolder.contractAbiType,
|
|
20
|
+
ABITypeABI.abi,
|
|
21
|
+
providerHolder.getProvider()
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
const resp = await c.stringT();
|
|
25
|
+
// console.log('getAbiType', 'string', resp);
|
|
26
|
+
resolve(resp);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
getParamTypes(): string {
|
|
30
|
+
return 'string';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
getParameters(data: any) {
|
|
34
|
+
return data;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default String;
|
package/src/tag/tag.ts
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
|
|
3
|
+
export enum TagType {
|
|
4
|
+
INT = '00',
|
|
5
|
+
UINT = '01',
|
|
6
|
+
BOOL = '02',
|
|
7
|
+
STRING = '03',
|
|
8
|
+
ARRAY = '04',
|
|
9
|
+
FL_ARRAY = '05',
|
|
10
|
+
TUPLE = '06',
|
|
11
|
+
ADDRESS = '07',
|
|
12
|
+
FL_BYTES = '08',
|
|
13
|
+
BYTES = '09'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const needLength = (type: TagType) => {
|
|
17
|
+
return type == TagType.INT;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const read2Byte = (hex: string, index: number) => {
|
|
21
|
+
return hex.substring(index, index + 4);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const readByte = (hex: string, index: number) => {
|
|
25
|
+
return hex.substring(index, index + 2);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export interface ProviderHolder {
|
|
29
|
+
contractAbiType: string;
|
|
30
|
+
getProvider(): ethers.JsonRpcProvider;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface Tag {
|
|
34
|
+
formatType(typeBytes: any, index: number | undefined): number;
|
|
35
|
+
|
|
36
|
+
parseValue(data: any): any;
|
|
37
|
+
|
|
38
|
+
getParamTypes(): string;
|
|
39
|
+
|
|
40
|
+
getParameters(data: any): any;
|
|
41
|
+
|
|
42
|
+
cursor: number | undefined;
|
|
43
|
+
|
|
44
|
+
type: TagType | undefined;
|
|
45
|
+
|
|
46
|
+
boxData: any;
|
|
47
|
+
|
|
48
|
+
fieldName: string | undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type Parse = (
|
|
52
|
+
typeBytes: string,
|
|
53
|
+
index: number
|
|
54
|
+
) => { cursor: number; tag: Tag | undefined };
|
|
55
|
+
export class ParserHolder {
|
|
56
|
+
parse: Parse | undefined = undefined;
|
|
57
|
+
|
|
58
|
+
setParser = (parse: Parse) => {
|
|
59
|
+
this.parse = parse;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const parserHolder = new ParserHolder();
|
|
64
|
+
|
|
65
|
+
export abstract class TagBase implements Tag {
|
|
66
|
+
type: TagType | undefined;
|
|
67
|
+
cursor: number | undefined;
|
|
68
|
+
boxData: any;
|
|
69
|
+
fieldName: string | undefined;
|
|
70
|
+
|
|
71
|
+
abstract getAbiType(providerHolder: ProviderHolder): Promise<any>;
|
|
72
|
+
|
|
73
|
+
abstract getParamTypes(): string;
|
|
74
|
+
|
|
75
|
+
abstract getParameters(data: any): any; // 包装类负责解析对应字段,并验证字段是否存在, 基础类型负责验证数据格式正确性
|
|
76
|
+
|
|
77
|
+
getFieldNames = (): string[][] => {
|
|
78
|
+
if (this instanceof Box) {
|
|
79
|
+
let a: string[][] = this.getFieldName();
|
|
80
|
+
return a;
|
|
81
|
+
} else {
|
|
82
|
+
let a: string[][] = [];
|
|
83
|
+
return a;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
typeName = (): string => {
|
|
88
|
+
switch (this.type) {
|
|
89
|
+
case TagType.INT:
|
|
90
|
+
return `int${this.boxData.length}`;
|
|
91
|
+
case TagType.UINT:
|
|
92
|
+
return `uint${this.boxData.length}`;
|
|
93
|
+
case TagType.BOOL:
|
|
94
|
+
return `bool`;
|
|
95
|
+
case TagType.STRING:
|
|
96
|
+
return 'string';
|
|
97
|
+
case TagType.ARRAY:
|
|
98
|
+
return `array<${this.boxData.arrayTag.typeName()}>`;
|
|
99
|
+
case TagType.FL_ARRAY:
|
|
100
|
+
return `array<${this.boxData.arrayTag.typeName()}>(${
|
|
101
|
+
this.boxData.length
|
|
102
|
+
})`;
|
|
103
|
+
case TagType.TUPLE:
|
|
104
|
+
return 'tuple';
|
|
105
|
+
case TagType.ADDRESS:
|
|
106
|
+
return 'address';
|
|
107
|
+
case TagType.FL_BYTES:
|
|
108
|
+
return `bytes${this.boxData.length}`;
|
|
109
|
+
case TagType.BYTES:
|
|
110
|
+
return 'bytes';
|
|
111
|
+
default:
|
|
112
|
+
return 'error';
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
constructor(
|
|
117
|
+
typeBytes: string | undefined,
|
|
118
|
+
index: number | undefined,
|
|
119
|
+
builder: boolean
|
|
120
|
+
) {
|
|
121
|
+
this.boxData = {};
|
|
122
|
+
|
|
123
|
+
if (builder) {
|
|
124
|
+
} else {
|
|
125
|
+
if (typeBytes == undefined || index == undefined) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
"constructor value error, typeBytes and index can't be undefined"
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
let typeFlag = readByte(typeBytes, index);
|
|
132
|
+
|
|
133
|
+
switch (typeFlag) {
|
|
134
|
+
case TagType.INT:
|
|
135
|
+
this.type = TagType.INT;
|
|
136
|
+
this.cursor = this.formatType(
|
|
137
|
+
readByte(typeBytes, index + 2),
|
|
138
|
+
index
|
|
139
|
+
);
|
|
140
|
+
break;
|
|
141
|
+
case TagType.UINT:
|
|
142
|
+
this.type = TagType.UINT;
|
|
143
|
+
this.cursor = this.formatType(
|
|
144
|
+
readByte(typeBytes, index + 2),
|
|
145
|
+
index
|
|
146
|
+
);
|
|
147
|
+
break;
|
|
148
|
+
case TagType.BOOL:
|
|
149
|
+
this.type = TagType.BOOL;
|
|
150
|
+
this.cursor = this.formatType(typeBytes, index);
|
|
151
|
+
break;
|
|
152
|
+
case TagType.STRING:
|
|
153
|
+
this.type = TagType.STRING;
|
|
154
|
+
this.cursor = this.formatType(typeBytes, index);
|
|
155
|
+
break;
|
|
156
|
+
case TagType.ARRAY:
|
|
157
|
+
this.type = TagType.ARRAY;
|
|
158
|
+
this.cursor = this.formatType(typeBytes, index);
|
|
159
|
+
break;
|
|
160
|
+
case TagType.FL_ARRAY:
|
|
161
|
+
this.type = TagType.FL_ARRAY;
|
|
162
|
+
this.cursor = this.formatType(typeBytes, index);
|
|
163
|
+
break;
|
|
164
|
+
case TagType.TUPLE:
|
|
165
|
+
this.type = TagType.TUPLE;
|
|
166
|
+
this.cursor = this.formatType(typeBytes, index);
|
|
167
|
+
break;
|
|
168
|
+
case TagType.ADDRESS:
|
|
169
|
+
this.type = TagType.ADDRESS;
|
|
170
|
+
this.cursor = this.formatType(typeBytes, index);
|
|
171
|
+
break;
|
|
172
|
+
case TagType.FL_BYTES:
|
|
173
|
+
this.type = TagType.FL_BYTES;
|
|
174
|
+
this.cursor = this.formatType(typeBytes, index);
|
|
175
|
+
break;
|
|
176
|
+
case TagType.BYTES:
|
|
177
|
+
this.type = TagType.BYTES;
|
|
178
|
+
this.cursor = this.formatType(typeBytes, index);
|
|
179
|
+
break;
|
|
180
|
+
|
|
181
|
+
default:
|
|
182
|
+
throw new Error('formatType error');
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
// // console.log('cursor', this)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
abstract formatType(typeBytes: any, index: number | undefined): number;
|
|
190
|
+
|
|
191
|
+
parseValue(data: any) {
|
|
192
|
+
let abiCoder = new ethers.AbiCoder();
|
|
193
|
+
|
|
194
|
+
// // console.log('AbiTypeString', this.getAbiTypeString());
|
|
195
|
+
// // console.dir(this, { depth: null });
|
|
196
|
+
return abiCoder.decode([this.getAbiTypeString()], data, false);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
hydration(data: any) {
|
|
200
|
+
let d = this.parseValue(data);
|
|
201
|
+
|
|
202
|
+
return this.doHydration(d[0]);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
doHydration(d: any) {
|
|
206
|
+
return d;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
abstract getAbiTypeString(): string;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export abstract class Box extends TagBase {
|
|
213
|
+
abstract formatType(typeBytes: any, index: number | undefined): number;
|
|
214
|
+
|
|
215
|
+
abstract setFieldNames(names: string[] | string[][], index: number): number;
|
|
216
|
+
|
|
217
|
+
formatChild(
|
|
218
|
+
typeBytes: any,
|
|
219
|
+
index: number
|
|
220
|
+
): { cursor: number; tag: Tag | undefined } {
|
|
221
|
+
if (!parserHolder.parse) throw new Error('not found parse');
|
|
222
|
+
|
|
223
|
+
return parserHolder.parse(typeBytes, index);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
abstract getFieldName(): string[][];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export default Tag;
|
package/src/tag/tuple.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { Tag, TagBase, Box, read2Byte, ProviderHolder } from './tag';
|
|
2
|
+
import { ethers } from 'ethers';
|
|
3
|
+
import ABITypeABI from '../abi/ABITypeABI';
|
|
4
|
+
export class Tuple extends Box {
|
|
5
|
+
constructor(
|
|
6
|
+
typeBytes: string | undefined,
|
|
7
|
+
index: number | undefined,
|
|
8
|
+
builder: boolean
|
|
9
|
+
) {
|
|
10
|
+
super(typeBytes, index, builder);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
formatType(typeBytes: any, index: number): number {
|
|
14
|
+
let lt = read2Byte(typeBytes, index + 2);
|
|
15
|
+
let length = parseInt(lt, 16);
|
|
16
|
+
|
|
17
|
+
let parsedLen = index + 6;
|
|
18
|
+
let tagArr: Tag[] = [];
|
|
19
|
+
for (let k = 0; k < length; k++) {
|
|
20
|
+
let { cursor, tag } = this.formatChild(typeBytes, parsedLen);
|
|
21
|
+
parsedLen = cursor;
|
|
22
|
+
if (tag == undefined) {
|
|
23
|
+
throw new Error('formatType error in tuple');
|
|
24
|
+
}
|
|
25
|
+
tagArr.push(tag);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this.boxData = {
|
|
29
|
+
tags: tagArr as TagBase[]
|
|
30
|
+
};
|
|
31
|
+
// this.boxData.tags = tagArr as TagBase[]
|
|
32
|
+
|
|
33
|
+
// console.log('this.boxData', this.boxData);
|
|
34
|
+
return parsedLen;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getAbiTypeString(): string {
|
|
38
|
+
let str = '';
|
|
39
|
+
for (const tag of this.boxData.tags) {
|
|
40
|
+
// // console.log (tag)
|
|
41
|
+
if (tag.fieldName == undefined) {
|
|
42
|
+
str = str + ',' + tag.getAbiTypeString();
|
|
43
|
+
} else {
|
|
44
|
+
str = str + ',' + `${tag.getAbiTypeString()} ${tag.fieldName}`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return `tuple(${str.substring(1)})`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
setFieldNames(names: string[] | string[][], index: number): number {
|
|
52
|
+
// console.log('setFieldNames');
|
|
53
|
+
// console.log(names);
|
|
54
|
+
// console.log(this.boxData.tags);
|
|
55
|
+
|
|
56
|
+
let fieldNames = names[index];
|
|
57
|
+
index++;
|
|
58
|
+
|
|
59
|
+
let indexInBox = 0;
|
|
60
|
+
for (const tag of this.boxData.tags) {
|
|
61
|
+
tag.fieldName = fieldNames[indexInBox];
|
|
62
|
+
indexInBox++;
|
|
63
|
+
|
|
64
|
+
// console.log('set FieldNames');
|
|
65
|
+
// console.log(tag);
|
|
66
|
+
// console.log('indexInBox', indexInBox);
|
|
67
|
+
|
|
68
|
+
if (tag instanceof Box) {
|
|
69
|
+
// // console.log('in Box')
|
|
70
|
+
// // console.log(names [index])
|
|
71
|
+
// // console.log(tag)
|
|
72
|
+
// // console.log(names)
|
|
73
|
+
|
|
74
|
+
// fix build
|
|
75
|
+
// if (indexInBox >= names.length) {
|
|
76
|
+
// indexInBox = index
|
|
77
|
+
// }
|
|
78
|
+
|
|
79
|
+
tag.setFieldNames(names, indexInBox);
|
|
80
|
+
index++;
|
|
81
|
+
}
|
|
82
|
+
// if (tag instanceof Box) {
|
|
83
|
+
// // console.log(tag)
|
|
84
|
+
// // console.log(this)
|
|
85
|
+
// throw new Error('tuple not support 2 level and more')
|
|
86
|
+
// }
|
|
87
|
+
}
|
|
88
|
+
return index;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
doHydration(d: any) {
|
|
92
|
+
// console.log('doHydration');
|
|
93
|
+
// console.log(d);
|
|
94
|
+
// console.log(this);
|
|
95
|
+
|
|
96
|
+
let i = 0;
|
|
97
|
+
let data: { [key: string]: any } = {};
|
|
98
|
+
for (const tag of this.boxData.tags) {
|
|
99
|
+
// console.log(tag);
|
|
100
|
+
// console.log(d);
|
|
101
|
+
// console.log(i);
|
|
102
|
+
|
|
103
|
+
data[`${tag.fieldName as string}`] = tag.doHydration(d[i]);
|
|
104
|
+
i++;
|
|
105
|
+
}
|
|
106
|
+
return data;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
setField(name: string, type: TagBase) {
|
|
110
|
+
type.fieldName = name;
|
|
111
|
+
|
|
112
|
+
if (this.boxData.tags == undefined) {
|
|
113
|
+
this.boxData.tags = [];
|
|
114
|
+
}
|
|
115
|
+
this.boxData.tags.push(type);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getAbiType = (providerHolder: ProviderHolder) =>
|
|
119
|
+
new Promise<void>(async (resolve, reject) => {
|
|
120
|
+
const c = new ethers.Contract(
|
|
121
|
+
providerHolder.contractAbiType,
|
|
122
|
+
ABITypeABI.abi,
|
|
123
|
+
providerHolder.getProvider()
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
let arr: string = '0x';
|
|
127
|
+
for (const tag of this.boxData.tags) {
|
|
128
|
+
let r = await tag.getAbiType(providerHolder);
|
|
129
|
+
arr += r.substring(2);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// console.log('arr', arr);
|
|
133
|
+
const resp = await c.tupleT(arr);
|
|
134
|
+
// console.log('getAbiType', 'Tuple', resp);
|
|
135
|
+
resolve(resp);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
getFieldName(): string[][] {
|
|
139
|
+
let arr: string[][] = [];
|
|
140
|
+
let names: string[] = [];
|
|
141
|
+
for (const tag of this.boxData.tags) {
|
|
142
|
+
let tagFieldNames = tag.getFieldNames();
|
|
143
|
+
if (tagFieldNames.length > 0) {
|
|
144
|
+
arr.push(...tagFieldNames);
|
|
145
|
+
// arr = Object.assign(arr, tagFieldNames)
|
|
146
|
+
}
|
|
147
|
+
names.push(tag.fieldName as string);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let arr2: string[][] = [];
|
|
151
|
+
arr2.push(names);
|
|
152
|
+
// arr2 = Object.assign(arr2, arr)
|
|
153
|
+
arr2.push(...arr);
|
|
154
|
+
|
|
155
|
+
// console.log('arr', arr2);
|
|
156
|
+
|
|
157
|
+
return arr2;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
getParamTypes(): string {
|
|
161
|
+
let paramTypes = 'tuple(';
|
|
162
|
+
|
|
163
|
+
let i = 0;
|
|
164
|
+
for (const t of this.boxData.tags) {
|
|
165
|
+
paramTypes += t.getParamTypes();
|
|
166
|
+
|
|
167
|
+
i++;
|
|
168
|
+
if (i >= this.boxData.tags.length) {
|
|
169
|
+
} else {
|
|
170
|
+
paramTypes += ',';
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
paramTypes += ')';
|
|
174
|
+
|
|
175
|
+
return paramTypes;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
getParameters(data: any) {
|
|
179
|
+
const values = [];
|
|
180
|
+
let i = 0;
|
|
181
|
+
// console.log('data', data);
|
|
182
|
+
// console.log('this.boxData.tags', this.boxData.tags);
|
|
183
|
+
for (const t of this.boxData.tags) {
|
|
184
|
+
// console.log(t, t.fieldName, data[t.fieldName]);
|
|
185
|
+
|
|
186
|
+
values.push(t.getParameters(data[t.fieldName]));
|
|
187
|
+
i++;
|
|
188
|
+
}
|
|
189
|
+
return values;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export default Tuple;
|