@ledgerhq/coin-casper 1.7.3 → 2.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/.unimportedrc.json +11 -35
- package/CHANGELOG.md +14 -0
- package/jest.config.js +2 -5
- package/lib/api/index.d.ts +8 -6
- package/lib/api/index.d.ts.map +1 -1
- package/lib/api/index.integ.test.js +12 -3
- package/lib/api/index.integ.test.js.map +1 -1
- package/lib/api/index.js +53 -91
- package/lib/api/index.js.map +1 -1
- package/lib/api/index.test.d.ts +2 -0
- package/lib/api/index.test.d.ts.map +1 -0
- package/lib/api/index.test.js +274 -0
- package/lib/api/index.test.js.map +1 -0
- package/lib/api/types.d.ts +2 -67
- package/lib/api/types.d.ts.map +1 -1
- package/lib/bridge/bridgeHelpers/accountShape.d.ts.map +1 -1
- package/lib/bridge/bridgeHelpers/accountShape.js +5 -7
- package/lib/bridge/bridgeHelpers/accountShape.js.map +1 -1
- package/lib/bridge/bridgeHelpers/accountShape.test.d.ts +2 -0
- package/lib/bridge/bridgeHelpers/accountShape.test.d.ts.map +1 -0
- package/lib/bridge/bridgeHelpers/accountShape.test.js +94 -0
- package/lib/bridge/bridgeHelpers/accountShape.test.js.map +1 -0
- package/lib/bridge/bridgeHelpers/addresses.d.ts +3 -9
- package/lib/bridge/bridgeHelpers/addresses.d.ts.map +1 -1
- package/lib/bridge/bridgeHelpers/addresses.js +5 -62
- package/lib/bridge/bridgeHelpers/addresses.js.map +1 -1
- package/lib/bridge/bridgeHelpers/txn.d.ts +2 -2
- package/lib/bridge/bridgeHelpers/txn.d.ts.map +1 -1
- package/lib/bridge/bridgeHelpers/txn.js +9 -11
- package/lib/bridge/bridgeHelpers/txn.js.map +1 -1
- package/lib/bridge/bridgeHelpers/txn.test.d.ts +2 -0
- package/lib/bridge/bridgeHelpers/txn.test.d.ts.map +1 -0
- package/lib/bridge/bridgeHelpers/txn.test.js +306 -0
- package/lib/bridge/bridgeHelpers/txn.test.js.map +1 -0
- package/lib/bridge/broadcast.d.ts.map +1 -1
- package/lib/bridge/broadcast.js +7 -6
- package/lib/bridge/broadcast.js.map +1 -1
- package/lib/bridge/broadcast.test.d.ts +2 -0
- package/lib/bridge/broadcast.test.d.ts.map +1 -0
- package/lib/bridge/broadcast.test.js +72 -0
- package/lib/bridge/broadcast.test.js.map +1 -0
- package/lib/bridge/buildOptimisticOperation.d.ts.map +1 -1
- package/lib/bridge/buildOptimisticOperation.js +2 -0
- package/lib/bridge/buildOptimisticOperation.js.map +1 -1
- package/lib/bridge/buildOptimisticOperation.test.d.ts +2 -0
- package/lib/bridge/buildOptimisticOperation.test.d.ts.map +1 -0
- package/lib/bridge/buildOptimisticOperation.test.js +76 -0
- package/lib/bridge/buildOptimisticOperation.test.js.map +1 -0
- package/lib/bridge/createTransaction.test.d.ts +2 -0
- package/lib/bridge/createTransaction.test.d.ts.map +1 -0
- package/lib/bridge/createTransaction.test.js +41 -0
- package/lib/bridge/createTransaction.test.js.map +1 -0
- package/lib/bridge/deviceTransactionConfig.d.ts.map +1 -1
- package/lib/bridge/deviceTransactionConfig.js +13 -0
- package/lib/bridge/deviceTransactionConfig.js.map +1 -1
- package/lib/bridge/deviceTransactionConfig.test.d.ts +2 -0
- package/lib/bridge/deviceTransactionConfig.test.d.ts.map +1 -0
- package/lib/bridge/deviceTransactionConfig.test.js +148 -0
- package/lib/bridge/deviceTransactionConfig.test.js.map +1 -0
- package/lib/bridge/estimateMaxSpendable.test.d.ts +2 -0
- package/lib/bridge/estimateMaxSpendable.test.d.ts.map +1 -0
- package/lib/bridge/estimateMaxSpendable.test.js +133 -0
- package/lib/bridge/estimateMaxSpendable.test.js.map +1 -0
- package/lib/bridge/getTransactionStatus.test.d.ts +2 -0
- package/lib/bridge/getTransactionStatus.test.d.ts.map +1 -0
- package/lib/bridge/getTransactionStatus.test.js +214 -0
- package/lib/bridge/getTransactionStatus.test.js.map +1 -0
- package/lib/bridge/index.d.ts +2 -1
- package/lib/bridge/index.d.ts.map +1 -1
- package/lib/bridge/index.js +3 -1
- package/lib/bridge/index.js.map +1 -1
- package/lib/bridge/prepareTransaction.js +1 -1
- package/lib/bridge/prepareTransaction.js.map +1 -1
- package/lib/bridge/prepareTransaction.test.d.ts +2 -0
- package/lib/bridge/prepareTransaction.test.d.ts.map +1 -0
- package/lib/bridge/prepareTransaction.test.js +159 -0
- package/lib/bridge/prepareTransaction.test.js.map +1 -0
- package/lib/bridge/signOperation.d.ts.map +1 -1
- package/lib/bridge/signOperation.js +14 -12
- package/lib/bridge/signOperation.js.map +1 -1
- package/lib/bridge/transaction.test.d.ts +2 -0
- package/lib/bridge/transaction.test.d.ts.map +1 -0
- package/lib/bridge/transaction.test.js +179 -0
- package/lib/bridge/transaction.test.js.map +1 -0
- package/lib/common-logic/utils.additional.test.d.ts +2 -0
- package/lib/common-logic/utils.additional.test.d.ts.map +1 -0
- package/lib/common-logic/utils.additional.test.js +58 -0
- package/lib/common-logic/utils.additional.test.js.map +1 -0
- package/lib/common-logic/utils.unit.test.js +6 -4
- package/lib/common-logic/utils.unit.test.js.map +1 -1
- package/lib/config.d.ts +10 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +16 -0
- package/lib/config.js.map +1 -0
- package/lib/consts.d.ts +3 -0
- package/lib/consts.d.ts.map +1 -1
- package/lib/consts.js +5 -1
- package/lib/consts.js.map +1 -1
- package/lib/hw-signMessage.test.d.ts +2 -0
- package/lib/hw-signMessage.test.d.ts.map +1 -0
- package/lib/hw-signMessage.test.js +80 -0
- package/lib/hw-signMessage.test.js.map +1 -0
- package/lib/signer/getAddress.js +1 -1
- package/lib/signer/getAddress.js.map +1 -1
- package/lib/signer/getAddress.test.d.ts +2 -0
- package/lib/signer/getAddress.test.d.ts.map +1 -0
- package/lib/signer/getAddress.test.js +133 -0
- package/lib/signer/getAddress.test.js.map +1 -0
- package/lib/test/bot-specs.d.ts.map +1 -1
- package/lib/test/bot-specs.js +1 -0
- package/lib/test/bot-specs.js.map +1 -1
- package/lib/test/fixtures.d.ts +131 -0
- package/lib/test/fixtures.d.ts.map +1 -0
- package/lib/test/fixtures.js +395 -0
- package/lib/test/fixtures.js.map +1 -0
- package/lib/test/index.d.ts +2 -0
- package/lib/test/index.d.ts.map +1 -1
- package/lib/test/index.js +2 -0
- package/lib/test/index.js.map +1 -1
- package/lib/test/speculos-deviceActions.d.ts.map +1 -1
- package/lib/test/speculos-deviceActions.js +4 -3
- package/lib/test/speculos-deviceActions.js.map +1 -1
- package/lib-es/api/index.d.ts +8 -6
- package/lib-es/api/index.d.ts.map +1 -1
- package/lib-es/api/index.integ.test.js +13 -4
- package/lib-es/api/index.integ.test.js.map +1 -1
- package/lib-es/api/index.js +51 -90
- package/lib-es/api/index.js.map +1 -1
- package/lib-es/api/index.test.d.ts +2 -0
- package/lib-es/api/index.test.d.ts.map +1 -0
- package/lib-es/api/index.test.js +269 -0
- package/lib-es/api/index.test.js.map +1 -0
- package/lib-es/api/types.d.ts +2 -67
- package/lib-es/api/types.d.ts.map +1 -1
- package/lib-es/bridge/bridgeHelpers/accountShape.d.ts.map +1 -1
- package/lib-es/bridge/bridgeHelpers/accountShape.js +6 -8
- package/lib-es/bridge/bridgeHelpers/accountShape.js.map +1 -1
- package/lib-es/bridge/bridgeHelpers/accountShape.test.d.ts +2 -0
- package/lib-es/bridge/bridgeHelpers/accountShape.test.d.ts.map +1 -0
- package/lib-es/bridge/bridgeHelpers/accountShape.test.js +89 -0
- package/lib-es/bridge/bridgeHelpers/accountShape.test.js.map +1 -0
- package/lib-es/bridge/bridgeHelpers/addresses.d.ts +3 -9
- package/lib-es/bridge/bridgeHelpers/addresses.d.ts.map +1 -1
- package/lib-es/bridge/bridgeHelpers/addresses.js +5 -60
- package/lib-es/bridge/bridgeHelpers/addresses.js.map +1 -1
- package/lib-es/bridge/bridgeHelpers/txn.d.ts +2 -2
- package/lib-es/bridge/bridgeHelpers/txn.d.ts.map +1 -1
- package/lib-es/bridge/bridgeHelpers/txn.js +10 -12
- package/lib-es/bridge/bridgeHelpers/txn.js.map +1 -1
- package/lib-es/bridge/bridgeHelpers/txn.test.d.ts +2 -0
- package/lib-es/bridge/bridgeHelpers/txn.test.d.ts.map +1 -0
- package/lib-es/bridge/bridgeHelpers/txn.test.js +278 -0
- package/lib-es/bridge/bridgeHelpers/txn.test.js.map +1 -0
- package/lib-es/bridge/broadcast.d.ts.map +1 -1
- package/lib-es/bridge/broadcast.js +8 -7
- package/lib-es/bridge/broadcast.js.map +1 -1
- package/lib-es/bridge/broadcast.test.d.ts +2 -0
- package/lib-es/bridge/broadcast.test.d.ts.map +1 -0
- package/lib-es/bridge/broadcast.test.js +70 -0
- package/lib-es/bridge/broadcast.test.js.map +1 -0
- package/lib-es/bridge/buildOptimisticOperation.d.ts.map +1 -1
- package/lib-es/bridge/buildOptimisticOperation.js +2 -0
- package/lib-es/bridge/buildOptimisticOperation.js.map +1 -1
- package/lib-es/bridge/buildOptimisticOperation.test.d.ts +2 -0
- package/lib-es/bridge/buildOptimisticOperation.test.d.ts.map +1 -0
- package/lib-es/bridge/buildOptimisticOperation.test.js +74 -0
- package/lib-es/bridge/buildOptimisticOperation.test.js.map +1 -0
- package/lib-es/bridge/createTransaction.test.d.ts +2 -0
- package/lib-es/bridge/createTransaction.test.d.ts.map +1 -0
- package/lib-es/bridge/createTransaction.test.js +36 -0
- package/lib-es/bridge/createTransaction.test.js.map +1 -0
- package/lib-es/bridge/deviceTransactionConfig.d.ts.map +1 -1
- package/lib-es/bridge/deviceTransactionConfig.js +13 -0
- package/lib-es/bridge/deviceTransactionConfig.js.map +1 -1
- package/lib-es/bridge/deviceTransactionConfig.test.d.ts +2 -0
- package/lib-es/bridge/deviceTransactionConfig.test.d.ts.map +1 -0
- package/lib-es/bridge/deviceTransactionConfig.test.js +143 -0
- package/lib-es/bridge/deviceTransactionConfig.test.js.map +1 -0
- package/lib-es/bridge/estimateMaxSpendable.test.d.ts +2 -0
- package/lib-es/bridge/estimateMaxSpendable.test.d.ts.map +1 -0
- package/lib-es/bridge/estimateMaxSpendable.test.js +131 -0
- package/lib-es/bridge/estimateMaxSpendable.test.js.map +1 -0
- package/lib-es/bridge/getTransactionStatus.test.d.ts +2 -0
- package/lib-es/bridge/getTransactionStatus.test.d.ts.map +1 -0
- package/lib-es/bridge/getTransactionStatus.test.js +212 -0
- package/lib-es/bridge/getTransactionStatus.test.js.map +1 -0
- package/lib-es/bridge/index.d.ts +2 -1
- package/lib-es/bridge/index.d.ts.map +1 -1
- package/lib-es/bridge/index.js +3 -1
- package/lib-es/bridge/index.js.map +1 -1
- package/lib-es/bridge/prepareTransaction.js +1 -1
- package/lib-es/bridge/prepareTransaction.js.map +1 -1
- package/lib-es/bridge/prepareTransaction.test.d.ts +2 -0
- package/lib-es/bridge/prepareTransaction.test.d.ts.map +1 -0
- package/lib-es/bridge/prepareTransaction.test.js +157 -0
- package/lib-es/bridge/prepareTransaction.test.js.map +1 -0
- package/lib-es/bridge/signOperation.d.ts.map +1 -1
- package/lib-es/bridge/signOperation.js +16 -14
- package/lib-es/bridge/signOperation.js.map +1 -1
- package/lib-es/bridge/transaction.test.d.ts +2 -0
- package/lib-es/bridge/transaction.test.d.ts.map +1 -0
- package/lib-es/bridge/transaction.test.js +154 -0
- package/lib-es/bridge/transaction.test.js.map +1 -0
- package/lib-es/common-logic/utils.additional.test.d.ts +2 -0
- package/lib-es/common-logic/utils.additional.test.d.ts.map +1 -0
- package/lib-es/common-logic/utils.additional.test.js +56 -0
- package/lib-es/common-logic/utils.additional.test.js.map +1 -0
- package/lib-es/common-logic/utils.unit.test.js +7 -5
- package/lib-es/common-logic/utils.unit.test.js.map +1 -1
- package/lib-es/config.d.ts +10 -0
- package/lib-es/config.d.ts.map +1 -0
- package/lib-es/config.js +11 -0
- package/lib-es/config.js.map +1 -0
- package/lib-es/consts.d.ts +3 -0
- package/lib-es/consts.d.ts.map +1 -1
- package/lib-es/consts.js +4 -0
- package/lib-es/consts.js.map +1 -1
- package/lib-es/hw-signMessage.test.d.ts +2 -0
- package/lib-es/hw-signMessage.test.d.ts.map +1 -0
- package/lib-es/hw-signMessage.test.js +78 -0
- package/lib-es/hw-signMessage.test.js.map +1 -0
- package/lib-es/signer/getAddress.js +2 -2
- package/lib-es/signer/getAddress.js.map +1 -1
- package/lib-es/signer/getAddress.test.d.ts +2 -0
- package/lib-es/signer/getAddress.test.d.ts.map +1 -0
- package/lib-es/signer/getAddress.test.js +105 -0
- package/lib-es/signer/getAddress.test.js.map +1 -0
- package/lib-es/test/bot-specs.d.ts.map +1 -1
- package/lib-es/test/bot-specs.js +1 -0
- package/lib-es/test/bot-specs.js.map +1 -1
- package/lib-es/test/fixtures.d.ts +131 -0
- package/lib-es/test/fixtures.d.ts.map +1 -0
- package/lib-es/test/fixtures.js +381 -0
- package/lib-es/test/fixtures.js.map +1 -0
- package/lib-es/test/index.d.ts +2 -0
- package/lib-es/test/index.d.ts.map +1 -1
- package/lib-es/test/index.js +2 -0
- package/lib-es/test/index.js.map +1 -1
- package/lib-es/test/speculos-deviceActions.d.ts.map +1 -1
- package/lib-es/test/speculos-deviceActions.js +5 -4
- package/lib-es/test/speculos-deviceActions.js.map +1 -1
- package/package.json +17 -12
- package/src/api/index.integ.test.ts +14 -4
- package/src/api/index.test.ts +386 -0
- package/src/api/index.ts +59 -117
- package/src/api/types.ts +2 -75
- package/src/bridge/bridgeHelpers/accountShape.test.ts +122 -0
- package/src/bridge/bridgeHelpers/accountShape.ts +7 -9
- package/src/bridge/bridgeHelpers/addresses.ts +6 -73
- package/src/bridge/bridgeHelpers/txn.test.ts +339 -0
- package/src/bridge/bridgeHelpers/txn.ts +18 -18
- package/src/bridge/broadcast.test.ts +89 -0
- package/src/bridge/broadcast.ts +9 -7
- package/src/bridge/buildOptimisticOperation.test.ts +89 -0
- package/src/bridge/buildOptimisticOperation.ts +2 -0
- package/src/bridge/createTransaction.test.ts +43 -0
- package/src/bridge/deviceTransactionConfig.test.ts +171 -0
- package/src/bridge/deviceTransactionConfig.ts +14 -0
- package/src/bridge/estimateMaxSpendable.test.ts +155 -0
- package/src/bridge/getTransactionStatus.test.ts +264 -0
- package/src/bridge/index.ts +8 -1
- package/src/bridge/prepareTransaction.test.ts +174 -0
- package/src/bridge/prepareTransaction.ts +1 -1
- package/src/bridge/signOperation.ts +16 -25
- package/src/bridge/transaction.test.ts +182 -0
- package/src/common-logic/utils.additional.test.ts +75 -0
- package/src/common-logic/utils.unit.test.ts +9 -7
- package/src/config.ts +22 -0
- package/src/consts.ts +5 -0
- package/src/hw-signMessage.test.ts +123 -0
- package/src/signer/getAddress.test.ts +134 -0
- package/src/signer/getAddress.ts +2 -2
- package/src/test/bot-specs.ts +1 -0
- package/src/test/fixtures.ts +448 -0
- package/src/test/index.ts +2 -0
- package/src/test/speculos-deviceActions.ts +6 -4
- package/tsconfig.json +1 -4
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { SignerContext } from "@ledgerhq/coin-framework/signer";
|
|
2
|
+
import { Account } from "@ledgerhq/types-live";
|
|
3
|
+
import { signMessage } from "./hw-signMessage";
|
|
4
|
+
import { CasperSigner, CasperSignature } from "./types";
|
|
5
|
+
|
|
6
|
+
describe("hw-signMessage", () => {
|
|
7
|
+
// Test fixtures
|
|
8
|
+
const mockDeviceId = "device_123";
|
|
9
|
+
|
|
10
|
+
// Only define the properties actually used by the signMessage function
|
|
11
|
+
const mockAccount = {
|
|
12
|
+
freshAddressPath: "44'/506'/0'/0/1",
|
|
13
|
+
} as unknown as Account;
|
|
14
|
+
|
|
15
|
+
// Mock signature response
|
|
16
|
+
const mockSignatureResponse: CasperSignature = {
|
|
17
|
+
errorMessage: "",
|
|
18
|
+
returnCode: 0x9000,
|
|
19
|
+
signatureRS: Buffer.from(
|
|
20
|
+
"00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
|
|
21
|
+
"hex",
|
|
22
|
+
),
|
|
23
|
+
signatureRSV: Buffer.from(
|
|
24
|
+
"00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff01",
|
|
25
|
+
"hex",
|
|
26
|
+
),
|
|
27
|
+
signature_compact: new Uint8Array(
|
|
28
|
+
Buffer.from("00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff01", "hex"),
|
|
29
|
+
),
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// Mock a successful signer
|
|
33
|
+
const mockSigner: CasperSigner = {
|
|
34
|
+
showAddressAndPubKey: jest.fn(),
|
|
35
|
+
getAddressAndPubKey: jest.fn(),
|
|
36
|
+
sign: jest.fn().mockResolvedValue(mockSignatureResponse),
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Mock signerContext function
|
|
40
|
+
const mockSignerContext: SignerContext<CasperSigner> = jest.fn((deviceId, callback) =>
|
|
41
|
+
callback(mockSigner),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
jest.clearAllMocks();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("should sign a plain text message correctly", async () => {
|
|
49
|
+
const message = "Hello Casper Network";
|
|
50
|
+
const result = await signMessage(mockSignerContext)(mockDeviceId, mockAccount, { message });
|
|
51
|
+
|
|
52
|
+
// Verify the signer was called with correct parameters
|
|
53
|
+
expect(mockSignerContext).toHaveBeenCalledWith(mockDeviceId, expect.any(Function));
|
|
54
|
+
expect(mockSigner.sign).toHaveBeenCalledWith(mockAccount.freshAddressPath, expect.any(Buffer));
|
|
55
|
+
|
|
56
|
+
// Verify the correct signature format is returned
|
|
57
|
+
expect(result).toEqual({
|
|
58
|
+
rsv: {
|
|
59
|
+
r: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
|
|
60
|
+
s: "01",
|
|
61
|
+
v: NaN,
|
|
62
|
+
},
|
|
63
|
+
signature: "0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff01",
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("should sign a hex message correctly", async () => {
|
|
68
|
+
const message = "0x123456789abcdef";
|
|
69
|
+
const result = await signMessage(mockSignerContext)(mockDeviceId, mockAccount, { message });
|
|
70
|
+
|
|
71
|
+
expect(mockSigner.sign).toHaveBeenCalledWith(mockAccount.freshAddressPath, expect.any(Buffer));
|
|
72
|
+
expect(result.signature).toBe(
|
|
73
|
+
"0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff01",
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("should sign a base64 message correctly", async () => {
|
|
78
|
+
const message = "SGVsbG8gQ2FzcGVyIE5ldHdvcms="; // "Hello Casper Network" in base64
|
|
79
|
+
const result = await signMessage(mockSignerContext)(mockDeviceId, mockAccount, { message });
|
|
80
|
+
|
|
81
|
+
expect(mockSigner.sign).toHaveBeenCalledWith(mockAccount.freshAddressPath, expect.any(Buffer));
|
|
82
|
+
expect(result.signature).toBe(
|
|
83
|
+
"0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff01",
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("should throw error if message is empty", async () => {
|
|
88
|
+
await expect(
|
|
89
|
+
signMessage(mockSignerContext)(mockDeviceId, mockAccount, { message: "" }),
|
|
90
|
+
).rejects.toThrow("Message cannot be empty");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("should throw error if message is undefined", async () => {
|
|
94
|
+
await expect(
|
|
95
|
+
signMessage(mockSignerContext)(mockDeviceId, mockAccount, { message: undefined as any }),
|
|
96
|
+
).rejects.toThrow("Message cannot be empty");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("should throw error if message is not a string", async () => {
|
|
100
|
+
await expect(
|
|
101
|
+
signMessage(mockSignerContext)(mockDeviceId, mockAccount, {
|
|
102
|
+
message: { type: "EIP712" } as any,
|
|
103
|
+
}),
|
|
104
|
+
).rejects.toThrow("Signing EIP712Message not supported");
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("should propagate errors from the signer", async () => {
|
|
108
|
+
// Mock an error from the signer
|
|
109
|
+
const errorSigner: CasperSigner = {
|
|
110
|
+
showAddressAndPubKey: jest.fn(),
|
|
111
|
+
getAddressAndPubKey: jest.fn(),
|
|
112
|
+
sign: jest.fn().mockRejectedValue(new Error("Device disconnected")),
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const errorSignerContext: SignerContext<CasperSigner> = jest.fn((deviceId, callback) =>
|
|
116
|
+
callback(errorSigner),
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
await expect(
|
|
120
|
+
signMessage(errorSignerContext)(mockDeviceId, mockAccount, { message: "test" }),
|
|
121
|
+
).rejects.toThrow("Device disconnected");
|
|
122
|
+
});
|
|
123
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { SignerContext } from "@ledgerhq/coin-framework/signer";
|
|
2
|
+
import { KeyAlgorithm } from "casper-js-sdk";
|
|
3
|
+
import getAddressResolver from "./getAddress";
|
|
4
|
+
import { CasperSigner, CasperGetAddrResponse } from "../types";
|
|
5
|
+
import * as addressHelpers from "../bridge/bridgeHelpers/addresses";
|
|
6
|
+
import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets";
|
|
7
|
+
|
|
8
|
+
describe("getAddress resolver", () => {
|
|
9
|
+
// Test fixtures
|
|
10
|
+
const mockDeviceId = "device_123";
|
|
11
|
+
const mockPath = "44'/506'/0'/0/0";
|
|
12
|
+
const mockCurrency = getCryptoCurrencyById("casper");
|
|
13
|
+
const mockDerivationMode = "casper_wallet";
|
|
14
|
+
const mockPubKey = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
|
15
|
+
const mockDerivedAddress = "02cafe0123456789abcdef";
|
|
16
|
+
|
|
17
|
+
let mockSigner: CasperSigner;
|
|
18
|
+
let mockSignerContext: SignerContext<CasperSigner>;
|
|
19
|
+
|
|
20
|
+
// Mock responses
|
|
21
|
+
const createAddressResponse = (includeAddress: boolean): CasperGetAddrResponse => ({
|
|
22
|
+
errorMessage: "",
|
|
23
|
+
returnCode: 0x9000,
|
|
24
|
+
publicKey: Buffer.from(mockPubKey, "hex"),
|
|
25
|
+
Address: {
|
|
26
|
+
toString: jest.fn().mockReturnValue(includeAddress ? "02CAFE0123456789ABCDEF" : ""),
|
|
27
|
+
length: includeAddress ? 1 : 0,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
jest.clearAllMocks();
|
|
33
|
+
|
|
34
|
+
// Set up mocks
|
|
35
|
+
mockSigner = {
|
|
36
|
+
showAddressAndPubKey: jest.fn(),
|
|
37
|
+
getAddressAndPubKey: jest.fn(),
|
|
38
|
+
sign: jest.fn(),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
mockSignerContext = jest.fn((deviceId, callback) => callback(mockSigner));
|
|
42
|
+
|
|
43
|
+
// Mock address derivation function
|
|
44
|
+
jest.spyOn(addressHelpers, "casperAddressFromPubKey").mockReturnValue(mockDerivedAddress);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const setupTest = (verify: boolean, includeAddress: boolean) => {
|
|
48
|
+
const response = createAddressResponse(includeAddress);
|
|
49
|
+
|
|
50
|
+
if (verify) {
|
|
51
|
+
mockSigner.showAddressAndPubKey = jest.fn().mockResolvedValue(response);
|
|
52
|
+
} else {
|
|
53
|
+
mockSigner.getAddressAndPubKey = jest.fn().mockResolvedValue(response);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return getAddressResolver(mockSignerContext);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
test("should return address from device when verify=false", async () => {
|
|
60
|
+
const getAddressFn = setupTest(false, true);
|
|
61
|
+
|
|
62
|
+
const result = await getAddressFn(mockDeviceId, {
|
|
63
|
+
path: mockPath,
|
|
64
|
+
verify: false,
|
|
65
|
+
currency: mockCurrency,
|
|
66
|
+
derivationMode: mockDerivationMode,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
expect(mockSigner.getAddressAndPubKey).toHaveBeenCalledWith(mockPath);
|
|
70
|
+
expect(mockSigner.showAddressAndPubKey).not.toHaveBeenCalled();
|
|
71
|
+
|
|
72
|
+
expect(result).toEqual({
|
|
73
|
+
path: mockPath,
|
|
74
|
+
address: mockDerivedAddress,
|
|
75
|
+
publicKey: mockPubKey,
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("should return address from device when verify=true", async () => {
|
|
80
|
+
const getAddressFn = setupTest(true, true);
|
|
81
|
+
|
|
82
|
+
const result = await getAddressFn(mockDeviceId, {
|
|
83
|
+
path: mockPath,
|
|
84
|
+
verify: true,
|
|
85
|
+
currency: mockCurrency,
|
|
86
|
+
derivationMode: mockDerivationMode,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
expect(mockSigner.showAddressAndPubKey).toHaveBeenCalledWith(mockPath);
|
|
90
|
+
expect(mockSigner.getAddressAndPubKey).not.toHaveBeenCalled();
|
|
91
|
+
|
|
92
|
+
expect(result).toEqual({
|
|
93
|
+
path: mockPath,
|
|
94
|
+
address: mockDerivedAddress,
|
|
95
|
+
publicKey: mockPubKey,
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("should derive address when not provided by device", async () => {
|
|
100
|
+
const getAddressFn = setupTest(false, false);
|
|
101
|
+
|
|
102
|
+
const result = await getAddressFn(mockDeviceId, {
|
|
103
|
+
path: mockPath,
|
|
104
|
+
verify: false,
|
|
105
|
+
currency: mockCurrency,
|
|
106
|
+
derivationMode: mockDerivationMode,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
expect(addressHelpers.casperAddressFromPubKey).toHaveBeenCalledWith(
|
|
110
|
+
expect.any(Buffer),
|
|
111
|
+
KeyAlgorithm.SECP256K1,
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
expect(result).toEqual({
|
|
115
|
+
path: mockPath,
|
|
116
|
+
address: mockDerivedAddress,
|
|
117
|
+
publicKey: mockPubKey,
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("should propagate errors from the signer", async () => {
|
|
122
|
+
mockSigner.getAddressAndPubKey = jest.fn().mockRejectedValue(new Error("Device disconnected"));
|
|
123
|
+
const getAddressFn = getAddressResolver(mockSignerContext);
|
|
124
|
+
|
|
125
|
+
await expect(
|
|
126
|
+
getAddressFn(mockDeviceId, {
|
|
127
|
+
path: mockPath,
|
|
128
|
+
verify: false,
|
|
129
|
+
currency: mockCurrency,
|
|
130
|
+
derivationMode: mockDerivationMode,
|
|
131
|
+
}),
|
|
132
|
+
).rejects.toThrow("Device disconnected");
|
|
133
|
+
});
|
|
134
|
+
});
|
package/src/signer/getAddress.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { log } from "@ledgerhq/logs";
|
|
|
2
2
|
import { GetAddressFn } from "@ledgerhq/coin-framework/bridge/getAddressWrapper";
|
|
3
3
|
import { GetAddressOptions } from "@ledgerhq/coin-framework/derivation";
|
|
4
4
|
import { SignerContext } from "@ledgerhq/coin-framework/signer";
|
|
5
|
-
import {
|
|
5
|
+
import { KeyAlgorithm } from "casper-js-sdk";
|
|
6
6
|
import { casperAddressFromPubKey } from "../bridge/bridgeHelpers/addresses";
|
|
7
7
|
import { CasperSigner } from "../types";
|
|
8
8
|
|
|
@@ -22,7 +22,7 @@ function resolver(signerContext: SignerContext<CasperSigner>): GetAddressFn {
|
|
|
22
22
|
path,
|
|
23
23
|
address: r.Address.length
|
|
24
24
|
? r.Address.toString().toLowerCase()
|
|
25
|
-
: casperAddressFromPubKey(r.publicKey,
|
|
25
|
+
: casperAddressFromPubKey(r.publicKey, KeyAlgorithm.SECP256K1),
|
|
26
26
|
publicKey: r.publicKey.toString("hex"),
|
|
27
27
|
};
|
|
28
28
|
};
|