koilib 2.2.0 → 2.5.0
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/README.md +17 -8
- package/dist/koinos.js +5862 -5750
- package/dist/koinos.min.js +1 -1
- package/dist/koinos.min.js.LICENSE.txt +1 -10
- package/lib/Contract.d.ts +47 -152
- package/lib/Contract.js +61 -148
- package/lib/Contract.js.map +1 -1
- package/lib/Provider.d.ts +8 -9
- package/lib/Provider.js +67 -16
- package/lib/Provider.js.map +1 -1
- package/lib/Serializer.d.ts +81 -0
- package/lib/Serializer.js +169 -0
- package/lib/Serializer.js.map +1 -0
- package/lib/Signer.d.ts +130 -24
- package/lib/Signer.js +162 -59
- package/lib/Signer.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/index2.js +2 -0
- package/lib/index2.js.map +1 -1
- package/lib/interface.d.ts +179 -24
- package/lib/{krc20-proto.json → jsonDescriptors/krc20-proto.json} +1 -1
- package/lib/{protocol-proto.json → jsonDescriptors/protocol-proto.json} +22 -18
- package/lib/utils.d.ts +250 -2
- package/lib/utils.js +15 -20
- package/lib/utils.js.map +1 -1
- package/package.json +7 -9
package/lib/Contract.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Contract.js","sourceRoot":"","sources":["../src/Contract.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"Contract.js","sourceRoot":"","sources":["../src/Contract.ts"],"names":[],"mappings":";;;AAGA,6CAA0C;AAU1C,mCAAmE;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,MAAa,QAAQ;IA6DnB,YAAY,CAcX;;QACC,IAAI,CAAC,CAAC,EAAE;YAAE,IAAI,CAAC,EAAE,GAAG,oBAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACvC,IAAI,CAAC,MA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ransactionResponse {
|
|
3
|
-
wait: () => Promise<string>;
|
|
4
|
-
}
|
|
1
|
+
import { BlockJson, TransactionJson, CallContractOperationJson, SendTransactionResponse } from "./interface";
|
|
5
2
|
/**
|
|
6
3
|
* Class to connect with the RPC node
|
|
7
4
|
*/
|
|
@@ -92,7 +89,7 @@ export declare class Provider {
|
|
|
92
89
|
height: string;
|
|
93
90
|
previous: string;
|
|
94
91
|
};
|
|
95
|
-
|
|
92
|
+
last_irreversible_block: string;
|
|
96
93
|
}>;
|
|
97
94
|
/**
|
|
98
95
|
* Function to get consecutive blocks in descending order
|
|
@@ -104,7 +101,7 @@ export declare class Provider {
|
|
|
104
101
|
*/
|
|
105
102
|
getBlocks(height: number, numBlocks?: number, idRef?: string): Promise<{
|
|
106
103
|
block_id: string;
|
|
107
|
-
block_height:
|
|
104
|
+
block_height: string;
|
|
108
105
|
block: BlockJson;
|
|
109
106
|
block_receipt: {
|
|
110
107
|
[x: string]: unknown;
|
|
@@ -115,7 +112,7 @@ export declare class Provider {
|
|
|
115
112
|
*/
|
|
116
113
|
getBlock(height: number): Promise<{
|
|
117
114
|
block_id: string;
|
|
118
|
-
block_height:
|
|
115
|
+
block_height: string;
|
|
119
116
|
block: BlockJson;
|
|
120
117
|
block_receipt: {
|
|
121
118
|
[x: string]: unknown;
|
|
@@ -125,7 +122,7 @@ export declare class Provider {
|
|
|
125
122
|
* Function to call "chain.submit_transaction" to send a signed
|
|
126
123
|
* transaction to the blockchain. It returns an object with the async
|
|
127
124
|
* function "wait", which can be called to wait for the
|
|
128
|
-
* transaction to be mined.
|
|
125
|
+
* transaction to be mined (see [[SendTransactionResponse]]).
|
|
129
126
|
* @param transaction - Signed transaction
|
|
130
127
|
* @example
|
|
131
128
|
* ```ts
|
|
@@ -136,7 +133,9 @@ export declare class Provider {
|
|
|
136
133
|
* });
|
|
137
134
|
* console.log("Transaction submitted to the mempool");
|
|
138
135
|
* // wait to be mined
|
|
139
|
-
* const
|
|
136
|
+
* const blockNumber = await transactionResponse.wait();
|
|
137
|
+
* // const blockNumber = await transactionResponse.wait("byBlock", 30000);
|
|
138
|
+
* // const blockId = await transactionResponse.wait("byTransactionId", 30000);
|
|
140
139
|
* console.log("Transaction mined")
|
|
141
140
|
* ```
|
|
142
141
|
*/
|
package/lib/Provider.js
CHANGED
|
@@ -78,7 +78,10 @@ class Provider {
|
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
if (response.data.error)
|
|
81
|
-
throw new Error(
|
|
81
|
+
throw new Error(JSON.stringify({
|
|
82
|
+
error: response.data.error,
|
|
83
|
+
request: { method, params },
|
|
84
|
+
}));
|
|
82
85
|
return response.data.result;
|
|
83
86
|
}
|
|
84
87
|
/**
|
|
@@ -155,7 +158,7 @@ class Provider {
|
|
|
155
158
|
* Function to call "chain.submit_transaction" to send a signed
|
|
156
159
|
* transaction to the blockchain. It returns an object with the async
|
|
157
160
|
* function "wait", which can be called to wait for the
|
|
158
|
-
* transaction to be mined.
|
|
161
|
+
* transaction to be mined (see [[SendTransactionResponse]]).
|
|
159
162
|
* @param transaction - Signed transaction
|
|
160
163
|
* @example
|
|
161
164
|
* ```ts
|
|
@@ -166,28 +169,76 @@ class Provider {
|
|
|
166
169
|
* });
|
|
167
170
|
* console.log("Transaction submitted to the mempool");
|
|
168
171
|
* // wait to be mined
|
|
169
|
-
* const
|
|
172
|
+
* const blockNumber = await transactionResponse.wait();
|
|
173
|
+
* // const blockNumber = await transactionResponse.wait("byBlock", 30000);
|
|
174
|
+
* // const blockId = await transactionResponse.wait("byTransactionId", 30000);
|
|
170
175
|
* console.log("Transaction mined")
|
|
171
176
|
* ```
|
|
172
177
|
*/
|
|
173
178
|
async sendTransaction(transaction) {
|
|
174
179
|
await this.call("chain.submit_transaction", { transaction });
|
|
175
|
-
const startTime = Date.now() + 10000;
|
|
176
180
|
return {
|
|
177
|
-
wait: async () => {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
+
wait: async (type = "byBlock", timeout = 30000) => {
|
|
182
|
+
const iniTime = Date.now();
|
|
183
|
+
if (type === "byTransactionId") {
|
|
184
|
+
while (Date.now() < iniTime + timeout) {
|
|
185
|
+
await sleep(1000);
|
|
186
|
+
const { transactions } = await this.getTransactionsById([
|
|
187
|
+
transaction.id,
|
|
188
|
+
]);
|
|
189
|
+
if (transactions &&
|
|
190
|
+
transactions[0] &&
|
|
191
|
+
transactions[0].containing_blocks)
|
|
192
|
+
return transactions[0].containing_blocks[0];
|
|
193
|
+
}
|
|
194
|
+
throw new Error(`Transaction not mined after ${timeout} ms`);
|
|
195
|
+
}
|
|
196
|
+
// byBlock
|
|
197
|
+
const findTxInBlocks = async (ini, numBlocks, idRef) => {
|
|
198
|
+
const blocks = await this.getBlocks(ini, numBlocks, idRef);
|
|
199
|
+
let bNum = 0;
|
|
200
|
+
blocks.forEach((block) => {
|
|
201
|
+
if (!block ||
|
|
202
|
+
!block.block ||
|
|
203
|
+
!block.block_id ||
|
|
204
|
+
!block.block.transactions)
|
|
205
|
+
return;
|
|
206
|
+
const tx = block.block.transactions.find((t) => t.id === transaction.id);
|
|
207
|
+
if (tx)
|
|
208
|
+
bNum = Number(block.block_height);
|
|
209
|
+
});
|
|
210
|
+
let lastId = blocks[blocks.length - 1].block_id;
|
|
211
|
+
return [bNum, lastId];
|
|
212
|
+
};
|
|
213
|
+
let blockNumber = 0;
|
|
214
|
+
let iniBlock = 0;
|
|
215
|
+
let previousId = "";
|
|
216
|
+
while (Date.now() < iniTime + timeout) {
|
|
181
217
|
await sleep(1000);
|
|
182
|
-
const {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
218
|
+
const { head_topology: headTopology } = await this.getHeadInfo();
|
|
219
|
+
if (blockNumber === 0) {
|
|
220
|
+
blockNumber = Number(headTopology.height);
|
|
221
|
+
iniBlock = blockNumber;
|
|
222
|
+
}
|
|
223
|
+
if (Number(headTopology.height) === blockNumber - 1 &&
|
|
224
|
+
previousId &&
|
|
225
|
+
previousId !== headTopology.id) {
|
|
226
|
+
const [bNum, lastId] = await findTxInBlocks(iniBlock, Number(headTopology.height) - iniBlock + 1, headTopology.id);
|
|
227
|
+
if (bNum)
|
|
228
|
+
return bNum;
|
|
229
|
+
previousId = lastId;
|
|
230
|
+
blockNumber = Number(headTopology.height) + 1;
|
|
231
|
+
}
|
|
232
|
+
if (blockNumber > Number(headTopology.height))
|
|
233
|
+
continue;
|
|
234
|
+
const [bNum, lastId] = await findTxInBlocks(blockNumber, 1, headTopology.id);
|
|
235
|
+
if (bNum)
|
|
236
|
+
return bNum;
|
|
237
|
+
if (!previousId)
|
|
238
|
+
previousId = lastId;
|
|
239
|
+
blockNumber += 1;
|
|
189
240
|
}
|
|
190
|
-
throw new Error(`Transaction not mined after
|
|
241
|
+
throw new Error(`Transaction not mined after ${timeout} ms. Blocks checked from ${iniBlock} to ${blockNumber}`);
|
|
191
242
|
},
|
|
192
243
|
};
|
|
193
244
|
}
|
package/lib/Provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Provider.js","sourceRoot":"","sources":["../src/Provider.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA6C;
|
|
1
|
+
{"version":3,"file":"Provider.js","sourceRoot":"","sources":["../src/Provider.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA6C;AAQ7C,KAAK,UAAU,KAAK,CAAC,EAAU;IAC7B,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AAC/C,CAAC;AAED;;GAEG;AACH,MAAa,QAAQ;IAwCnB;;;;;;;;;;;OAWG;IACH,YAAY,QAA2B;QACrC,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC;YAAE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;;YACjD,IAAI,CAAC,QAAQ,GAAG,CAAC,QAAQ,CAAC,CAAC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AC;iBAClB;gBACD,MAAM,IAAI,KAAK,CACb,+BAA+B,OAAO,4BAA4B,QAAQ,OAAO,WAAW,EAAE,CAC/F,CAAC;YACJ,CAAC;SACF,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,YAAY,CAAC,SAAoC;QAIrD,OAAO,IAAI,CAAC,IAAI,CAAC,qBAAqB,EAAE,SAAS,CAAC,CAAC;IACrD,CAAC;CACF;AAnYD,4BAmYC;AAED,kBAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Root, Type, INamespace } from "protobufjs/light";
|
|
2
|
+
/**
|
|
3
|
+
* The serializer class serialize and deserialize data using
|
|
4
|
+
* protocol buffers.
|
|
5
|
+
*
|
|
6
|
+
* NOTE: This class uses the [protobufjs/light](https://www.npmjs.com/package/protobufjs)
|
|
7
|
+
* library internally, which uses reflection (use of _eval_
|
|
8
|
+
* and _new Function_) for the construction of the types.
|
|
9
|
+
* This could cause issues in environments where _eval_ is not
|
|
10
|
+
* allowed, like in browser extensions. In such cases, this class
|
|
11
|
+
* must be confined in a [sandbox environment](https://developer.chrome.com/docs/apps/app_external/#sandboxing)
|
|
12
|
+
* where _eval_ is allowed. This is the principal reason of
|
|
13
|
+
* having the serializer in a separate class.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* const descriptorJson = {
|
|
19
|
+
* nested: {
|
|
20
|
+
* awesomepackage: {
|
|
21
|
+
* nested: {
|
|
22
|
+
* AwesomeMessage: {
|
|
23
|
+
* fields: {
|
|
24
|
+
* awesomeField: {
|
|
25
|
+
* type: "string",
|
|
26
|
+
* id: 1
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* const serializer = new Serializer(descriptorJson)
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare class Serializer {
|
|
38
|
+
/**
|
|
39
|
+
* Protobuffers descriptor in JSON format.
|
|
40
|
+
* See https://www.npmjs.com/package/protobufjs#using-json-descriptors
|
|
41
|
+
*/
|
|
42
|
+
types: INamespace;
|
|
43
|
+
/**
|
|
44
|
+
* Protobuffer definitions
|
|
45
|
+
*/
|
|
46
|
+
root: Root;
|
|
47
|
+
/**
|
|
48
|
+
* Default type for all serializations
|
|
49
|
+
*/
|
|
50
|
+
defaultType?: Type;
|
|
51
|
+
/**
|
|
52
|
+
* Preformat bytes for base64, base58 or hex string
|
|
53
|
+
*/
|
|
54
|
+
bytesConversion: boolean;
|
|
55
|
+
constructor(types: INamespace, opts?: {
|
|
56
|
+
/**
|
|
57
|
+
* Default type name. Use this option when you
|
|
58
|
+
* always want to serialize/deserialize the same type
|
|
59
|
+
*/
|
|
60
|
+
defaultTypeName?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Bytes conversion. Option to preformat bytes
|
|
63
|
+
* when "(koinos_bytes_type)" is defined in the type
|
|
64
|
+
* definitions. By default it is true.
|
|
65
|
+
*/
|
|
66
|
+
bytesConversion?: boolean;
|
|
67
|
+
});
|
|
68
|
+
/**
|
|
69
|
+
* Function to encode a type using the protobuffer definitions
|
|
70
|
+
* It also prepares the bytes for special cases (base58, hex string)
|
|
71
|
+
* when bytesConversion param is true.
|
|
72
|
+
*/
|
|
73
|
+
serialize(valueDecoded: Record<string, unknown>, typeName?: string): Promise<Uint8Array>;
|
|
74
|
+
/**
|
|
75
|
+
* Function to decode bytes using the protobuffer definitions
|
|
76
|
+
* It also encodes the bytes for special cases (base58, hex string)
|
|
77
|
+
* when bytesConversion param is true.
|
|
78
|
+
*/
|
|
79
|
+
deserialize<T = Record<string, unknown>>(valueEncoded: string | Uint8Array, typeName?: string): Promise<T>;
|
|
80
|
+
}
|
|
81
|
+
export default Serializer;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Serializer = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/require-await */
|
|
5
|
+
const light_1 = require("protobufjs/light");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
const OP_BYTES = "(koinos_bytes_type)";
|
|
8
|
+
/**
|
|
9
|
+
* Makes a copy of a value. The returned value can be modified
|
|
10
|
+
* without altering the original one. Although this is not needed
|
|
11
|
+
* for strings or numbers and only needed for objects and arrays,
|
|
12
|
+
* all these options are covered in a single function
|
|
13
|
+
*
|
|
14
|
+
* It is assumed that the argument is number, string, or contructions
|
|
15
|
+
* of these types inside objects or arrays.
|
|
16
|
+
*/
|
|
17
|
+
function copyValue(value) {
|
|
18
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
return JSON.parse(JSON.stringify(value));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The serializer class serialize and deserialize data using
|
|
25
|
+
* protocol buffers.
|
|
26
|
+
*
|
|
27
|
+
* NOTE: This class uses the [protobufjs/light](https://www.npmjs.com/package/protobufjs)
|
|
28
|
+
* library internally, which uses reflection (use of _eval_
|
|
29
|
+
* and _new Function_) for the construction of the types.
|
|
30
|
+
* This could cause issues in environments where _eval_ is not
|
|
31
|
+
* allowed, like in browser extensions. In such cases, this class
|
|
32
|
+
* must be confined in a [sandbox environment](https://developer.chrome.com/docs/apps/app_external/#sandboxing)
|
|
33
|
+
* where _eval_ is allowed. This is the principal reason of
|
|
34
|
+
* having the serializer in a separate class.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* const descriptorJson = {
|
|
40
|
+
* nested: {
|
|
41
|
+
* awesomepackage: {
|
|
42
|
+
* nested: {
|
|
43
|
+
* AwesomeMessage: {
|
|
44
|
+
* fields: {
|
|
45
|
+
* awesomeField: {
|
|
46
|
+
* type: "string",
|
|
47
|
+
* id: 1
|
|
48
|
+
* }
|
|
49
|
+
* }
|
|
50
|
+
* }
|
|
51
|
+
* }
|
|
52
|
+
* }
|
|
53
|
+
* }
|
|
54
|
+
* }
|
|
55
|
+
* const serializer = new Serializer(descriptorJson)
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
class Serializer {
|
|
59
|
+
constructor(types, opts) {
|
|
60
|
+
/**
|
|
61
|
+
* Preformat bytes for base64, base58 or hex string
|
|
62
|
+
*/
|
|
63
|
+
this.bytesConversion = true;
|
|
64
|
+
this.types = types;
|
|
65
|
+
this.root = light_1.Root.fromJSON(this.types);
|
|
66
|
+
if (opts === null || opts === void 0 ? void 0 : opts.defaultTypeName)
|
|
67
|
+
this.defaultType = this.root.lookupType(opts.defaultTypeName);
|
|
68
|
+
if (opts && typeof opts.bytesConversion !== "undefined")
|
|
69
|
+
this.bytesConversion = opts.bytesConversion;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Function to encode a type using the protobuffer definitions
|
|
73
|
+
* It also prepares the bytes for special cases (base58, hex string)
|
|
74
|
+
* when bytesConversion param is true.
|
|
75
|
+
*/
|
|
76
|
+
async serialize(valueDecoded, typeName) {
|
|
77
|
+
const protobufType = this.defaultType || this.root.lookupType(typeName);
|
|
78
|
+
let object = {};
|
|
79
|
+
if (this.bytesConversion) {
|
|
80
|
+
// TODO: format from Buffer to base58/base64 for nested fields
|
|
81
|
+
Object.keys(protobufType.fields).forEach((fieldName) => {
|
|
82
|
+
const { options, name, type } = protobufType.fields[fieldName];
|
|
83
|
+
// No byte conversion
|
|
84
|
+
if (type !== "bytes") {
|
|
85
|
+
object[name] = copyValue(valueDecoded[name]);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
// Default byte conversion
|
|
89
|
+
if (!options || !options[OP_BYTES]) {
|
|
90
|
+
object[name] = utils_1.decodeBase64(valueDecoded[name]);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
// Specific byte conversion
|
|
94
|
+
switch (options[OP_BYTES]) {
|
|
95
|
+
case "BASE58":
|
|
96
|
+
case "CONTRACT_ID":
|
|
97
|
+
case "ADDRESS":
|
|
98
|
+
object[name] = utils_1.decodeBase58(valueDecoded[name]);
|
|
99
|
+
break;
|
|
100
|
+
case "BASE64":
|
|
101
|
+
object[name] = utils_1.decodeBase64(valueDecoded[name]);
|
|
102
|
+
break;
|
|
103
|
+
case "HEX":
|
|
104
|
+
case "BLOCK_ID":
|
|
105
|
+
case "TRANSACTION_ID":
|
|
106
|
+
object[name] = utils_1.toUint8Array(valueDecoded[name].replace("0x", ""));
|
|
107
|
+
break;
|
|
108
|
+
default:
|
|
109
|
+
throw new Error(`unknown koinos_byte_type ${options[OP_BYTES]}`);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
object = valueDecoded;
|
|
115
|
+
}
|
|
116
|
+
const message = protobufType.create(object);
|
|
117
|
+
const buffer = protobufType.encode(message).finish();
|
|
118
|
+
return buffer;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Function to decode bytes using the protobuffer definitions
|
|
122
|
+
* It also encodes the bytes for special cases (base58, hex string)
|
|
123
|
+
* when bytesConversion param is true.
|
|
124
|
+
*/
|
|
125
|
+
async deserialize(valueEncoded, typeName) {
|
|
126
|
+
const valueBuffer = typeof valueEncoded === "string"
|
|
127
|
+
? utils_1.decodeBase64(valueEncoded)
|
|
128
|
+
: valueEncoded;
|
|
129
|
+
const protobufType = this.defaultType || this.root.lookupType(typeName);
|
|
130
|
+
const message = protobufType.decode(valueBuffer);
|
|
131
|
+
const object = protobufType.toObject(message, { longs: String });
|
|
132
|
+
if (!this.bytesConversion)
|
|
133
|
+
return object;
|
|
134
|
+
// TODO: format from Buffer to base58/base64 for nested fields
|
|
135
|
+
Object.keys(protobufType.fields).forEach((fieldName) => {
|
|
136
|
+
const { options, name, type } = protobufType.fields[fieldName];
|
|
137
|
+
// No byte conversion
|
|
138
|
+
if (type !== "bytes")
|
|
139
|
+
return;
|
|
140
|
+
// Default byte conversion
|
|
141
|
+
if (!options || !options[OP_BYTES]) {
|
|
142
|
+
object[name] = utils_1.encodeBase64(object[name]);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
// Specific byte conversion
|
|
146
|
+
switch (options[OP_BYTES]) {
|
|
147
|
+
case "BASE58":
|
|
148
|
+
case "CONTRACT_ID":
|
|
149
|
+
case "ADDRESS":
|
|
150
|
+
object[name] = utils_1.encodeBase58(object[name]);
|
|
151
|
+
break;
|
|
152
|
+
case "BASE64":
|
|
153
|
+
object[name] = utils_1.encodeBase64(object[name]);
|
|
154
|
+
break;
|
|
155
|
+
case "HEX":
|
|
156
|
+
case "BLOCK_ID":
|
|
157
|
+
case "TRANSACTION_ID":
|
|
158
|
+
object[name] = `0x${utils_1.toHexString(object[name])}`;
|
|
159
|
+
break;
|
|
160
|
+
default:
|
|
161
|
+
throw new Error(`unknown koinos_byte_type ${options[OP_BYTES]}`);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
return object;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
exports.Serializer = Serializer;
|
|
168
|
+
exports.default = Serializer;
|
|
169
|
+
//# sourceMappingURL=Serializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Serializer.js","sourceRoot":"","sources":["../src/Serializer.ts"],"names":[],"mappings":";;;AAAA,qDAAqD;AACrD,4CAA0D;AAC1D,mCAOiB;AAEjB,MAAM,QAAQ,GAAG,qBAAqB,CAAC;AAEvC;;;;;;;;GAQG;AACH,SAAS,SAAS,CAAC,KAAc;IAC/B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QAC1D,OAAO,KAAK,CAAC;KACd;IACD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAA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