@arkecosystem/typescript-crypto 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +16 -0
- package/.eslintrc.js +12 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +24 -0
- package/.github/workflows/ci.yml +36 -0
- package/.github/workflows/publish.yml +56 -0
- package/.vscode/settings.json +2 -0
- package/README.md +31 -0
- package/dist/configuration/Network.d.ts +7 -0
- package/dist/configuration/Network.d.ts.map +1 -0
- package/dist/configuration/Network.js +12 -0
- package/dist/configuration/index.d.ts +2 -0
- package/dist/configuration/index.d.ts.map +1 -0
- package/dist/configuration/index.js +1 -0
- package/dist/enums/AbiFunction.d.ts +10 -0
- package/dist/enums/AbiFunction.d.ts.map +1 -0
- package/dist/enums/AbiFunction.js +10 -0
- package/dist/enums/Constants.d.ts +5 -0
- package/dist/enums/Constants.d.ts.map +1 -0
- package/dist/enums/Constants.js +5 -0
- package/dist/enums/ContractAbiType.d.ts +7 -0
- package/dist/enums/ContractAbiType.d.ts.map +1 -0
- package/dist/enums/ContractAbiType.js +7 -0
- package/dist/enums/ContractAddresses.d.ts +6 -0
- package/dist/enums/ContractAddresses.d.ts.map +1 -0
- package/dist/enums/ContractAddresses.js +6 -0
- package/dist/enums/index.d.ts +5 -0
- package/dist/enums/index.d.ts.map +1 -0
- package/dist/enums/index.js +4 -0
- package/dist/exceptions/InvalidUsernameException.d.ts +4 -0
- package/dist/exceptions/InvalidUsernameException.d.ts.map +1 -0
- package/dist/exceptions/InvalidUsernameException.js +6 -0
- package/dist/exceptions/index.d.ts +2 -0
- package/dist/exceptions/index.d.ts.map +1 -0
- package/dist/exceptions/index.js +1 -0
- package/dist/identities/Address.d.ts +7 -0
- package/dist/identities/Address.d.ts.map +1 -0
- package/dist/identities/Address.js +15 -0
- package/dist/identities/PrivateKey.d.ts +16 -0
- package/dist/identities/PrivateKey.d.ts.map +1 -0
- package/dist/identities/PrivateKey.js +31 -0
- package/dist/identities/PublicKey.d.ts +8 -0
- package/dist/identities/PublicKey.d.ts.map +1 -0
- package/dist/identities/PublicKey.js +24 -0
- package/dist/identities/WIF.d.ts +4 -0
- package/dist/identities/WIF.d.ts.map +1 -0
- package/dist/identities/WIF.js +13 -0
- package/dist/identities/index.d.ts +5 -0
- package/dist/identities/index.d.ts.map +1 -0
- package/dist/identities/index.js +4 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/networks/AbstractNetwork.d.ts +6 -0
- package/dist/networks/AbstractNetwork.d.ts.map +1 -0
- package/dist/networks/AbstractNetwork.js +2 -0
- package/dist/networks/Mainnet.d.ts +7 -0
- package/dist/networks/Mainnet.d.ts.map +1 -0
- package/dist/networks/Mainnet.js +12 -0
- package/dist/networks/Testnet.d.ts +7 -0
- package/dist/networks/Testnet.d.ts.map +1 -0
- package/dist/networks/Testnet.js +12 -0
- package/dist/networks/index.d.ts +4 -0
- package/dist/networks/index.d.ts.map +1 -0
- package/dist/networks/index.js +3 -0
- package/dist/transactions/Deserializer.d.ts +18 -0
- package/dist/transactions/Deserializer.d.ts.map +1 -0
- package/dist/transactions/Deserializer.js +106 -0
- package/dist/transactions/Serializer.d.ts +9 -0
- package/dist/transactions/Serializer.d.ts.map +1 -0
- package/dist/transactions/Serializer.js +16 -0
- package/dist/transactions/builders/AbstractTransactionBuilder.d.ts +17 -0
- package/dist/transactions/builders/AbstractTransactionBuilder.d.ts.map +1 -0
- package/dist/transactions/builders/AbstractTransactionBuilder.js +54 -0
- package/dist/transactions/builders/EvmCallBuilder.d.ts +9 -0
- package/dist/transactions/builders/EvmCallBuilder.d.ts.map +1 -0
- package/dist/transactions/builders/EvmCallBuilder.js +18 -0
- package/dist/transactions/builders/MultipaymentBuilder.d.ts +9 -0
- package/dist/transactions/builders/MultipaymentBuilder.d.ts.map +1 -0
- package/dist/transactions/builders/MultipaymentBuilder.js +25 -0
- package/dist/transactions/builders/TransferBuilder.d.ts +8 -0
- package/dist/transactions/builders/TransferBuilder.d.ts.map +1 -0
- package/dist/transactions/builders/TransferBuilder.js +15 -0
- package/dist/transactions/builders/UnvoteBuilder.d.ts +8 -0
- package/dist/transactions/builders/UnvoteBuilder.d.ts.map +1 -0
- package/dist/transactions/builders/UnvoteBuilder.js +15 -0
- package/dist/transactions/builders/UsernameRegistrationBuilder.d.ts +9 -0
- package/dist/transactions/builders/UsernameRegistrationBuilder.d.ts.map +1 -0
- package/dist/transactions/builders/UsernameRegistrationBuilder.js +22 -0
- package/dist/transactions/builders/UsernameResignationBuilder.d.ts +8 -0
- package/dist/transactions/builders/UsernameResignationBuilder.d.ts.map +1 -0
- package/dist/transactions/builders/UsernameResignationBuilder.js +15 -0
- package/dist/transactions/builders/ValidatorRegistrationBuilder.d.ts +9 -0
- package/dist/transactions/builders/ValidatorRegistrationBuilder.d.ts.map +1 -0
- package/dist/transactions/builders/ValidatorRegistrationBuilder.js +20 -0
- package/dist/transactions/builders/ValidatorResignationBuilder.d.ts +8 -0
- package/dist/transactions/builders/ValidatorResignationBuilder.d.ts.map +1 -0
- package/dist/transactions/builders/ValidatorResignationBuilder.js +15 -0
- package/dist/transactions/builders/VoteBuilder.d.ts +9 -0
- package/dist/transactions/builders/VoteBuilder.d.ts.map +1 -0
- package/dist/transactions/builders/VoteBuilder.js +20 -0
- package/dist/transactions/builders/index.d.ts +11 -0
- package/dist/transactions/builders/index.d.ts.map +1 -0
- package/dist/transactions/builders/index.js +10 -0
- package/dist/transactions/index.d.ts +5 -0
- package/dist/transactions/index.d.ts.map +1 -0
- package/dist/transactions/index.js +4 -0
- package/dist/transactions/types/AbstractTransaction.d.ts +22 -0
- package/dist/transactions/types/AbstractTransaction.d.ts.map +1 -0
- package/dist/transactions/types/AbstractTransaction.js +79 -0
- package/dist/transactions/types/EvmCall.d.ts +8 -0
- package/dist/transactions/types/EvmCall.d.ts.map +1 -0
- package/dist/transactions/types/EvmCall.js +14 -0
- package/dist/transactions/types/Multipayment.d.ts +9 -0
- package/dist/transactions/types/Multipayment.d.ts.map +1 -0
- package/dist/transactions/types/Multipayment.js +38 -0
- package/dist/transactions/types/Transfer.d.ts +8 -0
- package/dist/transactions/types/Transfer.d.ts.map +1 -0
- package/dist/transactions/types/Transfer.js +14 -0
- package/dist/transactions/types/Unvote.d.ts +8 -0
- package/dist/transactions/types/Unvote.d.ts.map +1 -0
- package/dist/transactions/types/Unvote.js +16 -0
- package/dist/transactions/types/UsernameRegistration.d.ts +9 -0
- package/dist/transactions/types/UsernameRegistration.d.ts.map +1 -0
- package/dist/transactions/types/UsernameRegistration.js +33 -0
- package/dist/transactions/types/UsernameResignation.d.ts +8 -0
- package/dist/transactions/types/UsernameResignation.d.ts.map +1 -0
- package/dist/transactions/types/UsernameResignation.js +16 -0
- package/dist/transactions/types/ValidatorRegistration.d.ts +9 -0
- package/dist/transactions/types/ValidatorRegistration.d.ts.map +1 -0
- package/dist/transactions/types/ValidatorRegistration.js +33 -0
- package/dist/transactions/types/ValidatorResignation.d.ts +8 -0
- package/dist/transactions/types/ValidatorResignation.d.ts.map +1 -0
- package/dist/transactions/types/ValidatorResignation.js +16 -0
- package/dist/transactions/types/Vote.d.ts +9 -0
- package/dist/transactions/types/Vote.d.ts.map +1 -0
- package/dist/transactions/types/Vote.js +31 -0
- package/dist/transactions/types/index.d.ts +11 -0
- package/dist/transactions/types/index.d.ts.map +1 -0
- package/dist/transactions/types/index.js +10 -0
- package/dist/types.d.ts +72 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/utils/Abi/ArgumentDecoder.d.ts +10 -0
- package/dist/utils/Abi/ArgumentDecoder.d.ts.map +1 -0
- package/dist/utils/Abi/ArgumentDecoder.js +21 -0
- package/dist/utils/Abi/json/Abi.Consensus.json +1673 -0
- package/dist/utils/Abi/json/Abi.Multipayment.json +110 -0
- package/dist/utils/Abi/json/Abi.Usernames.json +900 -0
- package/dist/utils/AbiBase.d.ts +8 -0
- package/dist/utils/AbiBase.d.ts.map +1 -0
- package/dist/utils/AbiBase.js +25 -0
- package/dist/utils/AbiDecoder.d.ts +6 -0
- package/dist/utils/AbiDecoder.d.ts.map +1 -0
- package/dist/utils/AbiDecoder.js +14 -0
- package/dist/utils/AbiEncoder.d.ts +5 -0
- package/dist/utils/AbiEncoder.d.ts.map +1 -0
- package/dist/utils/AbiEncoder.js +6 -0
- package/dist/utils/Helpers.d.ts +19 -0
- package/dist/utils/Helpers.d.ts.map +1 -0
- package/dist/utils/Helpers.js +38 -0
- package/dist/utils/Message.d.ts +14 -0
- package/dist/utils/Message.d.ts.map +1 -0
- package/dist/utils/Message.js +43 -0
- package/dist/utils/Slot.d.ts +5 -0
- package/dist/utils/Slot.d.ts.map +1 -0
- package/dist/utils/Slot.js +9 -0
- package/dist/utils/TransactionUtils.d.ts +7 -0
- package/dist/utils/TransactionUtils.d.ts.map +1 -0
- package/dist/utils/TransactionUtils.js +35 -0
- package/dist/utils/UnitConverter.d.ts +8 -0
- package/dist/utils/UnitConverter.d.ts.map +1 -0
- package/dist/utils/UnitConverter.js +48 -0
- package/dist/utils/index.d.ts +9 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +8 -0
- package/jest.config.ts +14 -0
- package/package.json +59 -0
- package/prettier.config.js +8 -0
- package/src/configuration/Network.ts +18 -0
- package/src/configuration/index.ts +1 -0
- package/src/enums/AbiFunction.ts +9 -0
- package/src/enums/Constants.ts +4 -0
- package/src/enums/ContractAbiType.ts +6 -0
- package/src/enums/ContractAddresses.ts +5 -0
- package/src/enums/index.ts +4 -0
- package/src/exceptions/InvalidUsernameException.ts +6 -0
- package/src/exceptions/index.ts +1 -0
- package/src/identities/Address.ts +19 -0
- package/src/identities/PrivateKey.ts +49 -0
- package/src/identities/PublicKey.ts +28 -0
- package/src/identities/WIF.ts +16 -0
- package/src/identities/index.ts +4 -0
- package/src/index.ts +7 -0
- package/src/networks/AbstractNetwork.ts +7 -0
- package/src/networks/Mainnet.ts +15 -0
- package/src/networks/Testnet.ts +15 -0
- package/src/networks/index.ts +3 -0
- package/src/transactions/Deserializer.ts +140 -0
- package/src/transactions/Serializer.ts +24 -0
- package/src/transactions/builders/AbstractTransactionBuilder.ts +81 -0
- package/src/transactions/builders/EvmCallBuilder.ts +24 -0
- package/src/transactions/builders/MultipaymentBuilder.ts +37 -0
- package/src/transactions/builders/TransferBuilder.ts +22 -0
- package/src/transactions/builders/UnvoteBuilder.ts +21 -0
- package/src/transactions/builders/UsernameRegistrationBuilder.ts +35 -0
- package/src/transactions/builders/UsernameResignationBuilder.ts +21 -0
- package/src/transactions/builders/ValidatorRegistrationBuilder.ts +32 -0
- package/src/transactions/builders/ValidatorResignationBuilder.ts +21 -0
- package/src/transactions/builders/VoteBuilder.ts +29 -0
- package/src/transactions/builders/index.ts +10 -0
- package/src/transactions/index.ts +4 -0
- package/src/transactions/types/AbstractTransaction.ts +118 -0
- package/src/transactions/types/EvmCall.ts +22 -0
- package/src/transactions/types/Multipayment.ts +51 -0
- package/src/transactions/types/Transfer.ts +22 -0
- package/src/transactions/types/Unvote.ts +24 -0
- package/src/transactions/types/UsernameRegistration.ts +44 -0
- package/src/transactions/types/UsernameResignation.ts +24 -0
- package/src/transactions/types/ValidatorRegistration.ts +44 -0
- package/src/transactions/types/ValidatorResignation.ts +24 -0
- package/src/transactions/types/Vote.ts +42 -0
- package/src/transactions/types/index.ts +10 -0
- package/src/types.ts +86 -0
- package/src/utils/Abi/ArgumentDecoder.ts +29 -0
- package/src/utils/Abi/json/Abi.Consensus.json +1673 -0
- package/src/utils/Abi/json/Abi.Multipayment.json +110 -0
- package/src/utils/Abi/json/Abi.Usernames.json +900 -0
- package/src/utils/AbiBase.ts +32 -0
- package/src/utils/AbiDecoder.ts +19 -0
- package/src/utils/AbiEncoder.ts +7 -0
- package/src/utils/Helpers.ts +46 -0
- package/src/utils/Message.ts +63 -0
- package/src/utils/Slot.ts +11 -0
- package/src/utils/TransactionUtils.ts +43 -0
- package/src/utils/UnitConverter.ts +57 -0
- package/src/utils/index.ts +8 -0
- package/tests/fixtures/global.json +10 -0
- package/tests/fixtures/identity.json +11 -0
- package/tests/fixtures/message-sign.json +5 -0
- package/tests/fixtures/sign-compact.json +10 -0
- package/tests/fixtures/transactions/evm-sign.json +18 -0
- package/tests/fixtures/transactions/multipayment-empty.json +18 -0
- package/tests/fixtures/transactions/multipayment-single.json +18 -0
- package/tests/fixtures/transactions/multipayment.json +18 -0
- package/tests/fixtures/transactions/transfer-0.json +18 -0
- package/tests/fixtures/transactions/transfer-large-amount.json +18 -0
- package/tests/fixtures/transactions/transfer.json +18 -0
- package/tests/fixtures/transactions/unvote.json +18 -0
- package/tests/fixtures/transactions/username-registration.json +18 -0
- package/tests/fixtures/transactions/username-resignation.json +18 -0
- package/tests/fixtures/transactions/validator-registration.json +18 -0
- package/tests/fixtures/transactions/validator-resignation.json +18 -0
- package/tests/fixtures/transactions/vote.json +18 -0
- package/tests/unit/identities/Address.test.ts +26 -0
- package/tests/unit/identities/PrivateKey.test.ts +36 -0
- package/tests/unit/identities/PublicKey.test.ts +39 -0
- package/tests/unit/identities/WIF.test.ts +8 -0
- package/tests/unit/transactions/Deserializer.test.ts +138 -0
- package/tests/unit/transactions/Serializer.test.ts +9 -0
- package/tests/unit/transactions/builders/EvmCallBuilder.test.ts +49 -0
- package/tests/unit/transactions/builders/MultipaymentBuilder.test.ts +99 -0
- package/tests/unit/transactions/builders/TransferBuilder.test.ts +73 -0
- package/tests/unit/transactions/builders/UnvoteBuilder.test.ts +47 -0
- package/tests/unit/transactions/builders/UsernameRegistrationBuilder.test.ts +54 -0
- package/tests/unit/transactions/builders/UsernameResignationBuilder.test.ts +48 -0
- package/tests/unit/transactions/builders/ValidatorRegistrationBuilder.test.ts +51 -0
- package/tests/unit/transactions/builders/ValidatorResignationBuilder.test.ts +48 -0
- package/tests/unit/transactions/builders/VoteBuilder.test.ts +48 -0
- package/tests/unit/transactions/types/AbstractTransaction.test.ts +145 -0
- package/tests/unit/utils/Abi/ArgumentDecoder.test.ts +55 -0
- package/tests/unit/utils/AbiDecoder.test.ts +44 -0
- package/tests/unit/utils/AbiEncoder.test.ts +30 -0
- package/tests/unit/utils/Helpers.test.ts +120 -0
- package/tests/unit/utils/Message.test.ts +104 -0
- package/tests/unit/utils/Slot.test.ts +69 -0
- package/tests/unit/utils/TransactionUtils.test.ts +31 -0
- package/tests/unit/utils/UnitConverter.test.ts +71 -0
- package/tsconfig.json +20 -0
@@ -0,0 +1,118 @@
|
|
1
|
+
import { AbiResult, ITransaction, TransactionData } from "@/types";
|
2
|
+
|
3
|
+
import { Address } from "@/identities/Address";
|
4
|
+
import { Constants } from "@/enums/Constants";
|
5
|
+
import { ContractAbiType } from "@/enums/ContractAbiType";
|
6
|
+
import { Network } from "@/configuration/Network";
|
7
|
+
import { PrivateKey } from "@/identities/PrivateKey";
|
8
|
+
import { PublicKey } from "@/identities/PublicKey";
|
9
|
+
import { Serializer } from "@/transactions/Serializer";
|
10
|
+
import { TransactionUtils } from "@/utils/TransactionUtils";
|
11
|
+
|
12
|
+
export abstract class AbstractTransaction implements ITransaction {
|
13
|
+
public data: TransactionData;
|
14
|
+
public serialized: Buffer | undefined;
|
15
|
+
|
16
|
+
public constructor(data: TransactionData) {
|
17
|
+
this.data = { ...data };
|
18
|
+
|
19
|
+
this.refreshPayloadData();
|
20
|
+
}
|
21
|
+
|
22
|
+
public abstract getPayload(): string;
|
23
|
+
|
24
|
+
public refreshPayloadData(): this {
|
25
|
+
this.data.data = this.getPayload();
|
26
|
+
|
27
|
+
return this;
|
28
|
+
}
|
29
|
+
|
30
|
+
public getId(): string {
|
31
|
+
return TransactionUtils.getId(this.data);
|
32
|
+
}
|
33
|
+
|
34
|
+
public async sign(privateKey: PrivateKey): Promise<ITransaction> {
|
35
|
+
const hash = TransactionUtils.toBuffer(this.data, true).toString("binary");
|
36
|
+
|
37
|
+
const signature = await privateKey.sign(hash);
|
38
|
+
|
39
|
+
this.data["v"] = signature.v;
|
40
|
+
this.data["r"] = signature.r;
|
41
|
+
this.data["s"] = signature.s;
|
42
|
+
|
43
|
+
return this;
|
44
|
+
}
|
45
|
+
|
46
|
+
public recoverSender(): void {
|
47
|
+
const publicKey = this.recoverPublicKey();
|
48
|
+
|
49
|
+
this.data["senderPublicKey"] = publicKey.publicKey;
|
50
|
+
this.data["senderAddress"] = Address.fromPublicKey(publicKey.publicKey);
|
51
|
+
}
|
52
|
+
|
53
|
+
public verify(): boolean {
|
54
|
+
const recoveredPublicKey = this.recoverPublicKey();
|
55
|
+
|
56
|
+
const senderPublicKey = this.data.senderPublicKey;
|
57
|
+
if (!senderPublicKey) {
|
58
|
+
return false;
|
59
|
+
}
|
60
|
+
|
61
|
+
return recoveredPublicKey.publicKey === senderPublicKey;
|
62
|
+
}
|
63
|
+
|
64
|
+
public toArray(): TransactionData {
|
65
|
+
return [
|
66
|
+
"gasPrice",
|
67
|
+
"network",
|
68
|
+
"id",
|
69
|
+
"gasLimit",
|
70
|
+
"nonce",
|
71
|
+
"senderPublicKey",
|
72
|
+
"recipientAddress",
|
73
|
+
"value",
|
74
|
+
"data",
|
75
|
+
"r",
|
76
|
+
"s",
|
77
|
+
"v",
|
78
|
+
].reduce((acc: TransactionData, key) => {
|
79
|
+
if (this.data[key] !== undefined) {
|
80
|
+
acc[key] = this.data[key];
|
81
|
+
}
|
82
|
+
|
83
|
+
return acc;
|
84
|
+
}, {}) as TransactionData;
|
85
|
+
}
|
86
|
+
|
87
|
+
public toJson(): string {
|
88
|
+
return JSON.stringify(this.toArray());
|
89
|
+
}
|
90
|
+
|
91
|
+
public hash(skipSignature: boolean = false): string {
|
92
|
+
return TransactionUtils.toHash(this.data, skipSignature);
|
93
|
+
}
|
94
|
+
|
95
|
+
public serialize(skipSignature: boolean = false): Buffer {
|
96
|
+
return Serializer.new(this).serialize(skipSignature);
|
97
|
+
}
|
98
|
+
|
99
|
+
protected recoverPublicKey(): PublicKey {
|
100
|
+
if (!this.data.v || !this.data.r || !this.data.s) {
|
101
|
+
throw new Error("Transaction signature is missing");
|
102
|
+
}
|
103
|
+
|
104
|
+
return PublicKey.recover(
|
105
|
+
Buffer.from(this.hash(true), "hex"),
|
106
|
+
BigInt(`0x${this.data.r}`),
|
107
|
+
BigInt(`0x${this.data.s}`),
|
108
|
+
this.data.v - Constants.ETHEREUM_RECOVERY_ID_OFFSET,
|
109
|
+
);
|
110
|
+
}
|
111
|
+
|
112
|
+
protected decodePayload(
|
113
|
+
data: TransactionData,
|
114
|
+
type: ContractAbiType = ContractAbiType.CONSENSUS,
|
115
|
+
): AbiResult | null {
|
116
|
+
throw new Error("Method not implemented.");
|
117
|
+
}
|
118
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { AbiResult, TransactionData } from "@/types";
|
2
|
+
|
3
|
+
import { AbstractTransaction } from "./AbstractTransaction";
|
4
|
+
import { ContractAbiType } from "@/enums/ContractAbiType";
|
5
|
+
import { Deserializer } from "@/transactions/Deserializer";
|
6
|
+
|
7
|
+
export class EvmCall extends AbstractTransaction {
|
8
|
+
getPayload(): string {
|
9
|
+
return this.data["data"] || "";
|
10
|
+
}
|
11
|
+
|
12
|
+
protected decodePayload(
|
13
|
+
data: TransactionData,
|
14
|
+
type: ContractAbiType = ContractAbiType.CONSENSUS,
|
15
|
+
): AbiResult | null {
|
16
|
+
if (!data["data"]) {
|
17
|
+
return null;
|
18
|
+
}
|
19
|
+
|
20
|
+
return Deserializer.decodePayload(data, type);
|
21
|
+
}
|
22
|
+
}
|
@@ -0,0 +1,51 @@
|
|
1
|
+
import { AbiResult, TransactionData } from "@/types";
|
2
|
+
|
3
|
+
import { AbiDecoder } from "@/utils/AbiDecoder";
|
4
|
+
import { AbiEncoder } from "@/utils/AbiEncoder";
|
5
|
+
import { AbiFunction } from "@/enums/AbiFunction";
|
6
|
+
import { AbstractTransaction } from "./AbstractTransaction";
|
7
|
+
import { ContractAbiType } from "@/enums/ContractAbiType";
|
8
|
+
import { Deserializer } from "@/transactions/Deserializer";
|
9
|
+
|
10
|
+
export class Multipayment extends AbstractTransaction {
|
11
|
+
public constructor(data: TransactionData) {
|
12
|
+
let payload;
|
13
|
+
if (!data["data"] || data["data"] === "") {
|
14
|
+
payload = null;
|
15
|
+
} else {
|
16
|
+
payload = new AbiDecoder(ContractAbiType.MULTIPAYMENT).decodeFunctionData(data["data"]);
|
17
|
+
}
|
18
|
+
|
19
|
+
if (payload && payload.args[0].length === payload.args[1].length) {
|
20
|
+
data["pay"] = payload.args;
|
21
|
+
data["value"] = payload.args[1].reduce((a: bigint, b: bigint) => a + b);
|
22
|
+
} else {
|
23
|
+
data["pay"] = [[], []];
|
24
|
+
data["value"] = "0";
|
25
|
+
}
|
26
|
+
|
27
|
+
super(data);
|
28
|
+
}
|
29
|
+
|
30
|
+
getPayload(): string {
|
31
|
+
if (!this.data["pay"]) {
|
32
|
+
return "";
|
33
|
+
}
|
34
|
+
|
35
|
+
return new AbiEncoder(ContractAbiType.MULTIPAYMENT).encodeFunctionCall(
|
36
|
+
AbiFunction.MULTIPAYMENT,
|
37
|
+
this.data["pay"] as any[],
|
38
|
+
);
|
39
|
+
}
|
40
|
+
|
41
|
+
protected decodePayload(
|
42
|
+
data: TransactionData,
|
43
|
+
type: ContractAbiType = ContractAbiType.CONSENSUS,
|
44
|
+
): AbiResult | null {
|
45
|
+
if (!data["data"]) {
|
46
|
+
return null;
|
47
|
+
}
|
48
|
+
|
49
|
+
return Deserializer.decodePayload(data, ContractAbiType.MULTIPAYMENT);
|
50
|
+
}
|
51
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { AbiResult, TransactionData } from "@/types";
|
2
|
+
|
3
|
+
import { AbstractTransaction } from "./AbstractTransaction";
|
4
|
+
import { ContractAbiType } from "@/enums/ContractAbiType";
|
5
|
+
import { Deserializer } from "@/transactions/Deserializer";
|
6
|
+
|
7
|
+
export class Transfer extends AbstractTransaction {
|
8
|
+
getPayload(): string {
|
9
|
+
return "";
|
10
|
+
}
|
11
|
+
|
12
|
+
protected decodePayload(
|
13
|
+
data: TransactionData,
|
14
|
+
type: ContractAbiType = ContractAbiType.CONSENSUS,
|
15
|
+
): AbiResult | null {
|
16
|
+
if (!data["data"]) {
|
17
|
+
return null;
|
18
|
+
}
|
19
|
+
|
20
|
+
return Deserializer.decodePayload(data, type);
|
21
|
+
}
|
22
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import { AbiResult, TransactionData } from "@/types";
|
2
|
+
|
3
|
+
import { AbiEncoder } from "@/utils/AbiEncoder";
|
4
|
+
import { AbiFunction } from "@/enums/AbiFunction";
|
5
|
+
import { AbstractTransaction } from "./AbstractTransaction";
|
6
|
+
import { ContractAbiType } from "@/enums/ContractAbiType";
|
7
|
+
import { Deserializer } from "@/transactions/Deserializer";
|
8
|
+
|
9
|
+
export class Unvote extends AbstractTransaction {
|
10
|
+
getPayload(): string {
|
11
|
+
return new AbiEncoder().encodeFunctionCall(AbiFunction.UNVOTE, []);
|
12
|
+
}
|
13
|
+
|
14
|
+
protected decodePayload(
|
15
|
+
data: TransactionData,
|
16
|
+
type: ContractAbiType = ContractAbiType.CONSENSUS,
|
17
|
+
): AbiResult | null {
|
18
|
+
if (!data["data"]) {
|
19
|
+
return null;
|
20
|
+
}
|
21
|
+
|
22
|
+
return Deserializer.decodePayload(data, type);
|
23
|
+
}
|
24
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
import { AbiResult, TransactionData } from "@/types";
|
2
|
+
|
3
|
+
import { AbiDecoder } from "@/utils/AbiDecoder";
|
4
|
+
import { AbiEncoder } from "@/utils/AbiEncoder";
|
5
|
+
import { AbiFunction } from "@/enums/AbiFunction";
|
6
|
+
import { AbstractTransaction } from "./AbstractTransaction";
|
7
|
+
import { ContractAbiType } from "@/enums/ContractAbiType";
|
8
|
+
import { Deserializer } from "@/transactions/Deserializer";
|
9
|
+
|
10
|
+
export class UsernameRegistration extends AbstractTransaction {
|
11
|
+
public constructor(data: TransactionData) {
|
12
|
+
let payload;
|
13
|
+
if (!data["data"] || data["data"] === "") {
|
14
|
+
payload = null;
|
15
|
+
} else {
|
16
|
+
payload = new AbiDecoder(ContractAbiType.USERNAMES).decodeFunctionData(data["data"]);
|
17
|
+
}
|
18
|
+
|
19
|
+
data["username"] = payload ? payload.args[0] : null;
|
20
|
+
|
21
|
+
super(data);
|
22
|
+
}
|
23
|
+
|
24
|
+
getPayload(): string {
|
25
|
+
if (!this.data["username"]) {
|
26
|
+
return "";
|
27
|
+
}
|
28
|
+
|
29
|
+
return new AbiEncoder(ContractAbiType.USERNAMES).encodeFunctionCall(AbiFunction.USERNAME_REGISTRATION, [
|
30
|
+
this.data["username"],
|
31
|
+
]);
|
32
|
+
}
|
33
|
+
|
34
|
+
protected decodePayload(
|
35
|
+
data: TransactionData,
|
36
|
+
type: ContractAbiType = ContractAbiType.CONSENSUS,
|
37
|
+
): AbiResult | null {
|
38
|
+
if (!data["data"]) {
|
39
|
+
return null;
|
40
|
+
}
|
41
|
+
|
42
|
+
return Deserializer.decodePayload(data, ContractAbiType.USERNAMES);
|
43
|
+
}
|
44
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import { AbiResult, TransactionData } from "@/types";
|
2
|
+
|
3
|
+
import { AbiEncoder } from "@/utils/AbiEncoder";
|
4
|
+
import { AbiFunction } from "@/enums/AbiFunction";
|
5
|
+
import { AbstractTransaction } from "./AbstractTransaction";
|
6
|
+
import { ContractAbiType } from "@/enums/ContractAbiType";
|
7
|
+
import { Deserializer } from "@/transactions/Deserializer";
|
8
|
+
|
9
|
+
export class UsernameResignation extends AbstractTransaction {
|
10
|
+
getPayload(): string {
|
11
|
+
return new AbiEncoder(ContractAbiType.USERNAMES).encodeFunctionCall(AbiFunction.USERNAME_RESIGNATION, []);
|
12
|
+
}
|
13
|
+
|
14
|
+
protected decodePayload(
|
15
|
+
data: TransactionData,
|
16
|
+
type: ContractAbiType = ContractAbiType.CONSENSUS,
|
17
|
+
): AbiResult | null {
|
18
|
+
if (!data["data"]) {
|
19
|
+
return null;
|
20
|
+
}
|
21
|
+
|
22
|
+
return Deserializer.decodePayload(data, ContractAbiType.USERNAMES);
|
23
|
+
}
|
24
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
import { AbiResult, TransactionData } from "@/types";
|
2
|
+
|
3
|
+
import { AbiDecoder } from "@/utils/AbiDecoder";
|
4
|
+
import { AbiEncoder } from "@/utils/AbiEncoder";
|
5
|
+
import { AbiFunction } from "@/enums/AbiFunction";
|
6
|
+
import { AbstractTransaction } from "./AbstractTransaction";
|
7
|
+
import { ContractAbiType } from "@/enums/ContractAbiType";
|
8
|
+
import { Deserializer } from "@/transactions/Deserializer";
|
9
|
+
|
10
|
+
export class ValidatorRegistration extends AbstractTransaction {
|
11
|
+
public constructor(data: TransactionData) {
|
12
|
+
let payload;
|
13
|
+
if (!data["data"] || data["data"] === "") {
|
14
|
+
payload = null;
|
15
|
+
} else {
|
16
|
+
payload = new AbiDecoder().decodeFunctionData(data["data"]);
|
17
|
+
}
|
18
|
+
|
19
|
+
data["validatorPublicKey"] = payload ? payload.args[0] : null;
|
20
|
+
|
21
|
+
super(data);
|
22
|
+
}
|
23
|
+
|
24
|
+
getPayload(): string {
|
25
|
+
if (!this.data["validatorPublicKey"]) {
|
26
|
+
return "";
|
27
|
+
}
|
28
|
+
|
29
|
+
return new AbiEncoder().encodeFunctionCall(AbiFunction.VALIDATOR_REGISTRATION, [
|
30
|
+
this.data["validatorPublicKey"],
|
31
|
+
]);
|
32
|
+
}
|
33
|
+
|
34
|
+
protected decodePayload(
|
35
|
+
data: TransactionData,
|
36
|
+
type: ContractAbiType = ContractAbiType.CONSENSUS,
|
37
|
+
): AbiResult | null {
|
38
|
+
if (!data["data"]) {
|
39
|
+
return null;
|
40
|
+
}
|
41
|
+
|
42
|
+
return Deserializer.decodePayload(data, type);
|
43
|
+
}
|
44
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import { AbiResult, TransactionData } from "@/types";
|
2
|
+
|
3
|
+
import { AbiEncoder } from "@/utils/AbiEncoder";
|
4
|
+
import { AbiFunction } from "@/enums/AbiFunction";
|
5
|
+
import { AbstractTransaction } from "./AbstractTransaction";
|
6
|
+
import { ContractAbiType } from "@/enums/ContractAbiType";
|
7
|
+
import { Deserializer } from "@/transactions/Deserializer";
|
8
|
+
|
9
|
+
export class ValidatorResignation extends AbstractTransaction {
|
10
|
+
getPayload(): string {
|
11
|
+
return new AbiEncoder().encodeFunctionCall(AbiFunction.VALIDATOR_RESIGNATION, []);
|
12
|
+
}
|
13
|
+
|
14
|
+
protected decodePayload(
|
15
|
+
data: TransactionData,
|
16
|
+
type: ContractAbiType = ContractAbiType.CONSENSUS,
|
17
|
+
): AbiResult | null {
|
18
|
+
if (!data["data"]) {
|
19
|
+
return null;
|
20
|
+
}
|
21
|
+
|
22
|
+
return Deserializer.decodePayload(data, type);
|
23
|
+
}
|
24
|
+
}
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import { AbiResult, TransactionData } from "@/types";
|
2
|
+
|
3
|
+
import { AbiDecoder } from "@/utils/AbiDecoder";
|
4
|
+
import { AbiEncoder } from "@/utils/AbiEncoder";
|
5
|
+
import { AbiFunction } from "@/enums/AbiFunction";
|
6
|
+
import { AbstractTransaction } from "./AbstractTransaction";
|
7
|
+
import { ContractAbiType } from "@/enums/ContractAbiType";
|
8
|
+
import { Deserializer } from "@/transactions/Deserializer";
|
9
|
+
|
10
|
+
export class Vote extends AbstractTransaction {
|
11
|
+
public constructor(data: TransactionData) {
|
12
|
+
let payload;
|
13
|
+
if (!data["data"] || data["data"] === "") {
|
14
|
+
payload = null;
|
15
|
+
} else {
|
16
|
+
payload = new AbiDecoder().decodeFunctionData(data["data"]);
|
17
|
+
}
|
18
|
+
|
19
|
+
data["vote"] = payload ? payload.args[0] : null;
|
20
|
+
|
21
|
+
super(data);
|
22
|
+
}
|
23
|
+
|
24
|
+
getPayload(): string {
|
25
|
+
if (!this.data["vote"]) {
|
26
|
+
return "";
|
27
|
+
}
|
28
|
+
|
29
|
+
return new AbiEncoder().encodeFunctionCall(AbiFunction.VOTE, [this.data["vote"]]);
|
30
|
+
}
|
31
|
+
|
32
|
+
protected decodePayload(
|
33
|
+
data: TransactionData,
|
34
|
+
type: ContractAbiType = ContractAbiType.CONSENSUS,
|
35
|
+
): AbiResult | null {
|
36
|
+
if (!data["data"]) {
|
37
|
+
return null;
|
38
|
+
}
|
39
|
+
|
40
|
+
return Deserializer.decodePayload(data, type);
|
41
|
+
}
|
42
|
+
}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
export * from "./AbstractTransaction";
|
2
|
+
export * from "./EvmCall";
|
3
|
+
export * from "./Multipayment";
|
4
|
+
export * from "./Transfer";
|
5
|
+
export * from "./Unvote";
|
6
|
+
export * from "./ValidatorRegistration";
|
7
|
+
export * from "./ValidatorResignation";
|
8
|
+
export * from "./UsernameRegistration";
|
9
|
+
export * from "./UsernameResignation";
|
10
|
+
export * from "./Vote";
|
package/src/types.ts
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
import { Result } from "ethers";
|
2
|
+
|
3
|
+
export type TransactionData = {
|
4
|
+
gasPrice?: string | null;
|
5
|
+
network?: number | string | null;
|
6
|
+
id?: string | null;
|
7
|
+
gasLimit?: string | null;
|
8
|
+
nonce?: number | string | null;
|
9
|
+
senderPublicKey?: string | null;
|
10
|
+
recipientAddress?: string | null;
|
11
|
+
value?: string | null;
|
12
|
+
data?: string | null;
|
13
|
+
r?: string;
|
14
|
+
s?: string;
|
15
|
+
v?: number;
|
16
|
+
|
17
|
+
[key: string]: number | string | undefined | null | Array<any> | string;
|
18
|
+
};
|
19
|
+
|
20
|
+
export type AbiResult = {
|
21
|
+
functionName: string;
|
22
|
+
args: Result;
|
23
|
+
};
|
24
|
+
|
25
|
+
export type SignedMessage = {
|
26
|
+
publicKey: string;
|
27
|
+
signature: string;
|
28
|
+
message: string;
|
29
|
+
};
|
30
|
+
|
31
|
+
export interface ITransaction {
|
32
|
+
data: TransactionData;
|
33
|
+
serialized: Buffer | undefined;
|
34
|
+
|
35
|
+
getPayload(): string;
|
36
|
+
refreshPayloadData(): this;
|
37
|
+
getId(): string;
|
38
|
+
sign(privateKey: any): Promise<ITransaction>;
|
39
|
+
recoverSender(): void;
|
40
|
+
|
41
|
+
verify(): boolean;
|
42
|
+
toArray(): TransactionData;
|
43
|
+
toJson(): string;
|
44
|
+
hash(skipSignature?: boolean): string;
|
45
|
+
serialize(skipSignature?: boolean): Buffer;
|
46
|
+
}
|
47
|
+
|
48
|
+
export interface ITransactionBuilder<T extends ITransactionBuilder<T>> {
|
49
|
+
transaction: ITransaction;
|
50
|
+
|
51
|
+
gasLimit(gasLimit: string): T;
|
52
|
+
gasPrice(gasPrice: string): T;
|
53
|
+
recipientAddress(recipientAddress: string): T;
|
54
|
+
nonce(nonce: string): T;
|
55
|
+
network(network: number): T;
|
56
|
+
sign(passphrase: string): Promise<T>;
|
57
|
+
verify(): boolean;
|
58
|
+
toArray(): TransactionData;
|
59
|
+
toJson(): string;
|
60
|
+
}
|
61
|
+
|
62
|
+
export interface ITransferBuilder extends ITransactionBuilder<ITransferBuilder> {
|
63
|
+
value(value: string): ITransferBuilder;
|
64
|
+
}
|
65
|
+
|
66
|
+
export interface IVoteBuilder extends ITransactionBuilder<IVoteBuilder> {
|
67
|
+
vote(vote: string): IVoteBuilder;
|
68
|
+
}
|
69
|
+
|
70
|
+
export interface IValidatorRegistrationBuilder extends ITransactionBuilder<IValidatorRegistrationBuilder> {
|
71
|
+
validatorPublicKey(validatorPublicKey: string): IValidatorRegistrationBuilder;
|
72
|
+
}
|
73
|
+
|
74
|
+
export interface IUsernameRegistrationBuilder extends ITransactionBuilder<IUsernameRegistrationBuilder> {
|
75
|
+
username(username: string): IUsernameRegistrationBuilder;
|
76
|
+
}
|
77
|
+
|
78
|
+
export interface IEvmCallBuilder extends ITransactionBuilder<IEvmCallBuilder> {
|
79
|
+
payload(payload: string): IEvmCallBuilder;
|
80
|
+
}
|
81
|
+
|
82
|
+
export interface IMultipaymentBuilder extends ITransactionBuilder<IMultipaymentBuilder> {
|
83
|
+
pay(address: string, amount: string): IMultipaymentBuilder;
|
84
|
+
}
|
85
|
+
|
86
|
+
export interface IBasicTransactionBuilder extends ITransactionBuilder<IBasicTransactionBuilder> {}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import { AbiCoder } from "ethers";
|
2
|
+
|
3
|
+
export class ArgumentDecoder {
|
4
|
+
private bytes: Buffer;
|
5
|
+
|
6
|
+
constructor(bytes: string) {
|
7
|
+
this.bytes = Buffer.from(bytes, 'hex');
|
8
|
+
}
|
9
|
+
|
10
|
+
decodeString(): string {
|
11
|
+
return new AbiCoder().decode(['string'], this.bytes)[0].toString();
|
12
|
+
}
|
13
|
+
|
14
|
+
decodeAddress(): string {
|
15
|
+
return new AbiCoder().decode(['address'], this.bytes).toString();
|
16
|
+
}
|
17
|
+
|
18
|
+
decodeUnsignedInt(): string {
|
19
|
+
return new AbiCoder().decode(['uint'], this.bytes)[0].toString();
|
20
|
+
}
|
21
|
+
|
22
|
+
decodeSignedInt(): string {
|
23
|
+
return new AbiCoder().decode(['int'], this.bytes)[0].toString();
|
24
|
+
}
|
25
|
+
|
26
|
+
decodeBool(): boolean {
|
27
|
+
return new AbiCoder().decode(['bool'], this.bytes)[0] === true;
|
28
|
+
}
|
29
|
+
}
|