@arkecosystem/typescript-crypto 0.0.16 → 0.0.17
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/dist/enums/AbiFunction.d.ts +3 -1
- package/dist/enums/AbiFunction.d.ts.map +1 -1
- package/dist/enums/AbiFunction.js +2 -0
- package/dist/index.js +180 -96
- package/dist/transactions/builders/TokenTransferBuilder.d.ts +10 -0
- package/dist/transactions/builders/TokenTransferBuilder.d.ts.map +1 -0
- package/dist/transactions/builders/TokenTransferBuilder.js +25 -0
- package/dist/transactions/builders/index.d.ts +1 -0
- package/dist/transactions/builders/index.d.ts.map +1 -1
- package/dist/transactions/builders/index.js +1 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/AbiEncoder.d.ts +2 -0
- package/dist/utils/AbiEncoder.d.ts.map +1 -1
- package/dist/utils/AbiEncoder.js +3 -0
- package/dist/utils/Helpers.d.ts +8 -0
- package/dist/utils/Helpers.d.ts.map +1 -1
- package/dist/utils/Helpers.js +12 -0
- package/dist/utils/TransactionDataEncoder.d.ts +13 -0
- package/dist/utils/TransactionDataEncoder.d.ts.map +1 -0
- package/dist/utils/TransactionDataEncoder.js +44 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/package.json +1 -1
- package/src/enums/AbiFunction.ts +2 -0
- package/src/transactions/builders/TokenTransferBuilder.ts +35 -0
- package/src/transactions/builders/index.ts +1 -0
- package/src/types.ts +7 -0
- package/src/utils/AbiEncoder.ts +5 -0
- package/src/utils/Helpers.ts +15 -0
- package/src/utils/TransactionDataEncoder.ts +54 -0
- package/src/utils/index.ts +1 -0
- package/tests/fixtures/transactions/token-transfer.json +16 -0
- package/tests/fixtures/transactions/transaction-data-encoder.json +15 -0
- package/tests/unit/transactions/builders/TokenTransferBuilder.test.ts +138 -0
- package/tests/unit/utils/AbiEncoder.test.ts +12 -0
- package/tests/unit/utils/Helpers.test.ts +10 -0
- package/tests/unit/utils/TransactionDataEncoder.test.ts +51 -0
|
@@ -3,8 +3,10 @@ export declare enum AbiFunction {
|
|
|
3
3
|
UNVOTE = "unvote",
|
|
4
4
|
VALIDATOR_REGISTRATION = "registerValidator",
|
|
5
5
|
VALIDATOR_RESIGNATION = "resignValidator",
|
|
6
|
+
UPDATE_VALIDATOR = "updateValidator",
|
|
6
7
|
USERNAME_REGISTRATION = "registerUsername",
|
|
7
8
|
USERNAME_RESIGNATION = "resignUsername",
|
|
8
|
-
MULTIPAYMENT = "pay"
|
|
9
|
+
MULTIPAYMENT = "pay",
|
|
10
|
+
TRANSFER = "transfer"
|
|
9
11
|
}
|
|
10
12
|
//# sourceMappingURL=AbiFunction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbiFunction.d.ts","sourceRoot":"","sources":["../../src/enums/AbiFunction.ts"],"names":[],"mappings":"AAAA,oBAAY,WAAW;IACtB,IAAI,SAAS;IACb,MAAM,WAAW;IACjB,sBAAsB,sBAAsB;IAC5C,qBAAqB,oBAAoB;IACzC,qBAAqB,qBAAqB;IAC1C,oBAAoB,mBAAmB;IACvC,YAAY,QAAQ;
|
|
1
|
+
{"version":3,"file":"AbiFunction.d.ts","sourceRoot":"","sources":["../../src/enums/AbiFunction.ts"],"names":[],"mappings":"AAAA,oBAAY,WAAW;IACtB,IAAI,SAAS;IACb,MAAM,WAAW;IACjB,sBAAsB,sBAAsB;IAC5C,qBAAqB,oBAAoB;IACzC,gBAAgB,oBAAoB;IACpC,qBAAqB,qBAAqB;IAC1C,oBAAoB,mBAAmB;IACvC,YAAY,QAAQ;IACpB,QAAQ,aAAa;CACrB"}
|
|
@@ -4,7 +4,9 @@ export var AbiFunction;
|
|
|
4
4
|
AbiFunction["UNVOTE"] = "unvote";
|
|
5
5
|
AbiFunction["VALIDATOR_REGISTRATION"] = "registerValidator";
|
|
6
6
|
AbiFunction["VALIDATOR_RESIGNATION"] = "resignValidator";
|
|
7
|
+
AbiFunction["UPDATE_VALIDATOR"] = "updateValidator";
|
|
7
8
|
AbiFunction["USERNAME_REGISTRATION"] = "registerUsername";
|
|
8
9
|
AbiFunction["USERNAME_RESIGNATION"] = "resignUsername";
|
|
9
10
|
AbiFunction["MULTIPAYMENT"] = "pay";
|
|
11
|
+
AbiFunction["TRANSFER"] = "transfer";
|
|
10
12
|
})(AbiFunction || (AbiFunction = {}));
|
package/dist/index.js
CHANGED
|
@@ -40,9 +40,11 @@ var AbiFunction = /* @__PURE__ */ ((AbiFunction2) => {
|
|
|
40
40
|
AbiFunction2["UNVOTE"] = "unvote";
|
|
41
41
|
AbiFunction2["VALIDATOR_REGISTRATION"] = "registerValidator";
|
|
42
42
|
AbiFunction2["VALIDATOR_RESIGNATION"] = "resignValidator";
|
|
43
|
+
AbiFunction2["UPDATE_VALIDATOR"] = "updateValidator";
|
|
43
44
|
AbiFunction2["USERNAME_REGISTRATION"] = "registerUsername";
|
|
44
45
|
AbiFunction2["USERNAME_RESIGNATION"] = "resignUsername";
|
|
45
46
|
AbiFunction2["MULTIPAYMENT"] = "pay";
|
|
47
|
+
AbiFunction2["TRANSFER"] = "transfer";
|
|
46
48
|
return AbiFunction2;
|
|
47
49
|
})(AbiFunction || {});
|
|
48
50
|
|
|
@@ -25113,6 +25115,18 @@ var Helpers = class {
|
|
|
25113
25115
|
static removeLeadingHexZero(hex) {
|
|
25114
25116
|
return hex.replace(/^0x/, "");
|
|
25115
25117
|
}
|
|
25118
|
+
/**
|
|
25119
|
+
* Adds the leading '0x' to a string.
|
|
25120
|
+
*
|
|
25121
|
+
* @param string the input without `0x`
|
|
25122
|
+
* @returns the hex output with the leading `0x`
|
|
25123
|
+
*/
|
|
25124
|
+
static addLeadingHexZero(input) {
|
|
25125
|
+
if (input.startsWith("0x")) {
|
|
25126
|
+
return input;
|
|
25127
|
+
}
|
|
25128
|
+
return `0x${input}`;
|
|
25129
|
+
}
|
|
25116
25130
|
};
|
|
25117
25131
|
|
|
25118
25132
|
// src/transactions/builders/EvmCallBuilder.ts
|
|
@@ -28178,6 +28192,170 @@ var AbiEncoder = class extends AbiBase {
|
|
|
28178
28192
|
encodeFunctionCall(functionName, parameters) {
|
|
28179
28193
|
return Helpers.removeLeadingHexZero(this.interface.encodeFunctionData(functionName, parameters));
|
|
28180
28194
|
}
|
|
28195
|
+
encodeFunctionCallHex(functionName, parameters) {
|
|
28196
|
+
return this.interface.encodeFunctionData(functionName, parameters);
|
|
28197
|
+
}
|
|
28198
|
+
};
|
|
28199
|
+
|
|
28200
|
+
// src/utils/Message.ts
|
|
28201
|
+
var Message = class _Message {
|
|
28202
|
+
constructor(message) {
|
|
28203
|
+
this.publicKey = message.publicKey;
|
|
28204
|
+
this.signature = message.signature;
|
|
28205
|
+
this.message = message.message;
|
|
28206
|
+
}
|
|
28207
|
+
static new(message) {
|
|
28208
|
+
return new _Message(message);
|
|
28209
|
+
}
|
|
28210
|
+
static async sign(message, passphrase) {
|
|
28211
|
+
const privateKey = PrivateKey.fromPassphrase(passphrase);
|
|
28212
|
+
const publicKey = PublicKey.fromPassphrase(passphrase).publicKey;
|
|
28213
|
+
const signature = new SigningKey(`0x${privateKey.privateKey}`).sign(hashMessage(message));
|
|
28214
|
+
return _Message.new({
|
|
28215
|
+
publicKey,
|
|
28216
|
+
signature: signature.serialized,
|
|
28217
|
+
message
|
|
28218
|
+
});
|
|
28219
|
+
}
|
|
28220
|
+
verify() {
|
|
28221
|
+
const message = new Uint8Array(new TextEncoder().encode(this.message));
|
|
28222
|
+
const address = Address.fromPublicKey(this.publicKey);
|
|
28223
|
+
const signerAddress = verifyMessage(message, this.signature);
|
|
28224
|
+
return signerAddress === address;
|
|
28225
|
+
}
|
|
28226
|
+
toString() {
|
|
28227
|
+
return JSON.stringify(this.toJson());
|
|
28228
|
+
}
|
|
28229
|
+
toObject() {
|
|
28230
|
+
return [this.publicKey, this.signature, this.message];
|
|
28231
|
+
}
|
|
28232
|
+
toJson() {
|
|
28233
|
+
return {
|
|
28234
|
+
publicKey: this.publicKey,
|
|
28235
|
+
signature: this.signature,
|
|
28236
|
+
message: this.message
|
|
28237
|
+
};
|
|
28238
|
+
}
|
|
28239
|
+
};
|
|
28240
|
+
|
|
28241
|
+
// src/utils/Slot.ts
|
|
28242
|
+
var Slot = class {
|
|
28243
|
+
static time() {
|
|
28244
|
+
return Math.floor(Date.now() / 1e3) - this.epoch();
|
|
28245
|
+
}
|
|
28246
|
+
static epoch() {
|
|
28247
|
+
return Math.floor(new Date(Network.get().epoch()).getTime() / 1e3);
|
|
28248
|
+
}
|
|
28249
|
+
};
|
|
28250
|
+
|
|
28251
|
+
// src/utils/UnitConverter.ts
|
|
28252
|
+
var WEI_MULTIPLIER = 1;
|
|
28253
|
+
var GWEI_MULTIPLIER = 1e9;
|
|
28254
|
+
var ARK_MULTIPLIER = 1e18;
|
|
28255
|
+
var UnitConverter = class {
|
|
28256
|
+
static parseUnits(value, unit = "ark") {
|
|
28257
|
+
switch (unit.toLowerCase()) {
|
|
28258
|
+
case "wei":
|
|
28259
|
+
return bignumber_default(value).multipliedBy(WEI_MULTIPLIER);
|
|
28260
|
+
case "gwei":
|
|
28261
|
+
return bignumber_default(value).multipliedBy(GWEI_MULTIPLIER);
|
|
28262
|
+
case "ark":
|
|
28263
|
+
return bignumber_default(value).multipliedBy(ARK_MULTIPLIER);
|
|
28264
|
+
default:
|
|
28265
|
+
throw new Error(`Unsupported unit: ${unit}. Supported units are 'wei', 'gwei', and 'ark'.`);
|
|
28266
|
+
}
|
|
28267
|
+
}
|
|
28268
|
+
static formatUnits(value, unit = "ark") {
|
|
28269
|
+
switch (unit.toLowerCase()) {
|
|
28270
|
+
case "wei":
|
|
28271
|
+
return bignumber_default(value).dividedBy(WEI_MULTIPLIER).toNumber();
|
|
28272
|
+
case "gwei":
|
|
28273
|
+
return bignumber_default(value).dividedBy(GWEI_MULTIPLIER).toNumber();
|
|
28274
|
+
case "ark":
|
|
28275
|
+
return bignumber_default(value).dividedBy(ARK_MULTIPLIER).toNumber();
|
|
28276
|
+
default:
|
|
28277
|
+
throw new Error(`Unsupported unit: ${unit}. Supported units are 'wei', 'gwei', and 'ark'.`);
|
|
28278
|
+
}
|
|
28279
|
+
}
|
|
28280
|
+
static weiToArk(value, suffix) {
|
|
28281
|
+
const convertedValue = new bignumber_default(this.formatUnits(this.parseUnits(value, "wei").toString(), "ark")).decimalPlaces(18).toFixed();
|
|
28282
|
+
if (suffix) {
|
|
28283
|
+
return convertedValue + " " + suffix;
|
|
28284
|
+
}
|
|
28285
|
+
return convertedValue;
|
|
28286
|
+
}
|
|
28287
|
+
static gweiToArk(value, suffix) {
|
|
28288
|
+
const convertedValue = new bignumber_default(this.formatUnits(this.parseUnits(value, "gwei").toString(), "ark")).decimalPlaces(18).toFixed();
|
|
28289
|
+
if (suffix) {
|
|
28290
|
+
return convertedValue + " " + suffix;
|
|
28291
|
+
}
|
|
28292
|
+
return convertedValue;
|
|
28293
|
+
}
|
|
28294
|
+
};
|
|
28295
|
+
|
|
28296
|
+
// src/utils/TransactionDataEncoder.ts
|
|
28297
|
+
var TransactionDataEncoder = class {
|
|
28298
|
+
static multiPayment(recipients, amounts) {
|
|
28299
|
+
return new AbiEncoder("multipayment" /* MULTIPAYMENT */).encodeFunctionCallHex("pay" /* MULTIPAYMENT */, [
|
|
28300
|
+
recipients,
|
|
28301
|
+
amounts
|
|
28302
|
+
]);
|
|
28303
|
+
}
|
|
28304
|
+
static updateValidator(validatorPublicKey) {
|
|
28305
|
+
return new AbiEncoder("consensus" /* CONSENSUS */).encodeFunctionCallHex("updateValidator" /* UPDATE_VALIDATOR */, [
|
|
28306
|
+
Helpers.addLeadingHexZero(validatorPublicKey)
|
|
28307
|
+
]);
|
|
28308
|
+
}
|
|
28309
|
+
static usernameRegistration(username) {
|
|
28310
|
+
return new AbiEncoder("usernames" /* USERNAMES */).encodeFunctionCallHex("registerUsername" /* USERNAME_REGISTRATION */, [
|
|
28311
|
+
username
|
|
28312
|
+
]);
|
|
28313
|
+
}
|
|
28314
|
+
static usernameResignation() {
|
|
28315
|
+
return new AbiEncoder("usernames" /* USERNAMES */).encodeFunctionCallHex("resignUsername" /* USERNAME_RESIGNATION */, []);
|
|
28316
|
+
}
|
|
28317
|
+
static validatorRegistration(validatorPublicKey) {
|
|
28318
|
+
return new AbiEncoder("consensus" /* CONSENSUS */).encodeFunctionCallHex("registerValidator" /* VALIDATOR_REGISTRATION */, [
|
|
28319
|
+
Helpers.addLeadingHexZero(validatorPublicKey)
|
|
28320
|
+
]);
|
|
28321
|
+
}
|
|
28322
|
+
static validatorResignation() {
|
|
28323
|
+
return new AbiEncoder("consensus" /* CONSENSUS */).encodeFunctionCallHex("resignValidator" /* VALIDATOR_RESIGNATION */, []);
|
|
28324
|
+
}
|
|
28325
|
+
static tokenTransfer(recipientAddress, amount) {
|
|
28326
|
+
return new AbiEncoder("token" /* TOKEN */).encodeFunctionCallHex("transfer" /* TRANSFER */, [
|
|
28327
|
+
recipientAddress,
|
|
28328
|
+
amount
|
|
28329
|
+
]);
|
|
28330
|
+
}
|
|
28331
|
+
static vote(voteAddress) {
|
|
28332
|
+
return new AbiEncoder("consensus" /* CONSENSUS */).encodeFunctionCallHex("vote" /* VOTE */, [voteAddress]);
|
|
28333
|
+
}
|
|
28334
|
+
static unvote() {
|
|
28335
|
+
return new AbiEncoder("consensus" /* CONSENSUS */).encodeFunctionCallHex("unvote" /* UNVOTE */, []);
|
|
28336
|
+
}
|
|
28337
|
+
};
|
|
28338
|
+
|
|
28339
|
+
// src/transactions/builders/TokenTransferBuilder.ts
|
|
28340
|
+
var TokenTransferBuilder = class extends AbstractTransactionBuilder {
|
|
28341
|
+
constructor(data) {
|
|
28342
|
+
super(data);
|
|
28343
|
+
}
|
|
28344
|
+
static new(data) {
|
|
28345
|
+
return new this(data);
|
|
28346
|
+
}
|
|
28347
|
+
contractAddress(address) {
|
|
28348
|
+
this.transaction.data.to = address;
|
|
28349
|
+
return this;
|
|
28350
|
+
}
|
|
28351
|
+
recipient(address, amount) {
|
|
28352
|
+
const payload = new AbiEncoder("token" /* TOKEN */).encodeFunctionCall("transfer", [address, amount]);
|
|
28353
|
+
this.transaction.data["data"] = Helpers.removeLeadingHexZero(payload);
|
|
28354
|
+
return this;
|
|
28355
|
+
}
|
|
28356
|
+
getTransactionInstance(data) {
|
|
28357
|
+
return new EvmCall(data);
|
|
28358
|
+
}
|
|
28181
28359
|
};
|
|
28182
28360
|
|
|
28183
28361
|
// src/transactions/types/Multipayment.ts
|
|
@@ -28303,102 +28481,6 @@ var ValidatorRegistration = class extends AbstractTransaction {
|
|
|
28303
28481
|
}
|
|
28304
28482
|
};
|
|
28305
28483
|
|
|
28306
|
-
// src/utils/Message.ts
|
|
28307
|
-
var Message = class _Message {
|
|
28308
|
-
constructor(message) {
|
|
28309
|
-
this.publicKey = message.publicKey;
|
|
28310
|
-
this.signature = message.signature;
|
|
28311
|
-
this.message = message.message;
|
|
28312
|
-
}
|
|
28313
|
-
static new(message) {
|
|
28314
|
-
return new _Message(message);
|
|
28315
|
-
}
|
|
28316
|
-
static async sign(message, passphrase) {
|
|
28317
|
-
const privateKey = PrivateKey.fromPassphrase(passphrase);
|
|
28318
|
-
const publicKey = PublicKey.fromPassphrase(passphrase).publicKey;
|
|
28319
|
-
const signature = new SigningKey(`0x${privateKey.privateKey}`).sign(hashMessage(message));
|
|
28320
|
-
return _Message.new({
|
|
28321
|
-
publicKey,
|
|
28322
|
-
signature: signature.serialized,
|
|
28323
|
-
message
|
|
28324
|
-
});
|
|
28325
|
-
}
|
|
28326
|
-
verify() {
|
|
28327
|
-
const message = new Uint8Array(new TextEncoder().encode(this.message));
|
|
28328
|
-
const address = Address.fromPublicKey(this.publicKey);
|
|
28329
|
-
const signerAddress = verifyMessage(message, this.signature);
|
|
28330
|
-
return signerAddress === address;
|
|
28331
|
-
}
|
|
28332
|
-
toString() {
|
|
28333
|
-
return JSON.stringify(this.toJson());
|
|
28334
|
-
}
|
|
28335
|
-
toObject() {
|
|
28336
|
-
return [this.publicKey, this.signature, this.message];
|
|
28337
|
-
}
|
|
28338
|
-
toJson() {
|
|
28339
|
-
return {
|
|
28340
|
-
publicKey: this.publicKey,
|
|
28341
|
-
signature: this.signature,
|
|
28342
|
-
message: this.message
|
|
28343
|
-
};
|
|
28344
|
-
}
|
|
28345
|
-
};
|
|
28346
|
-
|
|
28347
|
-
// src/utils/Slot.ts
|
|
28348
|
-
var Slot = class {
|
|
28349
|
-
static time() {
|
|
28350
|
-
return Math.floor(Date.now() / 1e3) - this.epoch();
|
|
28351
|
-
}
|
|
28352
|
-
static epoch() {
|
|
28353
|
-
return Math.floor(new Date(Network.get().epoch()).getTime() / 1e3);
|
|
28354
|
-
}
|
|
28355
|
-
};
|
|
28356
|
-
|
|
28357
|
-
// src/utils/UnitConverter.ts
|
|
28358
|
-
var WEI_MULTIPLIER = 1;
|
|
28359
|
-
var GWEI_MULTIPLIER = 1e9;
|
|
28360
|
-
var ARK_MULTIPLIER = 1e18;
|
|
28361
|
-
var UnitConverter = class {
|
|
28362
|
-
static parseUnits(value, unit = "ark") {
|
|
28363
|
-
switch (unit.toLowerCase()) {
|
|
28364
|
-
case "wei":
|
|
28365
|
-
return bignumber_default(value).multipliedBy(WEI_MULTIPLIER);
|
|
28366
|
-
case "gwei":
|
|
28367
|
-
return bignumber_default(value).multipliedBy(GWEI_MULTIPLIER);
|
|
28368
|
-
case "ark":
|
|
28369
|
-
return bignumber_default(value).multipliedBy(ARK_MULTIPLIER);
|
|
28370
|
-
default:
|
|
28371
|
-
throw new Error(`Unsupported unit: ${unit}. Supported units are 'wei', 'gwei', and 'ark'.`);
|
|
28372
|
-
}
|
|
28373
|
-
}
|
|
28374
|
-
static formatUnits(value, unit = "ark") {
|
|
28375
|
-
switch (unit.toLowerCase()) {
|
|
28376
|
-
case "wei":
|
|
28377
|
-
return bignumber_default(value).dividedBy(WEI_MULTIPLIER).toNumber();
|
|
28378
|
-
case "gwei":
|
|
28379
|
-
return bignumber_default(value).dividedBy(GWEI_MULTIPLIER).toNumber();
|
|
28380
|
-
case "ark":
|
|
28381
|
-
return bignumber_default(value).dividedBy(ARK_MULTIPLIER).toNumber();
|
|
28382
|
-
default:
|
|
28383
|
-
throw new Error(`Unsupported unit: ${unit}. Supported units are 'wei', 'gwei', and 'ark'.`);
|
|
28384
|
-
}
|
|
28385
|
-
}
|
|
28386
|
-
static weiToArk(value, suffix) {
|
|
28387
|
-
const convertedValue = new bignumber_default(this.formatUnits(this.parseUnits(value, "wei").toString(), "ark")).decimalPlaces(18).toFixed();
|
|
28388
|
-
if (suffix) {
|
|
28389
|
-
return convertedValue + " " + suffix;
|
|
28390
|
-
}
|
|
28391
|
-
return convertedValue;
|
|
28392
|
-
}
|
|
28393
|
-
static gweiToArk(value, suffix) {
|
|
28394
|
-
const convertedValue = new bignumber_default(this.formatUnits(this.parseUnits(value, "gwei").toString(), "ark")).decimalPlaces(18).toFixed();
|
|
28395
|
-
if (suffix) {
|
|
28396
|
-
return convertedValue + " " + suffix;
|
|
28397
|
-
}
|
|
28398
|
-
return convertedValue;
|
|
28399
|
-
}
|
|
28400
|
-
};
|
|
28401
|
-
|
|
28402
28484
|
// src/transactions/builders/ValidatorRegistrationBuilder.ts
|
|
28403
28485
|
var ValidatorRegistrationBuilder = class extends AbstractTransactionBuilder {
|
|
28404
28486
|
constructor(data) {
|
|
@@ -28678,6 +28760,8 @@ export {
|
|
|
28678
28760
|
Slot,
|
|
28679
28761
|
Testnet,
|
|
28680
28762
|
Abi_Token_default as TokenContract,
|
|
28763
|
+
TokenTransferBuilder,
|
|
28764
|
+
TransactionDataEncoder,
|
|
28681
28765
|
TransactionUtils,
|
|
28682
28766
|
Transfer,
|
|
28683
28767
|
TransferBuilder,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ITokenTransferBuilder, ITransaction } from "@/types";
|
|
2
|
+
import { AbstractTransactionBuilder } from "./AbstractTransactionBuilder";
|
|
3
|
+
export declare class TokenTransferBuilder extends AbstractTransactionBuilder<ITokenTransferBuilder> implements ITokenTransferBuilder {
|
|
4
|
+
constructor(data?: Record<string, unknown>);
|
|
5
|
+
static new(data?: Record<string, unknown>): TokenTransferBuilder;
|
|
6
|
+
contractAddress(address: string): TokenTransferBuilder;
|
|
7
|
+
recipient(address: string, amount: bigint): TokenTransferBuilder;
|
|
8
|
+
protected getTransactionInstance(data?: Record<string, unknown>): ITransaction;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=TokenTransferBuilder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TokenTransferBuilder.d.ts","sourceRoot":"","sources":["../../../src/transactions/builders/TokenTransferBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,YAAY,EAAmB,MAAM,SAAS,CAAC;AAE/E,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAC;AAM1E,qBAAa,oBACZ,SAAQ,0BAA0B,CAAC,qBAAqB,CACxD,YAAW,qBAAqB;gBAEb,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;WAInC,GAAG,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,oBAAoB;IAIhE,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,oBAAoB;IAKtD,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,oBAAoB;IAMvE,SAAS,CAAC,sBAAsB,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,YAAY;CAG9E"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AbstractTransactionBuilder } from "./AbstractTransactionBuilder";
|
|
2
|
+
import { EvmCall } from "@/transactions/types/EvmCall";
|
|
3
|
+
import { Helpers } from "@/utils/Helpers";
|
|
4
|
+
import { AbiEncoder } from "@/utils";
|
|
5
|
+
import { ContractAbiType } from "@/enums";
|
|
6
|
+
export class TokenTransferBuilder extends AbstractTransactionBuilder {
|
|
7
|
+
constructor(data) {
|
|
8
|
+
super(data);
|
|
9
|
+
}
|
|
10
|
+
static new(data) {
|
|
11
|
+
return new this(data);
|
|
12
|
+
}
|
|
13
|
+
contractAddress(address) {
|
|
14
|
+
this.transaction.data.to = address;
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
recipient(address, amount) {
|
|
18
|
+
const payload = new AbiEncoder(ContractAbiType.TOKEN).encodeFunctionCall("transfer", [address, amount]);
|
|
19
|
+
this.transaction.data["data"] = Helpers.removeLeadingHexZero(payload);
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
getTransactionInstance(data) {
|
|
23
|
+
return new EvmCall(data);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/transactions/builders/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/transactions/builders/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,eAAe,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import BigNumber from "bignumber.js";
|
|
2
2
|
import { Result } from "ethers";
|
|
3
|
+
export type Hex = `0x${string}`;
|
|
3
4
|
export interface TransactionData {
|
|
4
5
|
gasPrice?: string | null;
|
|
5
6
|
gasLimit?: string | null;
|
|
@@ -66,6 +67,10 @@ export interface IUsernameRegistrationBuilder extends ITransactionBuilder<IUsern
|
|
|
66
67
|
export interface IEvmCallBuilder extends ITransactionBuilder<IEvmCallBuilder> {
|
|
67
68
|
payload(payload: string): IEvmCallBuilder;
|
|
68
69
|
}
|
|
70
|
+
export interface ITokenTransferBuilder extends ITransactionBuilder<ITokenTransferBuilder> {
|
|
71
|
+
recipient(address: string, amount: bigint): ITokenTransferBuilder;
|
|
72
|
+
contractAddress(address: string): ITokenTransferBuilder;
|
|
73
|
+
}
|
|
69
74
|
export interface IMultipaymentBuilder extends ITransactionBuilder<IMultipaymentBuilder> {
|
|
70
75
|
pay(address: string, amount: string | BigNumber): IMultipaymentBuilder;
|
|
71
76
|
}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,qBAAqB,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAC/C,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAC/B,eAAe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AwB,CAAC;CAAG"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { Hex } from "@/types";
|
|
1
2
|
import { AbiBase } from "./AbiBase";
|
|
2
3
|
export declare class AbiEncoder extends AbiBase {
|
|
3
4
|
encodeFunctionCall(functionName: string, parameters: unknown[]): string;
|
|
5
|
+
encodeFunctionCallHex(functionName: string, parameters: unknown[]): Hex;
|
|
4
6
|
}
|
|
5
7
|
//# sourceMappingURL=AbiEncoder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AbiEncoder.d.ts","sourceRoot":"","sources":["../../src/utils/AbiEncoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,qBAAa,UAAW,SAAQ,OAAO;IACtC,kBAAkB,CAAC,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,MAAM;CAGvE"}
|
|
1
|
+
{"version":3,"file":"AbiEncoder.d.ts","sourceRoot":"","sources":["../../src/utils/AbiEncoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,qBAAa,UAAW,SAAQ,OAAO;IACtC,kBAAkB,CAAC,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,MAAM;IAIvE,qBAAqB,CAAC,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,GAAG;CAGvE"}
|
package/dist/utils/AbiEncoder.js
CHANGED
|
@@ -4,4 +4,7 @@ export class AbiEncoder extends AbiBase {
|
|
|
4
4
|
encodeFunctionCall(functionName, parameters) {
|
|
5
5
|
return Helpers.removeLeadingHexZero(this.interface.encodeFunctionData(functionName, parameters));
|
|
6
6
|
}
|
|
7
|
+
encodeFunctionCallHex(functionName, parameters) {
|
|
8
|
+
return this.interface.encodeFunctionData(functionName, parameters);
|
|
9
|
+
}
|
|
7
10
|
}
|
package/dist/utils/Helpers.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Hex } from "@/types";
|
|
1
2
|
export declare class Helpers {
|
|
2
3
|
/**
|
|
3
4
|
* Validates a username based on specific rules.
|
|
@@ -15,5 +16,12 @@ export declare class Helpers {
|
|
|
15
16
|
* @returns The hex string without the leading '0x'
|
|
16
17
|
*/
|
|
17
18
|
static removeLeadingHexZero(hex: string): string;
|
|
19
|
+
/**
|
|
20
|
+
* Adds the leading '0x' to a string.
|
|
21
|
+
*
|
|
22
|
+
* @param string the input without `0x`
|
|
23
|
+
* @returns the hex output with the leading `0x`
|
|
24
|
+
*/
|
|
25
|
+
static addLeadingHexZero(input: string): Hex;
|
|
18
26
|
}
|
|
19
27
|
//# sourceMappingURL=Helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Helpers.d.ts","sourceRoot":"","sources":["../../src/utils/Helpers.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Helpers.d.ts","sourceRoot":"","sources":["../../src/utils/Helpers.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAE9B,qBAAa,OAAO;IACnB;;;;;;;OAOG;WACW,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAyBxD;;;;;OAKG;WACW,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAIvD;;;;;OAKG;WACW,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG;CAOnD"}
|
package/dist/utils/Helpers.js
CHANGED
|
@@ -35,4 +35,16 @@ export class Helpers {
|
|
|
35
35
|
static removeLeadingHexZero(hex) {
|
|
36
36
|
return hex.replace(/^0x/, ""); // Using replace to only remove the prefix, not leading zeros
|
|
37
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Adds the leading '0x' to a string.
|
|
40
|
+
*
|
|
41
|
+
* @param string the input without `0x`
|
|
42
|
+
* @returns the hex output with the leading `0x`
|
|
43
|
+
*/
|
|
44
|
+
static addLeadingHexZero(input) {
|
|
45
|
+
if (input.startsWith("0x")) {
|
|
46
|
+
return input;
|
|
47
|
+
}
|
|
48
|
+
return `0x${input}`;
|
|
49
|
+
}
|
|
38
50
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Hex } from "@/types";
|
|
2
|
+
export declare class TransactionDataEncoder {
|
|
3
|
+
static multiPayment(recipients: string[], amounts: string[]): Hex;
|
|
4
|
+
static updateValidator(validatorPublicKey: string): Hex;
|
|
5
|
+
static usernameRegistration(username: string): Hex;
|
|
6
|
+
static usernameResignation(): Hex;
|
|
7
|
+
static validatorRegistration(validatorPublicKey: string): Hex;
|
|
8
|
+
static validatorResignation(): Hex;
|
|
9
|
+
static tokenTransfer(recipientAddress: string, amount: string): Hex;
|
|
10
|
+
static vote(voteAddress: string): Hex;
|
|
11
|
+
static unvote(): Hex;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=TransactionDataEncoder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TransactionDataEncoder.d.ts","sourceRoot":"","sources":["../../src/utils/TransactionDataEncoder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAC;AAG9B,qBAAa,sBAAsB;WACpB,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,GAAG;WAO1D,eAAe,CAAC,kBAAkB,EAAE,MAAM,GAAG,GAAG;WAMhD,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG;WAM3C,mBAAmB,IAAI,GAAG;WAI1B,qBAAqB,CAAC,kBAAkB,EAAE,MAAM,GAAG,GAAG;WAMtD,oBAAoB,IAAI,GAAG;WAI3B,aAAa,CAAC,gBAAgB,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,GAAG;WAO5D,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,GAAG;WAI9B,MAAM,IAAI,GAAG;CAG3B"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { AbiFunction, ContractAbiType } from "@/enums";
|
|
2
|
+
import { AbiEncoder } from "./AbiEncoder";
|
|
3
|
+
import { Helpers } from "./Helpers";
|
|
4
|
+
export class TransactionDataEncoder {
|
|
5
|
+
static multiPayment(recipients, amounts) {
|
|
6
|
+
return new AbiEncoder(ContractAbiType.MULTIPAYMENT).encodeFunctionCallHex(AbiFunction.MULTIPAYMENT, [
|
|
7
|
+
recipients,
|
|
8
|
+
amounts,
|
|
9
|
+
]);
|
|
10
|
+
}
|
|
11
|
+
static updateValidator(validatorPublicKey) {
|
|
12
|
+
return new AbiEncoder(ContractAbiType.CONSENSUS).encodeFunctionCallHex(AbiFunction.UPDATE_VALIDATOR, [
|
|
13
|
+
Helpers.addLeadingHexZero(validatorPublicKey),
|
|
14
|
+
]);
|
|
15
|
+
}
|
|
16
|
+
static usernameRegistration(username) {
|
|
17
|
+
return new AbiEncoder(ContractAbiType.USERNAMES).encodeFunctionCallHex(AbiFunction.USERNAME_REGISTRATION, [
|
|
18
|
+
username,
|
|
19
|
+
]);
|
|
20
|
+
}
|
|
21
|
+
static usernameResignation() {
|
|
22
|
+
return new AbiEncoder(ContractAbiType.USERNAMES).encodeFunctionCallHex(AbiFunction.USERNAME_RESIGNATION, []);
|
|
23
|
+
}
|
|
24
|
+
static validatorRegistration(validatorPublicKey) {
|
|
25
|
+
return new AbiEncoder(ContractAbiType.CONSENSUS).encodeFunctionCallHex(AbiFunction.VALIDATOR_REGISTRATION, [
|
|
26
|
+
Helpers.addLeadingHexZero(validatorPublicKey),
|
|
27
|
+
]);
|
|
28
|
+
}
|
|
29
|
+
static validatorResignation() {
|
|
30
|
+
return new AbiEncoder(ContractAbiType.CONSENSUS).encodeFunctionCallHex(AbiFunction.VALIDATOR_RESIGNATION, []);
|
|
31
|
+
}
|
|
32
|
+
static tokenTransfer(recipientAddress, amount) {
|
|
33
|
+
return new AbiEncoder(ContractAbiType.TOKEN).encodeFunctionCallHex(AbiFunction.TRANSFER, [
|
|
34
|
+
recipientAddress,
|
|
35
|
+
amount,
|
|
36
|
+
]);
|
|
37
|
+
}
|
|
38
|
+
static vote(voteAddress) {
|
|
39
|
+
return new AbiEncoder(ContractAbiType.CONSENSUS).encodeFunctionCallHex(AbiFunction.VOTE, [voteAddress]);
|
|
40
|
+
}
|
|
41
|
+
static unvote() {
|
|
42
|
+
return new AbiEncoder(ContractAbiType.CONSENSUS).encodeFunctionCallHex(AbiFunction.UNVOTE, []);
|
|
43
|
+
}
|
|
44
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC;AACvB,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC"}
|
package/dist/utils/index.js
CHANGED
package/package.json
CHANGED
package/src/enums/AbiFunction.ts
CHANGED
|
@@ -3,7 +3,9 @@ export enum AbiFunction {
|
|
|
3
3
|
UNVOTE = "unvote",
|
|
4
4
|
VALIDATOR_REGISTRATION = "registerValidator",
|
|
5
5
|
VALIDATOR_RESIGNATION = "resignValidator",
|
|
6
|
+
UPDATE_VALIDATOR = "updateValidator",
|
|
6
7
|
USERNAME_REGISTRATION = "registerUsername",
|
|
7
8
|
USERNAME_RESIGNATION = "resignUsername",
|
|
8
9
|
MULTIPAYMENT = "pay",
|
|
10
|
+
TRANSFER = "transfer",
|
|
9
11
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ITokenTransferBuilder, ITransaction, TransactionData } from "@/types";
|
|
2
|
+
|
|
3
|
+
import { AbstractTransactionBuilder } from "./AbstractTransactionBuilder";
|
|
4
|
+
import { EvmCall } from "@/transactions/types/EvmCall";
|
|
5
|
+
import { Helpers } from "@/utils/Helpers";
|
|
6
|
+
import { AbiEncoder } from "@/utils";
|
|
7
|
+
import { ContractAbiType } from "@/enums";
|
|
8
|
+
|
|
9
|
+
export class TokenTransferBuilder
|
|
10
|
+
extends AbstractTransactionBuilder<ITokenTransferBuilder>
|
|
11
|
+
implements ITokenTransferBuilder
|
|
12
|
+
{
|
|
13
|
+
public constructor(data?: Record<string, unknown>) {
|
|
14
|
+
super(data);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public static new(data?: Record<string, unknown>): TokenTransferBuilder {
|
|
18
|
+
return new this(data);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public contractAddress(address: string): TokenTransferBuilder {
|
|
22
|
+
this.transaction.data.to = address;
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public recipient(address: string, amount: bigint): TokenTransferBuilder {
|
|
27
|
+
const payload = new AbiEncoder(ContractAbiType.TOKEN).encodeFunctionCall("transfer", [address, amount]);
|
|
28
|
+
this.transaction.data["data"] = Helpers.removeLeadingHexZero(payload);
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected getTransactionInstance(data?: Record<string, unknown>): ITransaction {
|
|
33
|
+
return new EvmCall(data as TransactionData);
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import BigNumber from "bignumber.js";
|
|
2
2
|
import { Result } from "ethers";
|
|
3
3
|
|
|
4
|
+
export type Hex = `0x${string}`;
|
|
5
|
+
|
|
4
6
|
export interface TransactionData {
|
|
5
7
|
gasPrice?: string | null;
|
|
6
8
|
gasLimit?: string | null;
|
|
@@ -81,6 +83,11 @@ export interface IEvmCallBuilder extends ITransactionBuilder<IEvmCallBuilder> {
|
|
|
81
83
|
payload(payload: string): IEvmCallBuilder;
|
|
82
84
|
}
|
|
83
85
|
|
|
86
|
+
export interface ITokenTransferBuilder extends ITransactionBuilder<ITokenTransferBuilder> {
|
|
87
|
+
recipient(address: string, amount: bigint): ITokenTransferBuilder;
|
|
88
|
+
contractAddress(address: string): ITokenTransferBuilder;
|
|
89
|
+
}
|
|
90
|
+
|
|
84
91
|
export interface IMultipaymentBuilder extends ITransactionBuilder<IMultipaymentBuilder> {
|
|
85
92
|
pay(address: string, amount: string | BigNumber): IMultipaymentBuilder;
|
|
86
93
|
}
|
package/src/utils/AbiEncoder.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Hex } from "@/types";
|
|
1
2
|
import { AbiBase } from "./AbiBase";
|
|
2
3
|
import { Helpers } from "./Helpers";
|
|
3
4
|
|
|
@@ -5,4 +6,8 @@ export class AbiEncoder extends AbiBase {
|
|
|
5
6
|
encodeFunctionCall(functionName: string, parameters: unknown[]): string {
|
|
6
7
|
return Helpers.removeLeadingHexZero(this.interface.encodeFunctionData(functionName, parameters));
|
|
7
8
|
}
|
|
9
|
+
|
|
10
|
+
encodeFunctionCallHex(functionName: string, parameters: unknown[]): Hex {
|
|
11
|
+
return this.interface.encodeFunctionData(functionName, parameters) as Hex;
|
|
12
|
+
}
|
|
8
13
|
}
|
package/src/utils/Helpers.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { InvalidUsernameException } from "@/exceptions/InvalidUsernameException";
|
|
2
|
+
import { Hex } from "@/types";
|
|
2
3
|
|
|
3
4
|
export class Helpers {
|
|
4
5
|
/**
|
|
@@ -43,4 +44,18 @@ export class Helpers {
|
|
|
43
44
|
public static removeLeadingHexZero(hex: string): string {
|
|
44
45
|
return hex.replace(/^0x/, ""); // Using replace to only remove the prefix, not leading zeros
|
|
45
46
|
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Adds the leading '0x' to a string.
|
|
50
|
+
*
|
|
51
|
+
* @param string the input without `0x`
|
|
52
|
+
* @returns the hex output with the leading `0x`
|
|
53
|
+
*/
|
|
54
|
+
public static addLeadingHexZero(input: string): Hex {
|
|
55
|
+
if (input.startsWith("0x")) {
|
|
56
|
+
return input as Hex;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return `0x${input}`;
|
|
60
|
+
}
|
|
46
61
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { AbiFunction, ContractAbiType } from "@/enums";
|
|
2
|
+
import { AbiEncoder } from "./AbiEncoder";
|
|
3
|
+
import { Hex } from "@/types";
|
|
4
|
+
import { Helpers } from "./Helpers";
|
|
5
|
+
|
|
6
|
+
export class TransactionDataEncoder {
|
|
7
|
+
public static multiPayment(recipients: string[], amounts: string[]): Hex {
|
|
8
|
+
return new AbiEncoder(ContractAbiType.MULTIPAYMENT).encodeFunctionCallHex(AbiFunction.MULTIPAYMENT, [
|
|
9
|
+
recipients,
|
|
10
|
+
amounts,
|
|
11
|
+
]);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public static updateValidator(validatorPublicKey: string): Hex {
|
|
15
|
+
return new AbiEncoder(ContractAbiType.CONSENSUS).encodeFunctionCallHex(AbiFunction.UPDATE_VALIDATOR, [
|
|
16
|
+
Helpers.addLeadingHexZero(validatorPublicKey),
|
|
17
|
+
]);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static usernameRegistration(username: string): Hex {
|
|
21
|
+
return new AbiEncoder(ContractAbiType.USERNAMES).encodeFunctionCallHex(AbiFunction.USERNAME_REGISTRATION, [
|
|
22
|
+
username,
|
|
23
|
+
]);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public static usernameResignation(): Hex {
|
|
27
|
+
return new AbiEncoder(ContractAbiType.USERNAMES).encodeFunctionCallHex(AbiFunction.USERNAME_RESIGNATION, []);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public static validatorRegistration(validatorPublicKey: string): Hex {
|
|
31
|
+
return new AbiEncoder(ContractAbiType.CONSENSUS).encodeFunctionCallHex(AbiFunction.VALIDATOR_REGISTRATION, [
|
|
32
|
+
Helpers.addLeadingHexZero(validatorPublicKey),
|
|
33
|
+
]);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public static validatorResignation(): Hex {
|
|
37
|
+
return new AbiEncoder(ContractAbiType.CONSENSUS).encodeFunctionCallHex(AbiFunction.VALIDATOR_RESIGNATION, []);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public static tokenTransfer(recipientAddress: string, amount: string): Hex {
|
|
41
|
+
return new AbiEncoder(ContractAbiType.TOKEN).encodeFunctionCallHex(AbiFunction.TRANSFER, [
|
|
42
|
+
recipientAddress,
|
|
43
|
+
amount,
|
|
44
|
+
]);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public static vote(voteAddress: string): Hex {
|
|
48
|
+
return new AbiEncoder(ContractAbiType.CONSENSUS).encodeFunctionCallHex(AbiFunction.VOTE, [voteAddress]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public static unvote(): Hex {
|
|
52
|
+
return new AbiEncoder(ContractAbiType.CONSENSUS).encodeFunctionCallHex(AbiFunction.UNVOTE, []);
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"data": {
|
|
3
|
+
"value": "0",
|
|
4
|
+
"senderPublicKey": "0243333347c8cbf4e3cbc7a96964181d02a2b0c854faa2fef86b4b8d92afcf473d",
|
|
5
|
+
"gasPrice": "5000000000",
|
|
6
|
+
"gasLimit": "21000",
|
|
7
|
+
"nonce": "1",
|
|
8
|
+
"data": "a9059cbb0000000000000000000000006f0182a0cc707b055322ccf6d4cb6a5aff1aeb220000000000000000000000000000000000000000000000000000000000000000",
|
|
9
|
+
"to": "0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22",
|
|
10
|
+
"v": 0,
|
|
11
|
+
"r": "ca5516c582647a1626bfeb4ccf62314dae02c8c561dbff690bdbda89d181e6f7",
|
|
12
|
+
"s": "1b4ddf55e432e532c8e8d1a854f21e0c80e35a35fe4557899fb3f485d071f1e1",
|
|
13
|
+
"hash": "4bb567e6ffd6716b99865fc6917eadf0f5b6cbccdd6999a592db6143a7f24d81"
|
|
14
|
+
},
|
|
15
|
+
"serialized": "f8ab0185012a05f200825208946f0182a0cc707b055322ccf6d4cb6a5aff1aeb2280b844a9059cbb0000000000000000000000006f0182a0cc707b055322ccf6d4cb6a5aff1aeb220000000000000000000000000000000000000000000000000000000000000000825c6ba0ca5516c582647a1626bfeb4ccf62314dae02c8c561dbff690bdbda89d181e6f7a01b4ddf55e432e532c8e8d1a854f21e0c80e35a35fe4557899fb3f485d071f1e1"
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"Username": "test",
|
|
3
|
+
"Address": "0xA5cc0BfEB09742C5e4C610f2EBaaB82Eb142Ca10",
|
|
4
|
+
"ValidatorPublicKey": "b209f4a7454ae17c5808991dffbf204c747b851f351d2ce72a6e18903d0e2f609e0328ebbc3fb97cd4d3660b4bc156f1",
|
|
5
|
+
"Amount": "1000000000000",
|
|
6
|
+
"Encoded": {
|
|
7
|
+
"UpdateValidator": "0x5a8eed7300000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030b209f4a7454ae17c5808991dffbf204c747b851f351d2ce72a6e18903d0e2f609e0328ebbc3fb97cd4d3660b4bc156f100000000000000000000000000000000",
|
|
8
|
+
"UsernameRegistration": "0x36a94134000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000047465737400000000000000000000000000000000000000000000000000000000",
|
|
9
|
+
"UsernameResignation": "0xebed6dab",
|
|
10
|
+
"ValidatorRegistration": "0x602a9eee00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030b209f4a7454ae17c5808991dffbf204c747b851f351d2ce72a6e18903d0e2f609e0328ebbc3fb97cd4d3660b4bc156f100000000000000000000000000000000",
|
|
11
|
+
"ValidatorResignation": "0xb85f5da2",
|
|
12
|
+
"TokenTransfer": "0xa9059cbb000000000000000000000000a5cc0bfeb09742c5e4c610f2ebaab82eb142ca10000000000000000000000000000000000000000000000000000000e8d4a51000",
|
|
13
|
+
"MultiPayment": "0x084ce708000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000a5cc0bfeb09742c5e4c610f2ebaab82eb142ca10000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000003e8"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { TokenTransferBuilder } from "@/transactions/builders/TokenTransferBuilder";
|
|
2
|
+
import identityFixture from "@tests/fixtures/identity.json";
|
|
3
|
+
import fixture from "@tests/fixtures/transactions/token-transfer.json";
|
|
4
|
+
|
|
5
|
+
it("should build a token transfer", async () => {
|
|
6
|
+
const transaction = (
|
|
7
|
+
await TokenTransferBuilder.new()
|
|
8
|
+
.recipient(fixture.data.to, BigInt(fixture.data.value))
|
|
9
|
+
.to(fixture.data.to)
|
|
10
|
+
.nonce(fixture.data.nonce)
|
|
11
|
+
.gasPrice(fixture.data.gasPrice)
|
|
12
|
+
.gasLimit(fixture.data.gasLimit)
|
|
13
|
+
.sign(identityFixture.passphrase)
|
|
14
|
+
).transaction;
|
|
15
|
+
|
|
16
|
+
expect(transaction.data.gasPrice).toBe(fixture.data.gasPrice);
|
|
17
|
+
expect(transaction.data.nonce).toBe(fixture.data.nonce);
|
|
18
|
+
expect(transaction.data.gasLimit).toBe(fixture.data.gasLimit);
|
|
19
|
+
expect(transaction.data.value).toBe("0");
|
|
20
|
+
expect(transaction.data.v).toBe(fixture.data.v);
|
|
21
|
+
expect(transaction.data.r).toBe(fixture.data.r);
|
|
22
|
+
expect(transaction.data.s).toBe(fixture.data.s);
|
|
23
|
+
expect(transaction.data.hash).toBe(fixture.data.hash);
|
|
24
|
+
|
|
25
|
+
expect(transaction.serialize().toString("hex")).toEqual(fixture.serialized);
|
|
26
|
+
expect(transaction.verify()).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should sign a token transfer", async () => {
|
|
30
|
+
const { transaction } = await TokenTransferBuilder.new()
|
|
31
|
+
.recipient(fixture.data.to, BigInt(fixture.data.value))
|
|
32
|
+
.to(fixture.data.to)
|
|
33
|
+
.nonce(fixture.data.nonce)
|
|
34
|
+
.gasPrice(fixture.data.gasPrice)
|
|
35
|
+
.gasLimit(fixture.data.gasLimit)
|
|
36
|
+
.sign(identityFixture.passphrase);
|
|
37
|
+
|
|
38
|
+
expect(transaction.data.gasPrice).toBe(fixture.data.gasPrice);
|
|
39
|
+
expect(transaction.data.nonce).toBe(fixture.data.nonce);
|
|
40
|
+
expect(transaction.data.gasLimit).toBe(fixture.data.gasLimit);
|
|
41
|
+
expect(transaction.data.to).toBe(fixture.data.to);
|
|
42
|
+
expect(transaction.data.value).toBe(fixture.data.value);
|
|
43
|
+
expect(transaction.data.v).toBe(fixture.data.v);
|
|
44
|
+
expect(transaction.data.r).toBe(fixture.data.r);
|
|
45
|
+
expect(transaction.data.s).toBe(fixture.data.s);
|
|
46
|
+
expect(transaction.data.hash).toBe(fixture.data.hash);
|
|
47
|
+
|
|
48
|
+
expect(transaction.serialize().toString("hex")).toEqual(fixture.serialized);
|
|
49
|
+
expect(transaction.verify()).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should verify", async () => {
|
|
53
|
+
const transaction = await TokenTransferBuilder.new()
|
|
54
|
+
.recipient(fixture.data.to, BigInt(fixture.data.value))
|
|
55
|
+
.to(fixture.data.to)
|
|
56
|
+
.nonce(fixture.data.nonce)
|
|
57
|
+
.gasPrice(fixture.data.gasPrice)
|
|
58
|
+
.gasLimit(fixture.data.gasLimit)
|
|
59
|
+
.sign(identityFixture.passphrase);
|
|
60
|
+
|
|
61
|
+
expect(transaction.verify()).toBe(true);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should convert to an array", async () => {
|
|
65
|
+
const transaction = await TokenTransferBuilder.new()
|
|
66
|
+
.recipient(fixture.data.to, BigInt(fixture.data.value))
|
|
67
|
+
.to(fixture.data.to)
|
|
68
|
+
.nonce(fixture.data.nonce)
|
|
69
|
+
.gasPrice(fixture.data.gasPrice)
|
|
70
|
+
.gasLimit(fixture.data.gasLimit)
|
|
71
|
+
.sign(identityFixture.passphrase);
|
|
72
|
+
|
|
73
|
+
expect(transaction.toObject()).toEqual({
|
|
74
|
+
data: fixture.data.data,
|
|
75
|
+
to: fixture.data.to,
|
|
76
|
+
value: fixture.data.value,
|
|
77
|
+
gasPrice: fixture.data.gasPrice,
|
|
78
|
+
gasLimit: fixture.data.gasLimit,
|
|
79
|
+
nonce: fixture.data.nonce,
|
|
80
|
+
v: fixture.data.v,
|
|
81
|
+
r: fixture.data.r,
|
|
82
|
+
s: fixture.data.s,
|
|
83
|
+
senderPublicKey: fixture.data.senderPublicKey,
|
|
84
|
+
hash: fixture.data.hash,
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("should convert to json", async () => {
|
|
89
|
+
const transaction = await TokenTransferBuilder.new()
|
|
90
|
+
.recipient(fixture.data.to, BigInt(fixture.data.value))
|
|
91
|
+
.to(fixture.data.to)
|
|
92
|
+
.nonce(fixture.data.nonce)
|
|
93
|
+
.gasPrice(fixture.data.gasPrice)
|
|
94
|
+
.gasLimit(fixture.data.gasLimit)
|
|
95
|
+
.sign(identityFixture.passphrase);
|
|
96
|
+
|
|
97
|
+
expect(transaction.toJson()).toEqual(
|
|
98
|
+
JSON.stringify({
|
|
99
|
+
gasPrice: fixture.data.gasPrice,
|
|
100
|
+
hash: fixture.data.hash,
|
|
101
|
+
gasLimit: fixture.data.gasLimit,
|
|
102
|
+
nonce: fixture.data.nonce,
|
|
103
|
+
senderPublicKey: fixture.data.senderPublicKey,
|
|
104
|
+
to: fixture.data.to,
|
|
105
|
+
value: fixture.data.value,
|
|
106
|
+
data: fixture.data.data,
|
|
107
|
+
r: fixture.data.r,
|
|
108
|
+
s: fixture.data.s,
|
|
109
|
+
v: fixture.data.v,
|
|
110
|
+
}),
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("should convert to string", async () => {
|
|
115
|
+
const transaction = await TokenTransferBuilder.new()
|
|
116
|
+
.recipient(fixture.data.to, BigInt(fixture.data.value))
|
|
117
|
+
.to(fixture.data.to)
|
|
118
|
+
.nonce(fixture.data.nonce)
|
|
119
|
+
.gasPrice(fixture.data.gasPrice)
|
|
120
|
+
.gasLimit(fixture.data.gasLimit)
|
|
121
|
+
.sign(identityFixture.passphrase);
|
|
122
|
+
|
|
123
|
+
expect(transaction.toString()).toEqual(
|
|
124
|
+
JSON.stringify({
|
|
125
|
+
gasPrice: fixture.data.gasPrice,
|
|
126
|
+
hash: fixture.data.hash,
|
|
127
|
+
gasLimit: fixture.data.gasLimit,
|
|
128
|
+
nonce: fixture.data.nonce,
|
|
129
|
+
senderPublicKey: fixture.data.senderPublicKey,
|
|
130
|
+
to: fixture.data.to,
|
|
131
|
+
value: fixture.data.value,
|
|
132
|
+
data: fixture.data.data,
|
|
133
|
+
r: fixture.data.r,
|
|
134
|
+
s: fixture.data.s,
|
|
135
|
+
v: fixture.data.v,
|
|
136
|
+
}),
|
|
137
|
+
);
|
|
138
|
+
});
|
|
@@ -46,3 +46,15 @@ it("should encode using a custom abi", () => {
|
|
|
46
46
|
|
|
47
47
|
expect(encodedData).toBe(expectedEncodedData);
|
|
48
48
|
});
|
|
49
|
+
|
|
50
|
+
it("should encode a vote function call to hex", () => {
|
|
51
|
+
const encoder = new AbiEncoder();
|
|
52
|
+
|
|
53
|
+
const functionName = "vote";
|
|
54
|
+
const args = ["0x512F366D524157BcF734546eB29a6d687B762255"];
|
|
55
|
+
const expectedEncodedData = "6dd7d8ea000000000000000000000000512f366d524157bcf734546eb29a6d687b762255";
|
|
56
|
+
|
|
57
|
+
const encodedData = encoder.encodeFunctionCallHex(functionName, args);
|
|
58
|
+
|
|
59
|
+
expect(encodedData).toBe(`0x${expectedEncodedData}`);
|
|
60
|
+
});
|
|
@@ -117,4 +117,14 @@ describe("Helpers", () => {
|
|
|
117
117
|
expect(Helpers.removeLeadingHexZero("0X1234")).toBe("0X1234"); // case sensitive
|
|
118
118
|
});
|
|
119
119
|
});
|
|
120
|
+
|
|
121
|
+
describe("addLeadingHexZero", () => {
|
|
122
|
+
it("should add the 0x prefix to string", () => {
|
|
123
|
+
expect(Helpers.addLeadingHexZero("1234")).toBe("0x1234");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("should not add the 0x prefix to string if it has already", () => {
|
|
127
|
+
expect(Helpers.addLeadingHexZero("0x1234")).toBe("0x1234");
|
|
128
|
+
});
|
|
129
|
+
});
|
|
120
130
|
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { TransactionDataEncoder } from "@/utils/TransactionDataEncoder";
|
|
2
|
+
import EncoderFixture from "@tests/fixtures/transactions/transaction-data-encoder.json";
|
|
3
|
+
|
|
4
|
+
it("should encode multi payment", () => {
|
|
5
|
+
const encoded = TransactionDataEncoder.multiPayment([EncoderFixture.Address], ["1000"]);
|
|
6
|
+
expect(encoded).toEqual(EncoderFixture.Encoded.MultiPayment);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("should encode update validator", () => {
|
|
10
|
+
const encoded = TransactionDataEncoder.updateValidator(EncoderFixture.ValidatorPublicKey);
|
|
11
|
+
expect(encoded).toEqual(EncoderFixture.Encoded.UpdateValidator);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("should encode username registration", () => {
|
|
15
|
+
const encoded = TransactionDataEncoder.usernameRegistration(EncoderFixture.Username);
|
|
16
|
+
expect(encoded).toEqual(EncoderFixture.Encoded.UsernameRegistration);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should encode username resignation", () => {
|
|
20
|
+
const encoded = TransactionDataEncoder.usernameResignation();
|
|
21
|
+
expect(encoded).toEqual(EncoderFixture.Encoded.UsernameResignation);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should encode validator registration", () => {
|
|
25
|
+
const encoded = TransactionDataEncoder.validatorRegistration(EncoderFixture.ValidatorPublicKey);
|
|
26
|
+
expect(encoded).toEqual(EncoderFixture.Encoded.ValidatorRegistration);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should encode validator resignation", () => {
|
|
30
|
+
const encoded = TransactionDataEncoder.validatorResignation();
|
|
31
|
+
expect(encoded).toEqual(EncoderFixture.Encoded.ValidatorResignation);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should encode token uransfer", () => {
|
|
35
|
+
const encoded = TransactionDataEncoder.tokenTransfer(EncoderFixture.Address, EncoderFixture.Amount);
|
|
36
|
+
expect(encoded).toEqual(EncoderFixture.Encoded.TokenTransfer);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should reject invalid address format in tokenTransfer", () => {
|
|
40
|
+
expect(() => TransactionDataEncoder.tokenTransfer("", "1000")).toThrow();
|
|
41
|
+
expect(() => TransactionDataEncoder.tokenTransfer("0x123", "1000")).toThrow();
|
|
42
|
+
expect(() => TransactionDataEncoder.tokenTransfer("0x123456789012345678901234567890123456789", "1000")).toThrow();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("should reject invalid address format in multiPayment", () => {
|
|
46
|
+
expect(() => TransactionDataEncoder.multiPayment([""], ["1000"])).toThrow();
|
|
47
|
+
expect(() => TransactionDataEncoder.multiPayment(["0x123"], ["1000"])).toThrow();
|
|
48
|
+
expect(() =>
|
|
49
|
+
TransactionDataEncoder.multiPayment(["0x123456789012345678901234567890123456789"], ["1000"]),
|
|
50
|
+
).toThrow();
|
|
51
|
+
});
|