@hashgraph/hedera-wallet-connect 1.3.2-0 → 1.3.2-1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +4 -4
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +20 -0
- package/dist/{types/src → src}/lib/dapp/DAppSigner.d.ts +0 -1
- package/dist/src/lib/dapp/DAppSigner.js +214 -0
- package/dist/{types/src → src}/lib/dapp/index.d.ts +0 -1
- package/dist/src/lib/dapp/index.js +431 -0
- package/dist/{types/src → src}/lib/index.d.ts +0 -1
- package/dist/src/lib/index.js +22 -0
- package/dist/{types/src → src}/lib/shared/chainIds.d.ts +0 -1
- package/dist/src/lib/shared/chainIds.js +31 -0
- package/dist/{types/src → src}/lib/shared/errors.d.ts +0 -1
- package/dist/src/lib/shared/errors.js +51 -0
- package/dist/{types/src → src}/lib/shared/events.d.ts +0 -1
- package/dist/src/lib/shared/events.js +27 -0
- package/dist/{types/src → src}/lib/shared/extensionController.d.ts +0 -1
- package/dist/src/lib/shared/extensionController.js +42 -0
- package/dist/{types/src → src}/lib/shared/index.d.ts +0 -1
- package/dist/src/lib/shared/index.js +26 -0
- package/dist/{types/src → src}/lib/shared/methods.d.ts +0 -1
- package/dist/src/lib/shared/methods.js +32 -0
- package/dist/{types/src → src}/lib/shared/payloads.d.ts +0 -1
- package/dist/src/lib/shared/payloads.js +20 -0
- package/dist/{types/src → src}/lib/shared/utils.d.ts +0 -1
- package/dist/src/lib/shared/utils.js +424 -0
- package/dist/{types/src → src}/lib/wallet/index.d.ts +0 -1
- package/dist/src/lib/wallet/index.js +301 -0
- package/dist/{types/src → src}/lib/wallet/provider.d.ts +0 -1
- package/dist/src/lib/wallet/provider.js +58 -0
- package/dist/{types/src → src}/lib/wallet/types.d.ts +0 -1
- package/dist/src/lib/wallet/types.js +20 -0
- package/dist/{types/test → test}/_helpers.d.ts +0 -1
- package/dist/test/_helpers.js +95 -0
- package/dist/test/dapp/DAppConnector.test.d.ts +1 -0
- package/dist/test/dapp/DAppConnector.test.js +236 -0
- package/dist/test/dapp/DAppSigner.test.d.ts +1 -0
- package/dist/test/dapp/DAppSigner.test.js +155 -0
- package/dist/test/utils.test.d.ts +1 -0
- package/dist/test/utils.test.js +202 -0
- package/dist/test/wallet/methods/wallet-executeTransaction.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-executeTransaction.test.js +43 -0
- package/dist/test/wallet/methods/wallet-getNodeAddresses.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-getNodeAddresses.test.js +40 -0
- package/dist/test/wallet/methods/wallet-signAndExecuteQuery.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-signAndExecuteQuery.test.js +41 -0
- package/dist/test/wallet/methods/wallet-signAndExecuteTransaction.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-signAndExecuteTransaction.test.js +48 -0
- package/dist/test/wallet/methods/wallet-signMessage.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-signMessage.test.js +60 -0
- package/dist/test/wallet/methods/wallet-signTransaction.test.d.ts +1 -0
- package/dist/test/wallet/methods/wallet-signTransaction.test.js +45 -0
- package/dist/test/wallet/wallet-init.test.d.ts +1 -0
- package/dist/test/wallet/wallet-init.test.js +53 -0
- package/package.json +11 -19
- package/dist/browser-cjs-metafile.json +0 -1
- package/dist/browser-cjs.js +0 -250
- package/dist/browser-esm-metafile.json +0 -1
- package/dist/browser-esm.js +0 -250
- package/dist/node-cjs-metafile.json +0 -1
- package/dist/node-cjs.js +0 -11404
- package/dist/node-esm-metafile.json +0 -1
- package/dist/node-esm.js +0 -11404
- package/dist/types/src/index.d.ts +0 -2
- package/dist/types/src/index.d.ts.map +0 -1
- package/dist/types/src/lib/dapp/DAppSigner.d.ts.map +0 -1
- package/dist/types/src/lib/dapp/index.d.ts.map +0 -1
- package/dist/types/src/lib/index.d.ts.map +0 -1
- package/dist/types/src/lib/shared/chainIds.d.ts.map +0 -1
- package/dist/types/src/lib/shared/errors.d.ts.map +0 -1
- package/dist/types/src/lib/shared/events.d.ts.map +0 -1
- package/dist/types/src/lib/shared/extensionController.d.ts.map +0 -1
- package/dist/types/src/lib/shared/index.d.ts.map +0 -1
- package/dist/types/src/lib/shared/methods.d.ts.map +0 -1
- package/dist/types/src/lib/shared/payloads.d.ts.map +0 -1
- package/dist/types/src/lib/shared/utils.d.ts.map +0 -1
- package/dist/types/src/lib/wallet/index.d.ts.map +0 -1
- package/dist/types/src/lib/wallet/provider.d.ts.map +0 -1
- package/dist/types/src/lib/wallet/types.d.ts.map +0 -1
- package/dist/types/test/_helpers.d.ts.map +0 -1
- package/dist/types/test/dapp/DAppConnector.test.d.ts +0 -2
- package/dist/types/test/dapp/DAppConnector.test.d.ts.map +0 -1
- package/dist/types/test/dapp/DAppSigner.test.d.ts +0 -2
- package/dist/types/test/dapp/DAppSigner.test.d.ts.map +0 -1
- package/dist/types/test/utils.test.d.ts +0 -2
- package/dist/types/test/utils.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-executeTransaction.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-executeTransaction.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-getNodeAddresses.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-getNodeAddresses.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-signAndExecuteQuery.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-signAndExecuteQuery.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-signAndExecuteTransaction.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-signAndExecuteTransaction.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-signMessage.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-signMessage.test.d.ts.map +0 -1
- package/dist/types/test/wallet/methods/wallet-signTransaction.test.d.ts +0 -2
- package/dist/types/test/wallet/methods/wallet-signTransaction.test.d.ts.map +0 -1
- package/dist/types/test/wallet/wallet-init.test.d.ts +0 -2
- package/dist/types/test/wallet/wallet-init.test.d.ts.map +0 -1
@@ -0,0 +1,236 @@
|
|
1
|
+
/*
|
2
|
+
*
|
3
|
+
* Hedera Wallet Connect
|
4
|
+
*
|
5
|
+
* Copyright (C) 2023 Hedera Hashgraph, LLC
|
6
|
+
*
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
* you may not use this file except in compliance with the License.
|
9
|
+
* You may obtain a copy of the License at
|
10
|
+
*
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
*
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
* See the License for the specific language governing permissions and
|
17
|
+
* limitations under the License.
|
18
|
+
*
|
19
|
+
*/
|
20
|
+
import { AccountId, AccountInfoQuery, LedgerId, TopicCreateTransaction } from '@hashgraph/sdk';
|
21
|
+
import { DAppConnector, HederaJsonRpcMethod, HederaSessionEvent, queryToBase64String, transactionToBase64String, transactionToTransactionBody, transactionBodyToBase64String, } from '../../src';
|
22
|
+
import { projectId, dAppMetadata, useJsonFixture, prepareTestTransaction, testUserAccountId, } from '../_helpers';
|
23
|
+
import Client from '@walletconnect/sign-client';
|
24
|
+
describe('DAppConnector', () => {
|
25
|
+
let connector;
|
26
|
+
const fakeSession = useJsonFixture('fakeSession');
|
27
|
+
beforeEach(() => {
|
28
|
+
connector = new DAppConnector(dAppMetadata, LedgerId.TESTNET, projectId);
|
29
|
+
});
|
30
|
+
afterEach(() => {
|
31
|
+
global.gc && global.gc();
|
32
|
+
});
|
33
|
+
describe('constructor', () => {
|
34
|
+
it('should create a valid class object', () => {
|
35
|
+
const methods = ['hedera_testMethod', 'any_testMethod'];
|
36
|
+
const events = [HederaSessionEvent.ChainChanged, HederaSessionEvent.AccountsChanged];
|
37
|
+
connector = new DAppConnector(dAppMetadata, LedgerId.TESTNET, projectId, methods, events);
|
38
|
+
expect(connector.dAppMetadata).toBe(dAppMetadata);
|
39
|
+
expect(connector.network).toBe(LedgerId.TESTNET);
|
40
|
+
expect(connector.projectId).toBe(projectId);
|
41
|
+
expect(connector.supportedMethods).toEqual(methods);
|
42
|
+
expect(connector.supportedEvents).toEqual(events);
|
43
|
+
});
|
44
|
+
it('should create a valid class object without passing of methods and events', () => {
|
45
|
+
connector = new DAppConnector(dAppMetadata, LedgerId.TESTNET, projectId);
|
46
|
+
expect(connector.dAppMetadata).toBe(dAppMetadata);
|
47
|
+
expect(connector.network).toBe(LedgerId.TESTNET);
|
48
|
+
expect(connector.projectId).toBe(projectId);
|
49
|
+
expect(connector.supportedMethods).toEqual(Object.values(HederaJsonRpcMethod));
|
50
|
+
expect(connector.supportedEvents).toEqual([]);
|
51
|
+
});
|
52
|
+
});
|
53
|
+
describe('init', () => {
|
54
|
+
it('should init SignClient correctly', async () => {
|
55
|
+
var _a, _b, _c;
|
56
|
+
await connector.init({ logger: 'error' });
|
57
|
+
expect(connector.walletConnectClient).toBeInstanceOf(Client);
|
58
|
+
expect((_a = connector.walletConnectClient) === null || _a === void 0 ? void 0 : _a.metadata).toBe(dAppMetadata);
|
59
|
+
expect((_b = connector.walletConnectClient) === null || _b === void 0 ? void 0 : _b.core.projectId).toBe(projectId);
|
60
|
+
expect((_c = connector.walletConnectClient) === null || _c === void 0 ? void 0 : _c.core.relayUrl).toBe('wss://relay.walletconnect.com');
|
61
|
+
});
|
62
|
+
it('should create signers if there are a persisted sessions', async () => {
|
63
|
+
const checkPersistedStateSpy = jest.spyOn(connector, 'checkPersistedState');
|
64
|
+
checkPersistedStateSpy.mockReturnValue([fakeSession]);
|
65
|
+
await connector.init({ logger: 'error' });
|
66
|
+
expect(checkPersistedStateSpy).toHaveBeenCalled();
|
67
|
+
expect(connector.signers[0].getAccountId().toString()).toBe(fakeSession.namespaces.hedera.accounts[0].split(':')[2]);
|
68
|
+
expect(connector.signers[0].topic).toBe(fakeSession.topic);
|
69
|
+
expect(connector.signers[0].getLedgerId()).toBe(LedgerId.TESTNET);
|
70
|
+
checkPersistedStateSpy.mockRestore();
|
71
|
+
});
|
72
|
+
});
|
73
|
+
describe('disconnect', () => {
|
74
|
+
beforeEach(async () => {
|
75
|
+
const checkPersistedStateSpy = jest.spyOn(connector, 'checkPersistedState');
|
76
|
+
checkPersistedStateSpy.mockReturnValue([fakeSession]);
|
77
|
+
await connector.init({ logger: 'error' });
|
78
|
+
checkPersistedStateSpy.mockRestore();
|
79
|
+
});
|
80
|
+
it('should disconnect Client from topic', async () => {
|
81
|
+
const walletConnectDisconnectSpy = jest.spyOn(connector.walletConnectClient, 'disconnect');
|
82
|
+
walletConnectDisconnectSpy.mockImplementation(async () => { });
|
83
|
+
connector.disconnect(fakeSession.topic);
|
84
|
+
expect(walletConnectDisconnectSpy).toHaveBeenCalled();
|
85
|
+
expect(walletConnectDisconnectSpy).toHaveBeenCalledTimes(1);
|
86
|
+
expect(walletConnectDisconnectSpy).toHaveBeenCalledWith(expect.objectContaining({ topic: fakeSession.topic }));
|
87
|
+
walletConnectDisconnectSpy.mockRestore();
|
88
|
+
});
|
89
|
+
});
|
90
|
+
describe('requests', () => {
|
91
|
+
let lastSignerRequestMock;
|
92
|
+
beforeEach(async () => {
|
93
|
+
const checkPersistedStateSpy = jest.spyOn(connector, 'checkPersistedState');
|
94
|
+
checkPersistedStateSpy.mockReturnValue([fakeSession]);
|
95
|
+
await connector.init({ logger: 'error' });
|
96
|
+
checkPersistedStateSpy.mockRestore();
|
97
|
+
lastSignerRequestMock = jest.spyOn(connector.signers[0], 'request');
|
98
|
+
lastSignerRequestMock.mockImplementation(() => { });
|
99
|
+
});
|
100
|
+
afterEach(() => {
|
101
|
+
lastSignerRequestMock.mockRestore();
|
102
|
+
});
|
103
|
+
// 1
|
104
|
+
describe(DAppConnector.prototype.getNodeAddresses, () => {
|
105
|
+
it('should throw an error if there is no any signer', async () => {
|
106
|
+
connector.signers = [];
|
107
|
+
await expect(connector.getNodeAddresses()).rejects.toThrow('There is no active session. Connect to the wallet at first.');
|
108
|
+
});
|
109
|
+
it('should invoke last signer request with correct params', async () => {
|
110
|
+
await connector.getNodeAddresses();
|
111
|
+
expect(lastSignerRequestMock).toHaveBeenCalled();
|
112
|
+
expect(lastSignerRequestMock).toHaveBeenCalledTimes(1);
|
113
|
+
expect(lastSignerRequestMock).toHaveBeenCalledWith({
|
114
|
+
method: HederaJsonRpcMethod.GetNodeAddresses,
|
115
|
+
params: undefined,
|
116
|
+
});
|
117
|
+
});
|
118
|
+
});
|
119
|
+
// 2
|
120
|
+
describe(DAppConnector.prototype.executeTransaction, () => {
|
121
|
+
const transaction = prepareTestTransaction(new TopicCreateTransaction(), { freeze: true });
|
122
|
+
const params = {
|
123
|
+
transactionList: transactionToBase64String(transaction),
|
124
|
+
};
|
125
|
+
it('should throw an error if there is no any signer', async () => {
|
126
|
+
connector.signers = [];
|
127
|
+
await expect(connector.executeTransaction(params)).rejects.toThrow('There is no active session. Connect to the wallet at first.');
|
128
|
+
});
|
129
|
+
it('should invoke last signer request with correct params', async () => {
|
130
|
+
await connector.executeTransaction(params);
|
131
|
+
expect(lastSignerRequestMock).toHaveBeenCalled();
|
132
|
+
expect(lastSignerRequestMock).toHaveBeenCalledTimes(1);
|
133
|
+
expect(lastSignerRequestMock).toHaveBeenCalledWith({
|
134
|
+
method: HederaJsonRpcMethod.ExecuteTransaction,
|
135
|
+
params,
|
136
|
+
});
|
137
|
+
});
|
138
|
+
});
|
139
|
+
// 3
|
140
|
+
describe(DAppConnector.prototype.signMessage, () => {
|
141
|
+
const params = {
|
142
|
+
message: 'test message',
|
143
|
+
signerAccountId: testUserAccountId.toString(),
|
144
|
+
};
|
145
|
+
it('should throw an error if there is no any signer', async () => {
|
146
|
+
connector.signers = [];
|
147
|
+
await expect(connector.signMessage(params)).rejects.toThrow('There is no active session. Connect to the wallet at first.');
|
148
|
+
});
|
149
|
+
it('should invoke last signer request with correct params', async () => {
|
150
|
+
await connector.signMessage(params);
|
151
|
+
expect(lastSignerRequestMock).toHaveBeenCalled();
|
152
|
+
expect(lastSignerRequestMock).toHaveBeenCalledTimes(1);
|
153
|
+
expect(lastSignerRequestMock).toHaveBeenCalledWith({
|
154
|
+
method: HederaJsonRpcMethod.SignMessage,
|
155
|
+
params,
|
156
|
+
});
|
157
|
+
});
|
158
|
+
});
|
159
|
+
// 4
|
160
|
+
describe(DAppConnector.prototype.signAndExecuteQuery, () => {
|
161
|
+
const query = new AccountInfoQuery().setAccountId(testUserAccountId.toString());
|
162
|
+
const params = {
|
163
|
+
signerAccountId: testUserAccountId.toString(),
|
164
|
+
query: queryToBase64String(query),
|
165
|
+
};
|
166
|
+
it('should throw an error if there is no any signer', async () => {
|
167
|
+
connector.signers = [];
|
168
|
+
await expect(connector.signAndExecuteQuery(params)).rejects.toThrow('There is no active session. Connect to the wallet at first.');
|
169
|
+
});
|
170
|
+
it('should invoke last signer request with correct params', async () => {
|
171
|
+
await connector.signAndExecuteQuery(params);
|
172
|
+
expect(lastSignerRequestMock).toHaveBeenCalled();
|
173
|
+
expect(lastSignerRequestMock).toHaveBeenCalledTimes(1);
|
174
|
+
expect(lastSignerRequestMock).toHaveBeenCalledWith({
|
175
|
+
method: HederaJsonRpcMethod.SignAndExecuteQuery,
|
176
|
+
params,
|
177
|
+
});
|
178
|
+
});
|
179
|
+
});
|
180
|
+
// 5
|
181
|
+
describe(DAppConnector.prototype.signAndExecuteTransaction, () => {
|
182
|
+
const transaction = prepareTestTransaction(new TopicCreateTransaction(), { freeze: true });
|
183
|
+
const params = {
|
184
|
+
signerAccountId: testUserAccountId.toString(),
|
185
|
+
transactionList: transactionToBase64String(transaction),
|
186
|
+
};
|
187
|
+
it('should throw an error if there is no any signer', async () => {
|
188
|
+
connector.signers = [];
|
189
|
+
await expect(connector.signAndExecuteTransaction(params)).rejects.toThrow('There is no active session. Connect to the wallet at first.');
|
190
|
+
});
|
191
|
+
it('should invoke last signer request with correct params', async () => {
|
192
|
+
await connector.signAndExecuteTransaction(params);
|
193
|
+
expect(lastSignerRequestMock).toHaveBeenCalled();
|
194
|
+
expect(lastSignerRequestMock).toHaveBeenCalledTimes(1);
|
195
|
+
expect(lastSignerRequestMock).toHaveBeenCalledWith({
|
196
|
+
method: HederaJsonRpcMethod.SignAndExecuteTransaction,
|
197
|
+
params,
|
198
|
+
});
|
199
|
+
});
|
200
|
+
});
|
201
|
+
// 6
|
202
|
+
describe(DAppConnector.prototype.signTransaction, () => {
|
203
|
+
const transaction = prepareTestTransaction(new TopicCreateTransaction(), { freeze: true });
|
204
|
+
const params = {
|
205
|
+
signerAccountId: testUserAccountId.toString(),
|
206
|
+
transactionBody: transactionBodyToBase64String(
|
207
|
+
// must specify a node account id for the transaction body
|
208
|
+
transactionToTransactionBody(transaction, AccountId.fromString('0.0.3'))),
|
209
|
+
};
|
210
|
+
it('should throw an error if there is no any signer', async () => {
|
211
|
+
connector.signers = [];
|
212
|
+
await expect(connector.signTransaction(params)).rejects.toThrow('There is no active session. Connect to the wallet at first.');
|
213
|
+
});
|
214
|
+
it('should invoke last signer request with correct params', async () => {
|
215
|
+
await connector.signTransaction(params);
|
216
|
+
expect(lastSignerRequestMock).toHaveBeenCalled();
|
217
|
+
expect(lastSignerRequestMock).toHaveBeenCalledTimes(1);
|
218
|
+
expect(lastSignerRequestMock).toHaveBeenCalledWith({
|
219
|
+
method: HederaJsonRpcMethod.SignTransaction,
|
220
|
+
params,
|
221
|
+
});
|
222
|
+
});
|
223
|
+
});
|
224
|
+
});
|
225
|
+
// describe('connect', () => {
|
226
|
+
// it('should establish connection and create session', async () => {
|
227
|
+
// connector = new DAppConnector(appMetadata, LedgerId.TESTNET, PROJECT_ID)
|
228
|
+
// await connector.init()
|
229
|
+
// expect(connector.walletConnectClient).not.toBeNull()
|
230
|
+
// await connector.connect((pairing) => {
|
231
|
+
// console.log('PairingString: ', pairing)
|
232
|
+
// })
|
233
|
+
// expect(connector.walletConnectClient?.session.getAll()).toHaveLength(1)
|
234
|
+
// }, 60_000)
|
235
|
+
// })
|
236
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,155 @@
|
|
1
|
+
/*
|
2
|
+
*
|
3
|
+
* Hedera Wallet Connect
|
4
|
+
*
|
5
|
+
* Copyright (C) 2023 Hedera Hashgraph, LLC
|
6
|
+
*
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
* you may not use this file except in compliance with the License.
|
9
|
+
* You may obtain a copy of the License at
|
10
|
+
*
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
*
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
* See the License for the specific language governing permissions and
|
17
|
+
* limitations under the License.
|
18
|
+
*
|
19
|
+
*/
|
20
|
+
import { AccountBalanceQuery, AccountCreateTransaction, AccountId, AccountInfoQuery, AccountRecordsQuery, AccountUpdateTransaction, LedgerId, PrivateKey, TokenAssociateTransaction, TokenCreateTransaction, TopicCreateTransaction, TransactionId, } from '@hashgraph/sdk';
|
21
|
+
import { proto } from '@hashgraph/proto';
|
22
|
+
import { DAppConnector, HederaJsonRpcMethod, transactionToBase64String, DAppSigner, Uint8ArrayToBase64String, base64StringToQuery, } from '../../src';
|
23
|
+
import { projectId, dAppMetadata, useJsonFixture, prepareTestTransaction, prepareTestQuery, } from '../_helpers';
|
24
|
+
describe('DAppSigner', () => {
|
25
|
+
let connector;
|
26
|
+
const fakeSession = useJsonFixture('fakeSession');
|
27
|
+
beforeEach(() => {
|
28
|
+
connector = new DAppConnector(dAppMetadata, LedgerId.TESTNET, projectId);
|
29
|
+
});
|
30
|
+
afterEach(() => {
|
31
|
+
global.gc && global.gc();
|
32
|
+
});
|
33
|
+
describe(DAppSigner.prototype.call, () => {
|
34
|
+
let signerRequestSpy;
|
35
|
+
let signer;
|
36
|
+
beforeEach(async () => {
|
37
|
+
// @ts-ignore
|
38
|
+
connector.signers = connector.createSigners(fakeSession);
|
39
|
+
signer = connector.signers[0];
|
40
|
+
signerRequestSpy = jest.spyOn(signer, 'request');
|
41
|
+
signerRequestSpy.mockImplementation((request) => {
|
42
|
+
const { method } = request;
|
43
|
+
if (method === HederaJsonRpcMethod.SignAndExecuteTransaction) {
|
44
|
+
const response = {
|
45
|
+
transactionId: TransactionId.generate('0.0.999').toString(),
|
46
|
+
nodeId: '0.0.3',
|
47
|
+
transactionHash: '0x',
|
48
|
+
};
|
49
|
+
return Promise.resolve(response);
|
50
|
+
}
|
51
|
+
else if (method === HederaJsonRpcMethod.ExecuteTransaction) {
|
52
|
+
const response = {
|
53
|
+
transactionId: TransactionId.generate('0.0.999').toString(),
|
54
|
+
nodeId: '0.0.3',
|
55
|
+
transactionHash: '0x',
|
56
|
+
};
|
57
|
+
return Promise.resolve(response);
|
58
|
+
}
|
59
|
+
else if (method === HederaJsonRpcMethod.SignAndExecuteQuery) {
|
60
|
+
const query = base64StringToQuery(request.params.query);
|
61
|
+
let queryResponse = 'ERROR: Unsupported query type';
|
62
|
+
if (query instanceof AccountBalanceQuery) {
|
63
|
+
queryResponse = Uint8ArrayToBase64String(proto.CryptoGetAccountBalanceResponse.encode({
|
64
|
+
balance: 0,
|
65
|
+
}).finish());
|
66
|
+
}
|
67
|
+
else if (query instanceof AccountInfoQuery) {
|
68
|
+
queryResponse = Uint8ArrayToBase64String(proto.CryptoGetInfoResponse.AccountInfo.encode({
|
69
|
+
accountID: {
|
70
|
+
shardNum: 0,
|
71
|
+
realmNum: 0,
|
72
|
+
accountNum: 3,
|
73
|
+
},
|
74
|
+
contractAccountID: AccountId.fromString('0.0.3').toSolidityAddress(),
|
75
|
+
key: {
|
76
|
+
ed25519: PrivateKey.generate().publicKey.toBytes(),
|
77
|
+
},
|
78
|
+
expirationTime: { seconds: 0, nanos: 0 },
|
79
|
+
}).finish());
|
80
|
+
}
|
81
|
+
else if (query instanceof AccountRecordsQuery) {
|
82
|
+
queryResponse = Uint8ArrayToBase64String(proto.TransactionGetRecordResponse.encode({
|
83
|
+
transactionRecord: {
|
84
|
+
alias: proto.Key.encode(PrivateKey.generate().publicKey._toProtobufKey()).finish(),
|
85
|
+
receipt: {
|
86
|
+
status: proto.ResponseCodeEnum.OK,
|
87
|
+
accountID: {
|
88
|
+
shardNum: 0,
|
89
|
+
realmNum: 0,
|
90
|
+
accountNum: 3,
|
91
|
+
},
|
92
|
+
},
|
93
|
+
consensusTimestamp: { seconds: 0, nanos: 0 },
|
94
|
+
transactionID: {
|
95
|
+
accountID: {
|
96
|
+
shardNum: 0,
|
97
|
+
realmNum: 0,
|
98
|
+
accountNum: 3,
|
99
|
+
},
|
100
|
+
transactionValidStart: { seconds: 0, nanos: 0 },
|
101
|
+
nonce: 0,
|
102
|
+
},
|
103
|
+
},
|
104
|
+
}).finish());
|
105
|
+
}
|
106
|
+
const response = {
|
107
|
+
response: queryResponse,
|
108
|
+
};
|
109
|
+
return Promise.resolve(response);
|
110
|
+
}
|
111
|
+
});
|
112
|
+
});
|
113
|
+
afterEach(() => {
|
114
|
+
signerRequestSpy.mockRestore();
|
115
|
+
});
|
116
|
+
it.each([
|
117
|
+
{ name: AccountCreateTransaction.name, ExecutableType: AccountCreateTransaction },
|
118
|
+
{ name: AccountUpdateTransaction.name, ExecutableType: AccountUpdateTransaction },
|
119
|
+
{ name: TopicCreateTransaction.name, ExecutableType: TopicCreateTransaction },
|
120
|
+
{ name: TokenAssociateTransaction.name, ExecutableType: TokenAssociateTransaction },
|
121
|
+
{ name: TokenCreateTransaction.name, ExecutableType: TokenCreateTransaction },
|
122
|
+
])('can execute $name transaction', async ({ name, ExecutableType }) => {
|
123
|
+
const transaction = prepareTestTransaction(new ExecutableType(), { freeze: true });
|
124
|
+
const params = {
|
125
|
+
signerAccountId: 'hedera:testnet:' + signer.getAccountId().toString(),
|
126
|
+
transactionList: transactionToBase64String(transaction),
|
127
|
+
};
|
128
|
+
await signer.call(transaction);
|
129
|
+
expect(signerRequestSpy).toHaveBeenCalled();
|
130
|
+
expect(signerRequestSpy).toHaveBeenCalledTimes(1);
|
131
|
+
expect(signerRequestSpy).toHaveBeenCalledWith({
|
132
|
+
method: HederaJsonRpcMethod.SignAndExecuteTransaction,
|
133
|
+
params,
|
134
|
+
});
|
135
|
+
});
|
136
|
+
it.each([
|
137
|
+
{ name: AccountBalanceQuery.name, ExecutableType: AccountBalanceQuery },
|
138
|
+
{ name: AccountInfoQuery.name, ExecutableType: AccountInfoQuery },
|
139
|
+
{ name: AccountRecordsQuery.name, ExecutableType: AccountRecordsQuery },
|
140
|
+
])('can execute $name query', async ({ name, ExecutableType }) => {
|
141
|
+
const query = prepareTestQuery(new ExecutableType());
|
142
|
+
const params = {
|
143
|
+
signerAccountId: 'hedera:testnet:' + signer.getAccountId().toString(),
|
144
|
+
query: Uint8ArrayToBase64String(query.toBytes()),
|
145
|
+
};
|
146
|
+
await signer.call(query);
|
147
|
+
expect(signerRequestSpy).toHaveBeenCalled();
|
148
|
+
expect(signerRequestSpy).toHaveBeenCalledTimes(1);
|
149
|
+
expect(signerRequestSpy).toHaveBeenCalledWith({
|
150
|
+
method: HederaJsonRpcMethod.SignAndExecuteQuery,
|
151
|
+
params,
|
152
|
+
});
|
153
|
+
});
|
154
|
+
});
|
155
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,202 @@
|
|
1
|
+
/*
|
2
|
+
*
|
3
|
+
* Hedera Wallet Connect
|
4
|
+
*
|
5
|
+
* Copyright (C) 2023 Hedera Hashgraph, LLC
|
6
|
+
*
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
* you may not use this file except in compliance with the License.
|
9
|
+
* You may obtain a copy of the License at
|
10
|
+
*
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
*
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
* See the License for the specific language governing permissions and
|
17
|
+
* limitations under the License.
|
18
|
+
*
|
19
|
+
*/
|
20
|
+
import { AccountId, AccountInfoQuery, LedgerId, Query, TopicCreateTransaction, } from '@hashgraph/sdk';
|
21
|
+
import { transactionToBase64String, freezeTransaction, setDefaultNodeAccountIds, base64StringToTransaction, base64StringToUint8Array, Uint8ArrayToBase64String, base64StringToQuery, queryToBase64String, EIPChainIdToLedgerId, ledgerIdToEIPChainId, CAIPChainIdToLedgerId, HederaChainId, ledgerIdToCAIPChainId, networkNameToCAIPChainId, networkNameToEIPChainId, networkNamespaces, HederaSessionEvent, } from '../src';
|
22
|
+
import { prepareTestTransaction, testUserAccountId, useJsonFixture } from './_helpers';
|
23
|
+
describe(freezeTransaction.name, () => {
|
24
|
+
it('should freeze an unfrozen transaction', () => {
|
25
|
+
const txn = prepareTestTransaction(new TopicCreateTransaction());
|
26
|
+
expect(txn.isFrozen()).toBe(false);
|
27
|
+
freezeTransaction(txn);
|
28
|
+
expect(txn.isFrozen()).toBe(true);
|
29
|
+
});
|
30
|
+
it('should have no effect on a frozen transaction', () => {
|
31
|
+
const txn = prepareTestTransaction(new TopicCreateTransaction());
|
32
|
+
txn.freeze();
|
33
|
+
expect(txn.isFrozen()).toBe(true);
|
34
|
+
freezeTransaction(txn);
|
35
|
+
expect(txn.isFrozen()).toBe(true);
|
36
|
+
});
|
37
|
+
});
|
38
|
+
describe(setDefaultNodeAccountIds.name, () => {
|
39
|
+
it('should set default node account ids if none are set', () => {
|
40
|
+
var _a;
|
41
|
+
const txn = new TopicCreateTransaction();
|
42
|
+
expect(txn.nodeAccountIds).toBeNull();
|
43
|
+
setDefaultNodeAccountIds(txn);
|
44
|
+
const result = (_a = txn.nodeAccountIds) === null || _a === void 0 ? void 0 : _a.map((id) => id.toString());
|
45
|
+
expect(result).toEqual(['0.0.3', '0.0.4', '0.0.5']);
|
46
|
+
});
|
47
|
+
it('should do nothing if node account ids are already set', () => {
|
48
|
+
var _a;
|
49
|
+
const txn = new TopicCreateTransaction();
|
50
|
+
txn.setNodeAccountIds([new AccountId(4)]);
|
51
|
+
setDefaultNodeAccountIds(txn);
|
52
|
+
const result = (_a = txn.nodeAccountIds) === null || _a === void 0 ? void 0 : _a.map((id) => id.toString());
|
53
|
+
expect(result).toEqual(['0.0.4']);
|
54
|
+
});
|
55
|
+
});
|
56
|
+
describe(transactionToBase64String.name, () => {
|
57
|
+
it('should convert a transaction to a base64 encoded string', () => {
|
58
|
+
const txn = prepareTestTransaction(new TopicCreateTransaction());
|
59
|
+
const result = transactionToBase64String(txn);
|
60
|
+
const { expected } = useJsonFixture('transactionToBase64StringResult');
|
61
|
+
expect(result).toBe(expected);
|
62
|
+
});
|
63
|
+
});
|
64
|
+
describe(base64StringToTransaction.name, () => {
|
65
|
+
it('should create a transaction from a base64 string', () => {
|
66
|
+
const txn = prepareTestTransaction(new TopicCreateTransaction());
|
67
|
+
txn.setTransactionMemo('I should be restored');
|
68
|
+
const str = transactionToBase64String(txn);
|
69
|
+
const resultWithParam = base64StringToTransaction(str);
|
70
|
+
const resultWithoutParam = base64StringToTransaction(str);
|
71
|
+
expect(resultWithParam).toBeInstanceOf(TopicCreateTransaction);
|
72
|
+
expect(resultWithoutParam).toBeInstanceOf(TopicCreateTransaction);
|
73
|
+
expect(resultWithParam.transactionMemo).toBe('I should be restored');
|
74
|
+
});
|
75
|
+
});
|
76
|
+
describe(`Uint8Array helpers`, () => {
|
77
|
+
let uInt8Array;
|
78
|
+
describe(base64StringToUint8Array.name, () => {
|
79
|
+
it('should decode base64 string to Uint8Array', async () => {
|
80
|
+
const base64String = btoa('Hello World!');
|
81
|
+
uInt8Array = base64StringToUint8Array(base64String);
|
82
|
+
console.log(Array.from(uInt8Array));
|
83
|
+
expect(uInt8Array).toBeInstanceOf(Uint8Array);
|
84
|
+
expect(Array.from(uInt8Array)).toEqual([
|
85
|
+
72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33,
|
86
|
+
]);
|
87
|
+
});
|
88
|
+
});
|
89
|
+
describe(Uint8ArrayToBase64String.name, () => {
|
90
|
+
it('should encode Uint8Array to base64 string', async () => {
|
91
|
+
const base64String = Uint8ArrayToBase64String(uInt8Array);
|
92
|
+
expect(typeof base64String).toBe('string');
|
93
|
+
expect(base64String).toBe('SGVsbG8gV29ybGQh');
|
94
|
+
expect(atob(base64String)).toBe('Hello World!');
|
95
|
+
});
|
96
|
+
});
|
97
|
+
});
|
98
|
+
describe(`Query helpers`, () => {
|
99
|
+
let base64Query;
|
100
|
+
describe(queryToBase64String.name, () => {
|
101
|
+
it('should encode Query to base64 string', async () => {
|
102
|
+
const query = new AccountInfoQuery().setAccountId(testUserAccountId);
|
103
|
+
base64Query = queryToBase64String(query);
|
104
|
+
expect(typeof base64Query).toBe('string');
|
105
|
+
expect(base64Query).toEqual('Sg0KAhAAEgcIABAAGLlg');
|
106
|
+
});
|
107
|
+
});
|
108
|
+
describe(base64StringToQuery.name, () => {
|
109
|
+
it('should decode base64 string to Query', async () => {
|
110
|
+
var _a;
|
111
|
+
const query = base64StringToQuery(base64Query);
|
112
|
+
expect(query).toBeInstanceOf(Query);
|
113
|
+
expect(query).toBeInstanceOf(AccountInfoQuery);
|
114
|
+
expect((_a = query.accountId) === null || _a === void 0 ? void 0 : _a.toString()).toBe(testUserAccountId.toString());
|
115
|
+
});
|
116
|
+
});
|
117
|
+
});
|
118
|
+
describe(EIPChainIdToLedgerId.name, () => {
|
119
|
+
it('should convert a EIP chain id to a LedgerId object', async () => {
|
120
|
+
const mainnetLedgerId = EIPChainIdToLedgerId(295);
|
121
|
+
const testnetLedgerId = EIPChainIdToLedgerId(296);
|
122
|
+
const previewnetLedgerId = EIPChainIdToLedgerId(297);
|
123
|
+
const localnodeLedgerId = EIPChainIdToLedgerId(298);
|
124
|
+
const localnodeLedgerIdWithRandomId = EIPChainIdToLedgerId(999);
|
125
|
+
expect(mainnetLedgerId).toBe(LedgerId.MAINNET);
|
126
|
+
expect(testnetLedgerId).toBe(LedgerId.TESTNET);
|
127
|
+
expect(previewnetLedgerId).toBe(LedgerId.PREVIEWNET);
|
128
|
+
expect(localnodeLedgerId).toBe(LedgerId.LOCAL_NODE);
|
129
|
+
expect(localnodeLedgerIdWithRandomId).toBe(LedgerId.LOCAL_NODE);
|
130
|
+
});
|
131
|
+
});
|
132
|
+
describe(ledgerIdToEIPChainId.name, () => {
|
133
|
+
it('should convert a LedgerId object to a EIP chain id', async () => {
|
134
|
+
const mainnetChainId = ledgerIdToEIPChainId(LedgerId.MAINNET);
|
135
|
+
const testnetChainId = ledgerIdToEIPChainId(LedgerId.TESTNET);
|
136
|
+
const previewnetChainId = ledgerIdToEIPChainId(LedgerId.PREVIEWNET);
|
137
|
+
const localnodeChainId = ledgerIdToEIPChainId(LedgerId.LOCAL_NODE);
|
138
|
+
expect(mainnetChainId).toBe(295);
|
139
|
+
expect(testnetChainId).toBe(296);
|
140
|
+
expect(previewnetChainId).toBe(297);
|
141
|
+
expect(localnodeChainId).toBe(298);
|
142
|
+
});
|
143
|
+
});
|
144
|
+
describe(networkNameToEIPChainId.name, () => {
|
145
|
+
it('should convert a network name to a EIP chain id', async () => {
|
146
|
+
const mainnetChainId = networkNameToEIPChainId('mainnet');
|
147
|
+
const testnetChainId = networkNameToEIPChainId('testnet');
|
148
|
+
const previewnetChainId = networkNameToEIPChainId('previewnet');
|
149
|
+
const localnodeChainId = networkNameToEIPChainId('devnet');
|
150
|
+
expect(mainnetChainId).toBe(295);
|
151
|
+
expect(testnetChainId).toBe(296);
|
152
|
+
expect(previewnetChainId).toBe(297);
|
153
|
+
expect(localnodeChainId).toBe(298);
|
154
|
+
});
|
155
|
+
});
|
156
|
+
describe(CAIPChainIdToLedgerId.name, () => {
|
157
|
+
it('should convert a CAIP chain id to a LedgerId object', async () => {
|
158
|
+
const mainnetLedgerId = CAIPChainIdToLedgerId(HederaChainId.Mainnet);
|
159
|
+
const testnetLedgerId = CAIPChainIdToLedgerId(HederaChainId.Testnet);
|
160
|
+
const previewnetLedgerId = CAIPChainIdToLedgerId(HederaChainId.Previewnet);
|
161
|
+
const localnodeLedgerId = CAIPChainIdToLedgerId(HederaChainId.Devnet);
|
162
|
+
expect(mainnetLedgerId).toBe(LedgerId.MAINNET);
|
163
|
+
expect(testnetLedgerId).toBe(LedgerId.TESTNET);
|
164
|
+
expect(previewnetLedgerId).toBe(LedgerId.PREVIEWNET);
|
165
|
+
expect(localnodeLedgerId).toBe(LedgerId.LOCAL_NODE);
|
166
|
+
});
|
167
|
+
});
|
168
|
+
describe(ledgerIdToCAIPChainId.name, () => {
|
169
|
+
it('should convert a LedgerId object to a CAIP chain id', async () => {
|
170
|
+
const mainnetChainId = ledgerIdToCAIPChainId(LedgerId.MAINNET);
|
171
|
+
const testnetChainId = ledgerIdToCAIPChainId(LedgerId.TESTNET);
|
172
|
+
const previewnetChainId = ledgerIdToCAIPChainId(LedgerId.PREVIEWNET);
|
173
|
+
const localnodeChainId = ledgerIdToCAIPChainId(LedgerId.LOCAL_NODE);
|
174
|
+
expect(mainnetChainId).toBe(HederaChainId.Mainnet);
|
175
|
+
expect(testnetChainId).toBe(HederaChainId.Testnet);
|
176
|
+
expect(previewnetChainId).toBe(HederaChainId.Previewnet);
|
177
|
+
expect(localnodeChainId).toBe(HederaChainId.Devnet);
|
178
|
+
});
|
179
|
+
});
|
180
|
+
describe(networkNameToCAIPChainId.name, () => {
|
181
|
+
it('should convert a network name to a CAIP chain id', async () => {
|
182
|
+
const mainnetChainId = networkNameToCAIPChainId('mainnet');
|
183
|
+
const testnetChainId = networkNameToCAIPChainId('testnet');
|
184
|
+
const previewnetChainId = networkNameToCAIPChainId('previewnet');
|
185
|
+
const localnodeChainId = networkNameToCAIPChainId('devnet');
|
186
|
+
expect(mainnetChainId).toBe(HederaChainId.Mainnet);
|
187
|
+
expect(testnetChainId).toBe(HederaChainId.Testnet);
|
188
|
+
expect(previewnetChainId).toBe(HederaChainId.Previewnet);
|
189
|
+
expect(localnodeChainId).toBe(HederaChainId.Devnet);
|
190
|
+
});
|
191
|
+
});
|
192
|
+
describe(networkNamespaces.name, () => {
|
193
|
+
it('should create a `ProposalTypes.RequiredNamespaces` object for a given ledgerId', async () => {
|
194
|
+
const methods = ['hedera_signMessage'];
|
195
|
+
const events = Object.values(HederaSessionEvent);
|
196
|
+
const testnetNamespaces = networkNamespaces(LedgerId.TESTNET, methods, events);
|
197
|
+
expect(testnetNamespaces.hedera).not.toBe(undefined);
|
198
|
+
expect(testnetNamespaces.hedera.methods).toBe(methods);
|
199
|
+
expect(testnetNamespaces.hedera.events).toBe(events);
|
200
|
+
expect(testnetNamespaces.hedera.chains).toEqual([HederaChainId.Testnet]);
|
201
|
+
});
|
202
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,43 @@
|
|
1
|
+
/*
|
2
|
+
*
|
3
|
+
* Hedera Wallet Connect
|
4
|
+
*
|
5
|
+
* Copyright (C) 2023 Hedera Hashgraph, LLC
|
6
|
+
*
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
* you may not use this file except in compliance with the License.
|
9
|
+
* You may obtain a copy of the License at
|
10
|
+
*
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
*
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
* See the License for the specific language governing permissions and
|
17
|
+
* limitations under the License.
|
18
|
+
*
|
19
|
+
*/
|
20
|
+
import { TopicCreateTransaction } from '@hashgraph/sdk';
|
21
|
+
import { HederaChainId, Wallet } from '../../../src';
|
22
|
+
import { prepareTestTransaction, projectId, requestId, requestTopic, testPrivateKeyECDSA, testUserAccountId, useJsonFixture, walletMetadata, } from '../../_helpers';
|
23
|
+
describe(Wallet.name, () => {
|
24
|
+
describe('executeTransaction', () => {
|
25
|
+
it('should execute signed transaction, returning the transaction response', async () => {
|
26
|
+
try {
|
27
|
+
const wallet = await Wallet.create(projectId, walletMetadata);
|
28
|
+
const hederaWallet = wallet.getHederaWallet(HederaChainId.Testnet, testUserAccountId.toString(), testPrivateKeyECDSA);
|
29
|
+
const signerCallMock = jest.spyOn(hederaWallet, 'call');
|
30
|
+
signerCallMock.mockImplementation(async () => { }); // Mocking the 'call' method to do nothing
|
31
|
+
const transaction = prepareTestTransaction(new TopicCreateTransaction(), {
|
32
|
+
freeze: true,
|
33
|
+
});
|
34
|
+
const signTransaction = await hederaWallet.signTransaction(transaction);
|
35
|
+
const respondSessionRequestSpy = jest.spyOn(wallet, 'respondSessionRequest');
|
36
|
+
await wallet.hedera_executeTransaction(requestId, requestTopic, signTransaction, hederaWallet);
|
37
|
+
const mockResponse = useJsonFixture('methods/executeTransactionSuccess');
|
38
|
+
expect(respondSessionRequestSpy).toHaveBeenCalledWith(mockResponse);
|
39
|
+
}
|
40
|
+
catch (err) { }
|
41
|
+
}, 15000);
|
42
|
+
});
|
43
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|