@arkecosystem/typescript-crypto 0.0.5 → 0.0.6
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/identities/Address.js +2 -2
- package/dist/identities/PrivateKey.d.ts.map +1 -1
- package/dist/identities/PrivateKey.js +2 -2
- package/dist/identities/PublicKey.js +2 -2
- package/dist/index.js +4 -4
- package/dist/utils/AbiBase.d.ts +1 -1
- package/dist/utils/AbiBase.js +1 -1
- package/package.json +1 -1
- package/src/identities/Address.ts +2 -2
- package/src/identities/PrivateKey.ts +2 -2
- package/src/identities/PublicKey.ts +2 -2
- package/src/utils/AbiBase.ts +2 -2
- package/tests/unit/utils/AbiDecoder.test.ts +8 -6
- package/tests/unit/utils/AbiEncoder.test.ts +18 -0
- package/tests/unit/utils/TransactionUtils.test.ts +28 -1
- package/tests/unit/utils/UnitConverter.test.ts +2 -2
@@ -1,7 +1,7 @@
|
|
1
|
-
import { ethers, sha256 } from "ethers";
|
1
|
+
import { ethers, sha256, toUtf8Bytes } from "ethers";
|
2
2
|
export class Address {
|
3
3
|
static fromPassphrase(passphrase) {
|
4
|
-
return this.fromPrivateKey(sha256(
|
4
|
+
return this.fromPrivateKey(sha256(toUtf8Bytes(passphrase)));
|
5
5
|
}
|
6
6
|
static fromPublicKey(publicKey) {
|
7
7
|
return ethers.computeAddress(`0x${publicKey}`);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"PrivateKey.d.ts","sourceRoot":"","sources":["../../src/identities/PrivateKey.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,
|
1
|
+
{"version":3,"file":"PrivateKey.d.ts","sourceRoot":"","sources":["../../src/identities/PrivateKey.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAkC,MAAM,QAAQ,CAAC;AAMhE,UAAU,oBAAoB;IAC7B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACV;AAED,qBAAa,UAAU;IACf,UAAU,EAAE,MAAM,CAAC;gBAEd,OAAO,EAAE,MAAM,CAAC,MAAM;IAIlC,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU;IAIrD,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAMvC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAMjC,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC;CAW1D"}
|
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
8
8
|
});
|
9
9
|
};
|
10
10
|
import * as WIF from "wif";
|
11
|
-
import { ethers, keccak256, sha256 } from "ethers";
|
11
|
+
import { ethers, keccak256, sha256, toUtf8Bytes } from "ethers";
|
12
12
|
import { signAsync } from "@noble/secp256k1";
|
13
13
|
import { Constants } from "@/enums/Constants";
|
14
14
|
import { Network } from "@/configuration/Network";
|
@@ -17,7 +17,7 @@ export class PrivateKey {
|
|
17
17
|
this.privateKey = keyPair.privateKey.substring(2);
|
18
18
|
}
|
19
19
|
static fromPassphrase(passphrase) {
|
20
|
-
return PrivateKey.fromHex(sha256(
|
20
|
+
return PrivateKey.fromHex(sha256(toUtf8Bytes(passphrase)));
|
21
21
|
}
|
22
22
|
static fromHex(hex) {
|
23
23
|
const wallet = new ethers.Wallet(hex);
|
@@ -1,11 +1,11 @@
|
|
1
|
-
import { sha256, SigningKey } from "ethers";
|
1
|
+
import { sha256, SigningKey, toUtf8Bytes } from "ethers";
|
2
2
|
import { Signature } from "@noble/secp256k1";
|
3
3
|
export class PublicKey {
|
4
4
|
constructor(publicKey) {
|
5
5
|
this.publicKey = publicKey;
|
6
6
|
}
|
7
7
|
static fromPassphrase(passphrase) {
|
8
|
-
const publicKey = SigningKey.computePublicKey(sha256(
|
8
|
+
const publicKey = SigningKey.computePublicKey(sha256(toUtf8Bytes(passphrase)), true).substring(2);
|
9
9
|
return new PublicKey(publicKey);
|
10
10
|
}
|
11
11
|
static fromHex(publicKey) {
|
package/dist/index.js
CHANGED
@@ -22691,7 +22691,7 @@ var wordlists = {
|
|
22691
22691
|
// src/identities/Address.ts
|
22692
22692
|
var Address = class {
|
22693
22693
|
static fromPassphrase(passphrase) {
|
22694
|
-
return this.fromPrivateKey(sha2562(
|
22694
|
+
return this.fromPrivateKey(sha2562(toUtf8Bytes(passphrase)));
|
22695
22695
|
}
|
22696
22696
|
static fromPublicKey(publicKey) {
|
22697
22697
|
return ethers_exports.computeAddress(`0x${publicKey}`);
|
@@ -23509,7 +23509,7 @@ var PrivateKey = class _PrivateKey {
|
|
23509
23509
|
this.privateKey = keyPair.privateKey.substring(2);
|
23510
23510
|
}
|
23511
23511
|
static fromPassphrase(passphrase) {
|
23512
|
-
return _PrivateKey.fromHex(sha2562(
|
23512
|
+
return _PrivateKey.fromHex(sha2562(toUtf8Bytes(passphrase)));
|
23513
23513
|
}
|
23514
23514
|
static fromHex(hex) {
|
23515
23515
|
const wallet = new ethers_exports.Wallet(hex);
|
@@ -23536,7 +23536,7 @@ var PublicKey = class _PublicKey {
|
|
23536
23536
|
this.publicKey = publicKey;
|
23537
23537
|
}
|
23538
23538
|
static fromPassphrase(passphrase) {
|
23539
|
-
const publicKey = SigningKey.computePublicKey(sha2562(
|
23539
|
+
const publicKey = SigningKey.computePublicKey(sha2562(toUtf8Bytes(passphrase)), true).substring(2);
|
23540
23540
|
return new _PublicKey(publicKey);
|
23541
23541
|
}
|
23542
23542
|
static fromHex(publicKey) {
|
@@ -27809,7 +27809,7 @@ var AbiBase = class {
|
|
27809
27809
|
const abi = this.contractAbi(type, customAbi);
|
27810
27810
|
this.interface = new Interface(JSON.parse(JSON.stringify(abi)).abi);
|
27811
27811
|
}
|
27812
|
-
contractAbi(type
|
27812
|
+
contractAbi(type, customAbi) {
|
27813
27813
|
switch (type) {
|
27814
27814
|
case "consensus" /* CONSENSUS */:
|
27815
27815
|
return Abi_Consensus_default;
|
package/dist/utils/AbiBase.d.ts
CHANGED
@@ -2,7 +2,7 @@ import { ContractAbiType } from "@/enums/ContractAbiType";
|
|
2
2
|
import { Interface } from "ethers";
|
3
3
|
export declare abstract class AbiBase {
|
4
4
|
protected interface: Interface;
|
5
|
-
constructor(type?: ContractAbiType, customAbi?:
|
5
|
+
constructor(type?: ContractAbiType, customAbi?: object);
|
6
6
|
private contractAbi;
|
7
7
|
}
|
8
8
|
//# sourceMappingURL=AbiBase.d.ts.map
|
package/dist/utils/AbiBase.js
CHANGED
@@ -8,7 +8,7 @@ export class AbiBase {
|
|
8
8
|
const abi = this.contractAbi(type, customAbi);
|
9
9
|
this.interface = new Interface(JSON.parse(JSON.stringify(abi)).abi);
|
10
10
|
}
|
11
|
-
contractAbi(type
|
11
|
+
contractAbi(type, customAbi) {
|
12
12
|
switch (type) {
|
13
13
|
case ContractAbiType.CONSENSUS:
|
14
14
|
return ConsensusContract;
|
package/package.json
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
import { ethers, sha256 } from "ethers";
|
1
|
+
import { ethers, sha256, toUtf8Bytes } from "ethers";
|
2
2
|
|
3
3
|
export class Address {
|
4
4
|
static fromPassphrase(passphrase: string): string {
|
5
|
-
return this.fromPrivateKey(sha256(
|
5
|
+
return this.fromPrivateKey(sha256(toUtf8Bytes(passphrase)));
|
6
6
|
}
|
7
7
|
|
8
8
|
static fromPublicKey(publicKey: string): string {
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import * as WIF from "wif";
|
2
2
|
|
3
|
-
import { ethers, keccak256, sha256 } from "ethers";
|
3
|
+
import { ethers, keccak256, sha256, toUtf8Bytes } from "ethers";
|
4
4
|
import { signAsync } from "@noble/secp256k1";
|
5
5
|
|
6
6
|
import { Constants } from "@/enums/Constants";
|
@@ -20,7 +20,7 @@ export class PrivateKey {
|
|
20
20
|
}
|
21
21
|
|
22
22
|
static fromPassphrase(passphrase: string): PrivateKey {
|
23
|
-
return PrivateKey.fromHex(sha256(
|
23
|
+
return PrivateKey.fromHex(sha256(toUtf8Bytes(passphrase)));
|
24
24
|
}
|
25
25
|
|
26
26
|
static fromHex(hex: string): PrivateKey {
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { sha256, SigningKey } from "ethers";
|
1
|
+
import { sha256, SigningKey, toUtf8Bytes } from "ethers";
|
2
2
|
import { Signature } from "@noble/secp256k1";
|
3
3
|
|
4
4
|
export class PublicKey {
|
@@ -9,7 +9,7 @@ export class PublicKey {
|
|
9
9
|
}
|
10
10
|
|
11
11
|
static fromPassphrase(passphrase: string): PublicKey {
|
12
|
-
const publicKey = SigningKey.computePublicKey(sha256(
|
12
|
+
const publicKey = SigningKey.computePublicKey(sha256(toUtf8Bytes(passphrase)), true).substring(2);
|
13
13
|
|
14
14
|
return new PublicKey(publicKey);
|
15
15
|
}
|
package/src/utils/AbiBase.ts
CHANGED
@@ -7,13 +7,13 @@ import UsernamesContract from "./Abi/json/Abi.Usernames.json";
|
|
7
7
|
export abstract class AbiBase {
|
8
8
|
protected interface: Interface;
|
9
9
|
|
10
|
-
constructor(type: ContractAbiType = ContractAbiType.CONSENSUS, customAbi?:
|
10
|
+
constructor(type: ContractAbiType = ContractAbiType.CONSENSUS, customAbi?: object) {
|
11
11
|
const abi = this.contractAbi(type, customAbi);
|
12
12
|
|
13
13
|
this.interface = new Interface(JSON.parse(JSON.stringify(abi)).abi);
|
14
14
|
}
|
15
15
|
|
16
|
-
private contractAbi(type: ContractAbiType
|
16
|
+
private contractAbi(type: ContractAbiType, customAbi?: object): object {
|
17
17
|
switch (type) {
|
18
18
|
case ContractAbiType.CONSENSUS:
|
19
19
|
return ConsensusContract;
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import { AbiDecoder } from "@/utils/AbiDecoder";
|
2
2
|
import { ContractAbiType } from "@/enums/ContractAbiType";
|
3
|
+
import MultipaymentAbi from "@/utils/Abi/json/Abi.Multipayment.json";
|
3
4
|
|
4
5
|
it("should decode a vote function call", () => {
|
5
6
|
const decoder = new AbiDecoder();
|
@@ -23,8 +24,13 @@ it("should decode an unvote function call", () => {
|
|
23
24
|
expect(decodedData.args).toEqual([]);
|
24
25
|
});
|
25
26
|
|
26
|
-
it("should
|
27
|
-
|
27
|
+
it("should throw an error if custom abi is not passed", () => {
|
28
|
+
expect(() => new AbiDecoder(ContractAbiType.CUSTOM)).toThrow("Invalid contract type");
|
29
|
+
});
|
30
|
+
|
31
|
+
// We'll re-use the multipayment contract for this test
|
32
|
+
it("should decode using a custom abi", () => {
|
33
|
+
const decoder = new AbiDecoder(ContractAbiType.CUSTOM, MultipaymentAbi);
|
28
34
|
|
29
35
|
const args = [
|
30
36
|
["0xb693449AdDa7EFc015D87944EAE8b7C37EB1690A", "0xb693449AdDa7EFc015D87944EAE8b7C37EB1690A"],
|
@@ -38,7 +44,3 @@ it("should decode a multipayment function call", () => {
|
|
38
44
|
expect(decodedData.args[0]).toEqual(args[0]);
|
39
45
|
expect(decodedData.args[1].map((value: BigInt) => value.toString())).toEqual(args[1]);
|
40
46
|
});
|
41
|
-
|
42
|
-
it("should throw an error if custom abi is not passed", () => {
|
43
|
-
expect(() => new AbiDecoder(ContractAbiType.CUSTOM)).toThrow("Invalid contract type");
|
44
|
-
});
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import { AbiEncoder } from "@/utils/AbiEncoder";
|
2
2
|
import { ContractAbiType } from "@/enums/ContractAbiType";
|
3
|
+
import MultipaymentAbi from "@/utils/Abi/json/Abi.Multipayment.json";
|
3
4
|
|
4
5
|
it("should encode a vote function call", () => {
|
5
6
|
const encoder = new AbiEncoder();
|
@@ -28,3 +29,20 @@ it("should encode a multipayment function call", () => {
|
|
28
29
|
|
29
30
|
expect(encodedData).toBe(expectedEncodedData);
|
30
31
|
});
|
32
|
+
|
33
|
+
// We'll re-use the multipayment contract for this test
|
34
|
+
it("should encode using a custom abi", () => {
|
35
|
+
const encoder = new AbiEncoder(ContractAbiType.CUSTOM, MultipaymentAbi);
|
36
|
+
|
37
|
+
const functionName = "pay";
|
38
|
+
const args = [
|
39
|
+
["0xb693449AdDa7EFc015D87944EAE8b7C37EB1690A", "0xb693449AdDa7EFc015D87944EAE8b7C37EB1690A"],
|
40
|
+
["100000000", "200000000"],
|
41
|
+
];
|
42
|
+
const expectedEncodedData =
|
43
|
+
"084ce708000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000b693449adda7efc015d87944eae8b7c37eb1690a000000000000000000000000b693449adda7efc015d87944eae8b7c37eb1690a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000005f5e100000000000000000000000000000000000000000000000000000000000bebc200";
|
44
|
+
|
45
|
+
const encodedData = encoder.encodeFunctionCall(functionName, args);
|
46
|
+
|
47
|
+
expect(encodedData).toBe(expectedEncodedData);
|
48
|
+
});
|
@@ -1,5 +1,4 @@
|
|
1
1
|
import fixture from "@tests/fixtures/transactions/transfer.json";
|
2
|
-
|
3
2
|
import { TransactionUtils } from "@/utils/TransactionUtils";
|
4
3
|
|
5
4
|
it("should convert a transaction to a buffer with a signature", () => {
|
@@ -15,6 +14,34 @@ it("should convert a transaction to a buffer without a signature", () => {
|
|
15
14
|
expect(buffer).toBeInstanceOf(Buffer);
|
16
15
|
});
|
17
16
|
|
17
|
+
it("should convert a transaction to a buffer using fallback values if missing", () => {
|
18
|
+
const result = TransactionUtils.toBuffer({
|
19
|
+
network: undefined,
|
20
|
+
nonce: undefined,
|
21
|
+
gasPrice: undefined,
|
22
|
+
gasLimit: undefined,
|
23
|
+
recipientAddress: undefined,
|
24
|
+
value: undefined,
|
25
|
+
data: undefined,
|
26
|
+
}).toString("hex");
|
27
|
+
|
28
|
+
expect(result).toEqual("02c98080808080808080c0");
|
29
|
+
});
|
30
|
+
|
31
|
+
it("should convert a transaction to a buffer handling data starting with 0x", () => {
|
32
|
+
const result = TransactionUtils.toBuffer({
|
33
|
+
network: undefined,
|
34
|
+
nonce: undefined,
|
35
|
+
gasPrice: undefined,
|
36
|
+
gasLimit: undefined,
|
37
|
+
recipientAddress: undefined,
|
38
|
+
value: undefined,
|
39
|
+
data: "0x1234567890",
|
40
|
+
}).toString("hex");
|
41
|
+
|
42
|
+
expect(result).toEqual("02ce80808080808080851234567890c0");
|
43
|
+
});
|
44
|
+
|
18
45
|
it("should convert a transaction to a hash with a signature", () => {
|
19
46
|
expect(TransactionUtils.toHash(fixture.data)).toBe(fixture.data.hash);
|
20
47
|
});
|
@@ -35,7 +35,7 @@ it.each([
|
|
35
35
|
[1000, "1000000000000000000000"],
|
36
36
|
[10000, "10000000000000000000000"],
|
37
37
|
])("should parse units into ark [%s]", (amount, formattedAmount) => {
|
38
|
-
const arkValue = UnitConverter.parseUnits(amount
|
38
|
+
const arkValue = UnitConverter.parseUnits(amount);
|
39
39
|
|
40
40
|
expect(arkValue.toFixed()).toEqual(formattedAmount);
|
41
41
|
});
|
@@ -71,7 +71,7 @@ it.each([
|
|
71
71
|
["1000000000000000000000", 1000],
|
72
72
|
["10000000000000000000000", 10000],
|
73
73
|
])("should format units from ark [%s]", (formattedAmount, amount) => {
|
74
|
-
const formattedValue = UnitConverter.formatUnits(formattedAmount
|
74
|
+
const formattedValue = UnitConverter.formatUnits(formattedAmount);
|
75
75
|
|
76
76
|
expect(formattedValue).toEqual(amount);
|
77
77
|
});
|