koilib 2.0.0 → 2.4.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 +49 -7
- package/dist/koinos.js +4910 -4762
- package/dist/koinos.min.js +1 -1
- package/dist/koinos.min.js.LICENSE.txt +1 -10
- package/lib/Contract.d.ts +48 -140
- package/lib/Contract.js +82 -156
- package/lib/Contract.js.map +1 -1
- package/lib/Provider.d.ts +2 -5
- package/lib/Provider.js +42 -11
- 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 +161 -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 +270 -2
- package/lib/utils.js +72 -34
- package/lib/utils.js.map +1 -1
- package/package.json +9 -5
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
|
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
|
/**
|
|
@@ -174,20 +177,48 @@ class Provider {
|
|
|
174
177
|
await this.call("chain.submit_transaction", { transaction });
|
|
175
178
|
const startTime = Date.now() + 10000;
|
|
176
179
|
return {
|
|
177
|
-
wait: async () => {
|
|
180
|
+
wait: async (type = "byTransactionId") => {
|
|
178
181
|
// sleep some seconds before it gets mined
|
|
179
182
|
await sleep(startTime - Date.now() - 1000);
|
|
180
|
-
|
|
183
|
+
if (type === "byTransactionId") {
|
|
184
|
+
for (let i = 0; i < 30; i += 1) {
|
|
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 40 seconds`);
|
|
195
|
+
}
|
|
196
|
+
// byBlock
|
|
197
|
+
let blockNumber = 0;
|
|
198
|
+
let iniBlock = 0;
|
|
199
|
+
for (let i = 0; i < 90; i += 1) {
|
|
181
200
|
await sleep(1000);
|
|
182
|
-
const {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
201
|
+
const { head_topology: headTopology } = await this.getHeadInfo();
|
|
202
|
+
if (i === 0) {
|
|
203
|
+
blockNumber = Number(headTopology.height);
|
|
204
|
+
iniBlock = blockNumber;
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
blockNumber += 1;
|
|
208
|
+
}
|
|
209
|
+
if (blockNumber > Number(headTopology.height))
|
|
210
|
+
continue;
|
|
211
|
+
const [block] = await this.getBlocks(blockNumber, 1, headTopology.id);
|
|
212
|
+
if (!block ||
|
|
213
|
+
!block.block ||
|
|
214
|
+
!block.block_id ||
|
|
215
|
+
!block.block.transactions)
|
|
216
|
+
continue;
|
|
217
|
+
const tx = block.block.transactions.find((t) => t.id === transaction.id);
|
|
218
|
+
if (tx)
|
|
219
|
+
return blockNumber.toString();
|
|
189
220
|
}
|
|
190
|
-
throw new Error(`Transaction not mined
|
|
221
|
+
throw new Error(`Transaction not mined from block ${iniBlock} to ${blockNumber}`);
|
|
191
222
|
},
|
|
192
223
|
};
|
|
193
224
|
}
|
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;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M,CAAC,YAAY,CAAC,MAAM,CAAC;wBAAE,SAAS;oBACxD,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC,EAAE,YAAY,CAAC,EAAE,CAAC,CAAC;oBACtE,IACE,CAAC,KAAK;wBACN,CAAC,KAAK,CAAC,KAAK;wBACZ,CAAC,KAAK,CAAC,QAAQ;wBACf,CAAC,KAAK,CAAC,KAAK,CAAC,YAAY;wBAEzB,SAAS;oBACX,MAAM,EAAE,GAAG,KAAK,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,CACtC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,WAAW,CAAC,EAAE,CAC/B,CAAC;oBACF,IAAI,EAAE;wBAAE,OAAO,WAAW,CAAC,QAAQ,EAAE,CAAC;iBACvC;gBACD,MAAM,IAAI,KAAK,CACb,oCAAoC,QAAQ,OAAO,WAAW,EAAE,CACjE,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;AA9VD,4BA8VC;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,CAAY,CAAC;AACtD,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAa,UAAU;IAsBrB,YACE,KAAiB,EACjB,IAaC;QApBH;;WAEG;QACH,oBAAe,GAAG,IAAI,CAAC;QAmBrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,YAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACtC,IAAI,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,eAAe;YACvB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;QAChE,IAAI,IAAI,IAAI,OAAO,IAAI,CAAC,eAAe,KAAK,WAAW;YACrD,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC;IAChD,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,SAAS,CACb,YAAqC,EACrC,QAAiB;QAEjB,MAAM,YAAY,GAChB,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,IAAI,CAAC,UAAU,CAA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AAM,CAAC,IAAI,CAAC,GAAG,oBAAY,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;gBAC1C,OAAO;aACR;YAED,2BAA2B;YAC3B,QAAQ,OAAO,CAAC,QAAQ,CAAC,EAAE;gBACzB,KAAK,QAAQ,CAAC;gBACd,KAAK,aAAa,CAAC;gBACnB,KAAK,SAAS;oBACZ,MAAM,CAAC,IAAI,CAAC,GAAG,oBAAY,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;oBAC1C,MAAM;gBACR,KAAK,QAAQ;oBACX,MAAM,CAAC,IAAI,CAAC,GAAG,oBAAY,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;oBAC1C,MAAM;gBACR,KAAK,KAAK,CAAC;gBACX,KAAK,UAAU,CAAC;gBAChB,KAAK,gBAAgB;oBACnB,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,mBAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;oBAChD,MAAM;gBACR;oBACE,MAAM,IAAI,KAAK,CACb,4BAA4B,OAAO,CAAC,QAAQ,CAAW,EAAE,CAC1D,CAAC;aACL;QACH,CAAC,CAAC,CAAC;QAEH,OAAO,MAAW,CAAC;IACrB,CAAC;CACF;AAnKD,gCAmKC;AAED,kBAAe,UAAU,CAAC"}
|
package/lib/Signer.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import { Provider
|
|
2
|
-
import { TransactionJson, ActiveTransactionData } from "./interface";
|
|
3
|
-
import {
|
|
1
|
+
import { Provider } from "./Provider";
|
|
2
|
+
import { TransactionJson, ActiveTransactionData, Abi, SendTransactionResponse, RecoverPublicKeyOptions, BlockJson } from "./interface";
|
|
3
|
+
import { Serializer } from "./Serializer";
|
|
4
4
|
export interface SignerInterface {
|
|
5
5
|
provider?: Provider;
|
|
6
|
+
serializer?: Serializer;
|
|
6
7
|
getAddress(compressed?: boolean): string;
|
|
7
8
|
getPrivateKey(format: "wif" | "hex", compressed?: boolean): string;
|
|
8
9
|
signTransaction(tx: TransactionJson): Promise<TransactionJson>;
|
|
9
10
|
sendTransaction(tx: TransactionJson, abis?: Record<string, Abi>): Promise<SendTransactionResponse>;
|
|
10
11
|
encodeTransaction(activeData: ActiveTransactionData): Promise<TransactionJson>;
|
|
12
|
+
decodeTransaction(tx: TransactionJson): Promise<ActiveTransactionData>;
|
|
11
13
|
}
|
|
12
14
|
/**
|
|
13
15
|
* The Signer Class contains the private key needed to sign transactions.
|
|
@@ -16,7 +18,8 @@ export interface SignerInterface {
|
|
|
16
18
|
* @example
|
|
17
19
|
* using private key as hex string
|
|
18
20
|
* ```ts
|
|
19
|
-
* var
|
|
21
|
+
* var privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
|
|
22
|
+
* var signer = new Signer({ privateKey });
|
|
20
23
|
* ```
|
|
21
24
|
* <br>
|
|
22
25
|
*
|
|
@@ -28,14 +31,15 @@ export interface SignerInterface {
|
|
|
28
31
|
* 1, 203, 55, 128, 187, 2, 192, 249,
|
|
29
32
|
* 205, 254, 157, 9, 218, 173, 223, 156
|
|
30
33
|
* ]);
|
|
31
|
-
* var signer = new Signer(buffer);
|
|
34
|
+
* var signer = new Signer({ privateKey: buffer });
|
|
32
35
|
* ```
|
|
33
36
|
*
|
|
34
37
|
* <br>
|
|
35
38
|
*
|
|
36
39
|
* using private key as bigint
|
|
37
40
|
* ```ts
|
|
38
|
-
* var
|
|
41
|
+
* var privateKey = 106982601049961974618234078204952280507266494766432547312316920283818886029212n;
|
|
42
|
+
* var signer = new Signer({ privateKey });
|
|
39
43
|
* ```
|
|
40
44
|
*
|
|
41
45
|
* <br>
|
|
@@ -57,7 +61,8 @@ export interface SignerInterface {
|
|
|
57
61
|
* defining a provider
|
|
58
62
|
* ```ts
|
|
59
63
|
* var provider = new Provider(["https://example.com/jsonrpc"]);
|
|
60
|
-
* var
|
|
64
|
+
* var privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
|
|
65
|
+
* var signer = new Signer({ privateKey, provider });
|
|
61
66
|
* ```
|
|
62
67
|
*/
|
|
63
68
|
export declare class Signer implements SignerInterface {
|
|
@@ -73,9 +78,13 @@ export declare class Signer implements SignerInterface {
|
|
|
73
78
|
*/
|
|
74
79
|
address: string;
|
|
75
80
|
/**
|
|
76
|
-
* Provider
|
|
81
|
+
* Provider to connect with the blockchain
|
|
77
82
|
*/
|
|
78
|
-
provider
|
|
83
|
+
provider?: Provider;
|
|
84
|
+
/**
|
|
85
|
+
* Serializer to serialize/deserialize data types
|
|
86
|
+
*/
|
|
87
|
+
serializer?: Serializer;
|
|
79
88
|
/**
|
|
80
89
|
* The constructor receives de private key as hexstring, bigint or Uint8Array.
|
|
81
90
|
* See also the functions [[Signer.fromWif]] and [[Signer.fromSeed]]
|
|
@@ -86,12 +95,24 @@ export declare class Signer implements SignerInterface {
|
|
|
86
95
|
* @param provider - provider to connect with the blockchain
|
|
87
96
|
* @example
|
|
88
97
|
* ```ts
|
|
89
|
-
*
|
|
98
|
+
* const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
|
|
99
|
+
* cons signer = new Signer({ privateKey });
|
|
90
100
|
* console.log(signer.getAddress());
|
|
91
101
|
* // 1MbL6mG8ASAvSYdoMnGUfG3ZXkmQ2dpL5b
|
|
92
102
|
* ```
|
|
93
103
|
*/
|
|
94
|
-
constructor(
|
|
104
|
+
constructor(c: {
|
|
105
|
+
privateKey: string | number | bigint | Uint8Array;
|
|
106
|
+
compressed?: boolean;
|
|
107
|
+
provider?: Provider;
|
|
108
|
+
/**
|
|
109
|
+
* Set this option if you can not use _eval_ functions
|
|
110
|
+
* in the current environment. In such cases, the
|
|
111
|
+
* serializer must come from an environment where it
|
|
112
|
+
* is able to use those functions.
|
|
113
|
+
*/
|
|
114
|
+
serializer?: Serializer;
|
|
115
|
+
});
|
|
95
116
|
/**
|
|
96
117
|
* Function to import a private key from the WIF
|
|
97
118
|
* @param wif - Private key in WIF format
|
|
@@ -160,24 +181,109 @@ export declare class Signer implements SignerInterface {
|
|
|
160
181
|
*/
|
|
161
182
|
sendTransaction(tx: TransactionJson, _abis?: Record<string, Abi>): Promise<SendTransactionResponse>;
|
|
162
183
|
/**
|
|
163
|
-
* Function to recover the public key from a signed
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
184
|
+
* Function to recover the public key from a signed
|
|
185
|
+
* transaction or block.
|
|
186
|
+
* The output format can be compressed (default) or uncompressed.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```ts
|
|
190
|
+
* const publicKey = await Signer.recoverPublicKey(tx);
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* If the signature data contains more data, like in the
|
|
194
|
+
* blocks for PoW consensus, use the "transformSignature"
|
|
195
|
+
* function to extract the signature.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* const powDescriptorJson = {
|
|
200
|
+
* nested: {
|
|
201
|
+
* mypackage: {
|
|
202
|
+
* nested: {
|
|
203
|
+
* pow_signature_data: {
|
|
204
|
+
* fields: {
|
|
205
|
+
* nonce: {
|
|
206
|
+
* type: "bytes",
|
|
207
|
+
* id: 1,
|
|
208
|
+
* },
|
|
209
|
+
* recoverable_signature: {
|
|
210
|
+
* type: "bytes",
|
|
211
|
+
* id: 2,
|
|
212
|
+
* },
|
|
213
|
+
* },
|
|
214
|
+
* },
|
|
215
|
+
* },
|
|
216
|
+
* },
|
|
217
|
+
* },
|
|
218
|
+
* };
|
|
219
|
+
*
|
|
220
|
+
* const serializer = new Serializer(powDescriptorJson, {
|
|
221
|
+
* defaultTypeName: "pow_signature_data",
|
|
222
|
+
* });
|
|
223
|
+
*
|
|
224
|
+
* const signer = await Signer.recoverPublicKey(block, {
|
|
225
|
+
* transformSignature: async (signatureData) => {
|
|
226
|
+
* const powSignatureData = await serializer.deserialize(signatureData);
|
|
227
|
+
* return powSignatureData.recoverable_signature;
|
|
228
|
+
* },
|
|
229
|
+
* });
|
|
230
|
+
* ```
|
|
167
231
|
*/
|
|
168
|
-
static recoverPublicKey(
|
|
232
|
+
static recoverPublicKey(txOrBlock: TransactionJson | BlockJson, opts?: RecoverPublicKeyOptions): Promise<string>;
|
|
169
233
|
/**
|
|
170
|
-
* Function to recover the signer address from a signed
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
* @
|
|
234
|
+
* Function to recover the signer address from a signed
|
|
235
|
+
* transaction or block.
|
|
236
|
+
* The output format can be compressed (default) or uncompressed.
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* const publicKey = await Signer.recoverAddress(tx);
|
|
240
|
+
* ```
|
|
241
|
+
*
|
|
242
|
+
* If the signature data contains more data, like in the
|
|
243
|
+
* blocks for PoW consensus, use the "transformSignature"
|
|
244
|
+
* function to extract the signature.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```ts
|
|
248
|
+
* const powDescriptorJson = {
|
|
249
|
+
* nested: {
|
|
250
|
+
* mypackage: {
|
|
251
|
+
* nested: {
|
|
252
|
+
* pow_signature_data: {
|
|
253
|
+
* fields: {
|
|
254
|
+
* nonce: {
|
|
255
|
+
* type: "bytes",
|
|
256
|
+
* id: 1,
|
|
257
|
+
* },
|
|
258
|
+
* recoverable_signature: {
|
|
259
|
+
* type: "bytes",
|
|
260
|
+
* id: 2,
|
|
261
|
+
* },
|
|
262
|
+
* },
|
|
263
|
+
* },
|
|
264
|
+
* },
|
|
265
|
+
* },
|
|
266
|
+
* },
|
|
267
|
+
* };
|
|
268
|
+
*
|
|
269
|
+
* const serializer = new Serializer(powDescriptorJson, {
|
|
270
|
+
* defaultTypeName: "pow_signature_data",
|
|
271
|
+
* });
|
|
272
|
+
*
|
|
273
|
+
* const signer = await Signer.recoverAddress(block, {
|
|
274
|
+
* transformSignature: async (signatureData) => {
|
|
275
|
+
* const powSignatureData = await serializer.deserialize(signatureData);
|
|
276
|
+
* return powSignatureData.recoverable_signature;
|
|
277
|
+
* },
|
|
278
|
+
* });
|
|
279
|
+
* ```
|
|
174
280
|
*/
|
|
175
|
-
static recoverAddress(
|
|
281
|
+
static recoverAddress(txOrBlock: TransactionJson | BlockJson, opts?: RecoverPublicKeyOptions): Promise<string>;
|
|
176
282
|
/**
|
|
177
283
|
* Function to encode a transaction
|
|
178
|
-
* @param activeData - Active data consists of nonce,
|
|
284
|
+
* @param activeData - Active data consists of nonce, rc_limit, and
|
|
179
285
|
* operations. Do not set the nonce to get it from the blockchain
|
|
180
|
-
* using the provider. The
|
|
286
|
+
* using the provider. The rc_limit is 1000000 by default.
|
|
181
287
|
* @returns A transaction encoded. The active field is encoded in
|
|
182
288
|
* base64url
|
|
183
289
|
*/
|
|
@@ -185,6 +291,6 @@ export declare class Signer implements SignerInterface {
|
|
|
185
291
|
/**
|
|
186
292
|
* Function to decode a transaction
|
|
187
293
|
*/
|
|
188
|
-
|
|
294
|
+
decodeTransaction(tx: TransactionJson): Promise<ActiveTransactionData>;
|
|
189
295
|
}
|
|
190
296
|
export default Signer;
|