Skip to content

Commit ed431e7

Browse files
Merge pull request #176 from aergoio/feature/updateWallet
Update wallet client when custom network updating fix a bug
2 parents b819d9c + 089224e commit ed431e7

File tree

10 files changed

+61
-23
lines changed

10 files changed

+61
-23
lines changed

.github/workflows/build.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ jobs:
1818

1919
steps:
2020
- name: Checkout code
21-
uses: actions/checkout@v2
21+
uses: actions/checkout@v4
2222

2323
- name: Setup Node.js
24-
uses: actions/setup-node@v2
24+
uses: actions/setup-node@v4
2525
with:
2626
node-version: ${{ matrix.node-version }}
2727

2828
- name: Cache node modules
29-
uses: actions/cache@v2
29+
uses: actions/cache@v4
3030
with:
3131
path: node_modules
3232
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"packages": [
44
"packages/*"
55
],
6-
"version": "2.1.1",
6+
"version": "2.1.2",
77
"useWorkspaces": true
88
}

packages/client/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@herajs/client",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "Javascript client framework for aergo",
55
"main": "dist/herajs.common.js",
66
"module": "dist/herajs.esm.js",
@@ -32,7 +32,7 @@
3232
"homepage": "https://github.com/aergoio/herajs#readme",
3333
"dependencies": {
3434
"@grpc/grpc-js": "^1.11.1",
35-
"@herajs/common": "^2.1.1",
35+
"@herajs/common": "^2.1.2",
3636
"@improbable-eng/grpc-web": "^0.9.1",
3737
"@types/google-protobuf": "^3.2.7",
3838
"google-protobuf": "^3.21.2",

packages/common/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@herajs/common",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "Herajs common library",
55
"main": "dist/herajs-common.cjs.js",
66
"module": "dist/herajs-common.esm.js",

packages/crypto/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@herajs/crypto",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "Cryptography tools for aergo",
55
"main": "dist/herajs-crypto.cjs.js",
66
"module": "dist/herajs-crypto.esm.js",
@@ -27,7 +27,7 @@
2727
},
2828
"homepage": "https://github.com/aergoio/herajs-crypto#readme",
2929
"dependencies": {
30-
"@herajs/common": "^2.1.1",
30+
"@herajs/common": "^2.1.2",
3131
"@herajs/hdkey": "^11.2.0",
3232
"@types/elliptic": "^6.4.0",
3333
"aes-js": "^3.1.1",

packages/ledger-hw-app-aergo/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@herajs/ledger-hw-app-aergo",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "Ledger Hardware app for Aergo",
55
"main": "dist/herajs-ledger-hw-app-aergo.cjs.js",
66
"module": "dist/herajs-ledger-hw-app-aergo.esm.js",
@@ -27,9 +27,9 @@
2727
},
2828
"homepage": "https://github.com/aergoio/herajs#readme",
2929
"dependencies": {
30-
"@herajs/client": "^2.1.1",
31-
"@herajs/common": "^2.1.1",
32-
"@herajs/crypto": "^2.1.1"
30+
"@herajs/client": "^2.1.2",
31+
"@herajs/common": "^2.1.2",
32+
"@herajs/crypto": "^2.1.2"
3333
},
3434
"devDependencies": {
3535
"@ledgerhq/hw-transport": "^6.24.1",

packages/vue-plugin/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "@herajs/vue-plugin",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "Add Aergo controller to your Vue.js project",
55
"main": "plugin.js",
66
"scripts": {},
77
"author": "aergo team and contributors",
88
"license": "MIT",
99
"dependencies": {
10-
"@herajs/client": "^2.1.1",
11-
"@herajs/wallet": "^2.1.1"
10+
"@herajs/client": "^2.1.2",
11+
"@herajs/wallet": "^2.1.2"
1212
},
1313
"gitHead": "97d1b15d147bb1bbff6d70dbe4bb06a60a881bc1"
1414
}

packages/wallet/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@herajs/wallet",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "SDK for making Aergo wallet applications",
55
"scripts": {
66
"type-check": "tsc --noEmit",
@@ -24,10 +24,10 @@
2424
"license": "MIT",
2525
"dependencies": {
2626
"@elderapo/typed-event-emitter": "^1.4.1",
27-
"@herajs/client": "^2.1.1",
28-
"@herajs/common": "^2.1.1",
29-
"@herajs/crypto": "^2.1.1",
30-
"@herajs/ledger-hw-app-aergo": "^2.1.1",
27+
"@herajs/client": "^2.1.2",
28+
"@herajs/common": "^2.1.2",
29+
"@herajs/crypto": "^2.1.2",
30+
"@herajs/ledger-hw-app-aergo": "^2.1.2",
3131
"@types/google-protobuf": "^3.2.7",
3232
"@types/hashmap": "^2.0.29",
3333
"@types/leveldown": "^4.0.0",

packages/wallet/src/wallet.ts

+39-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,23 @@ export class Wallet extends MiddlewareConsumer {
7575
throw new Error('supply nodeUrl in chain config or instantiate provider manually');
7676
}
7777

78+
// Check if the existing chain exists.
79+
if (this.chainConfigs.has(chainConfig.chainId)) {
80+
const existingConfig = this.chainConfigs.get(chainConfig.chainId);
81+
82+
if (existingConfig) {
83+
if (existingConfig.nodeUrl !== chainConfig.nodeUrl) {
84+
this.chainConfigs.set(chainConfig.chainId, {
85+
...existingConfig,
86+
nodeUrl: chainConfig.nodeUrl
87+
});
88+
}
89+
return;
90+
}
91+
}
92+
7893
this.chainConfigs.set(chainConfig.chainId, chainConfig);
94+
7995
if (
8096
this.chainConfigs.size === 1 &&
8197
this.defaultChainId === DEFAULT_CHAIN &&
@@ -116,9 +132,11 @@ export class Wallet extends MiddlewareConsumer {
116132
if (!this.chainConfigs.has(chainId)) {
117133
throw new Error(`trying to use not configured chainId ${chainId}`);
118134
}
135+
119136
const chainConfig = this.chainConfigs.get(chainId) as ChainConfig;
120137
let client: AergoClient;
121-
138+
139+
// If the client does not exist, create a new one.
122140
if (!this.clients.has(chainId)) {
123141
let provider = chainConfig.provider;
124142
if (typeof provider === 'function') {
@@ -127,14 +145,33 @@ export class Wallet extends MiddlewareConsumer {
127145
client = new AergoClient({}, provider);
128146
this.clients.set(chainId, client);
129147
} else {
148+
// If the client already exists, compare nodeUrl and update if necessary.
130149
client = this.clients.get(chainId) as AergoClient;
150+
let existingNodeUrl;
151+
if (client && (client as any).config && (client as any).config.url) {
152+
existingNodeUrl = (client as any).config.url;
153+
} else {
154+
existingNodeUrl = undefined;
155+
}
156+
157+
if (existingNodeUrl !== chainConfig.nodeUrl) {
158+
// Create a new `AergoClient` and replace the existing one.
159+
let provider = chainConfig.provider;
160+
if (typeof provider === 'function') {
161+
provider = new provider({ url: chainConfig.nodeUrl });
162+
}
163+
client = new AergoClient({}, provider);
164+
this.clients.set(chainId, client);
165+
}
131166
}
167+
132168
if (this.defaultLimit) {
133169
client.setDefaultLimit(this.defaultLimit);
134170
}
171+
135172
return client;
136173
}
137-
174+
138175
/**
139176
* Prepare a transaction from given account specified by simple TxBody.
140177
* Completes missing information (chainIdHash, nonce) and signs tx using key of account.

0 commit comments

Comments
 (0)