Skip to content

Commit 85bdd69

Browse files
committed
optimised cosmjs migration doc
1 parent d26a528 commit 85bdd69

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

docs/migration-from-cosmjs.md

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Migrating from CosmJS to @interchainjs/cosmos
22

3-
This guide shows you how to migrate from CosmJS to the new InterchainJS SDK. The updated examples below follow the patterns used in our unit tests, with a special focus on wallet generation that completely removes any CosmJS dependency.
3+
This guide shows you how to migrate from CosmJS to the new InterchainJS SDK. The updated examples below follow the patterns used in our unit tests, with a special focus on wallet generation and signers that completely removes any CosmJS dependency.
44

55
## 1. Overview
66

77
### Goal:
88
Replace CosmJS with @interchainjs/cosmos to sign, build, and broadcast transactions on Cosmos-based blockchains.
99

1010
### Key Improvements:
11-
- Wallet Generation: Create wallets using our own methods (using Secp256k1Auth and generateMnemonic) instead of CosmJS’s DirectSecp256k1HdWallet.
11+
- Wallet Generation: Create wallets using our own methods (using Secp256k1Auth and Bip39) instead of CosmJS’s DirectSecp256k1HdWallet.
1212
- Modular Imports: Import only the needed submodules to reduce bundle size.
1313
- Unified Signer Interfaces: Use Direct (Protobuf) or Amino signing with a consistent API.
1414

@@ -23,7 +23,6 @@ npm install @interchainjs/cosmos @interchainjs/auth @interchainjs/cosmos-types
2323
## 3. Updated Wallet Generation
2424

2525
In the new SDK, you can generate a wallet using our own methods rather than relying on CosmJS. For example, the unit tests use:
26-
- generateMnemonic – to create a mnemonic phrase.
2726
- Secp256k1Auth.fromMnemonic – to derive authentication objects from the mnemonic.
2827
- HDPath – to derive the correct HD paths for Cosmos.
2928

@@ -32,23 +31,24 @@ Below is a sample code snippet illustrating the updated wallet generation:
3231
// Import wallet and HD path utilities from the SDK packages
3332
import { Secp256k1Auth } from '@interchainjs/auth/secp256k1';
3433
import { HDPath } from '@interchainjs/types';
35-
// Assume generateMnemonic is exported from the SDK (or from a dedicated utility module)
36-
import { generateMnemonic } from '@interchainjs/cosmos/utils';
3734
// Import the DirectSigner from our SDK
3835
import { DirectSigner } from '@interchainjs/cosmos/signers/direct';
36+
import { Bip39, Random } from '@interchainjs/crypto';
37+
import { toEncoders } from '@interchainjs/cosmos/utils';
38+
import { MsgSend } from 'interchainjs/cosmos/bank/v1beta1/tx';
3939

4040
(async () => {
4141
// Generate a mnemonic using the SDK utility
42-
const mnemonic = generateMnemonic();
42+
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
4343

4444
// Derive authentication objects (wallet accounts) using the SDK's Secp256k1Auth
4545
// Here we derive the first account using the standard Cosmos HD path.
4646
const [auth] = Secp256k1Auth.fromMnemonic(mnemonic, [
4747
HDPath.cosmos(0, 0, 0).toString(),
4848
]);
4949

50-
// Prepare any encoders required for your message types; can be an empty array if not needed.
51-
const encoders = [];
50+
// Prepare any encoders required for your message types
51+
const encoders:Encoder[] = toEncoders(MsgSend);
5252

5353
// Define your RPC endpoint (ensure it points to a working Cosmos RPC node)
5454
const rpcEndpoint = 'http://your-rpc-endpoint:26657';
@@ -68,8 +68,8 @@ import { DirectSigner } from '@interchainjs/cosmos/signers/direct';
6868
const msg = {
6969
// Example message object; adjust fields according to your chain and message type
7070
// For instance, if using bank.MsgSend, you would populate:
71-
// typeUrl: '/cosmos.bank.v1beta1.MsgSend',
72-
// value: { fromAddress: address, toAddress: 'destination-address', amount: [{ denom: 'uatom', amount: '1000' }] }
71+
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
72+
value: { fromAddress: address, toAddress: address, amount: [{ denom: 'uatom', amount: '1' }] }
7373
};
7474

7575
// Sign and broadcast the transaction.
@@ -79,7 +79,7 @@ import { DirectSigner } from '@interchainjs/cosmos/signers/direct';
7979
})();
8080
```
8181
Key Points:
82-
- No CosmJS Dependency: The wallet is generated entirely using generateMnemonic and Secp256k1Auth.fromMnemonic.
82+
- No CosmJS Dependency: The wallet is generated entirely using Bip39 and Secp256k1Auth.fromMnemonic.
8383
- HDPath Usage: The HD path is derived using HDPath.cosmos(0, 0, 0).toString(), which follows the Cosmos standard.
8484
- DirectSigner: Instantiated with the auth object and a set of encoders (which you can populate based on your message types).
8585

@@ -124,20 +124,23 @@ If you need Amino signing for legacy compatibility, the process is analogous:
124124

125125
``` typescript
126126
import { AminoSigner } from '@interchainjs/cosmos/signers/amino';
127+
import { toEncoders, toConverters } from '@interchainjs/cosmos/utils';
128+
import { MsgSend } from 'interchainjs/cosmos/bank/v1beta1/tx';
127129

128130
(async () => {
129-
// Generate wallet using the same approach as above
130-
const mnemonic = generateMnemonic();
131131
const [auth] = Secp256k1Auth.fromMnemonic(mnemonic, [
132132
HDPath.cosmos(0, 0, 0).toString(),
133133
]);
134-
const encoders = [];
135134
const rpcEndpoint = 'http://your-rpc-endpoint:26657';
136135

137136
// Create an AminoSigner instance
138-
const aminoSigner = new AminoSigner(auth, encoders, rpcEndpoint, {
139-
prefix: 'cosmos',
140-
});
137+
const aminoSigner = new AminoSigner(
138+
auth,
139+
toEncoders(MsgSend),
140+
toConverters(MsgSend),
141+
rpcEndpoint,
142+
{ prefix: 'cosmos' }
143+
);
141144

142145
// Build your message and set fee/memo if needed
143146
const msg = {
@@ -154,9 +157,8 @@ import { AminoSigner } from '@interchainjs/cosmos/signers/amino';
154157
gas: '200000',
155158
};
156159

157-
const result = await aminoSigner.signAndBroadcast([msg], {
158-
fee,
159-
memo: 'Amino transaction test',
160+
const result = await aminoSigner.signAndBroadcast({
161+
messages: [msg], fee
160162
});
161163
console.log('Transaction hash:', result.hash);
162164
})();
@@ -183,10 +185,10 @@ import { makeCosmoshubPath } from "@cosmjs/crypto";
183185
``` typescript
184186
import { Secp256k1Auth } from '@interchainjs/auth/secp256k1';
185187
import { HDPath } from '@interchainjs/types';
186-
import { generateMnemonic } from '@interchainjs/cosmos/utils';
188+
import { Bip39, Random } from '@interchainjs/crypto';
187189

188190
(async () => {
189-
const mnemonic = generateMnemonic();
191+
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
190192
const [auth] = Secp256k1Auth.fromMnemonic(mnemonic, [
191193
HDPath.cosmos(0, 0, 0).toString(),
192194
]);
@@ -238,7 +240,9 @@ import { DirectSigner } from '@interchainjs/cosmos/signers/direct';
238240
};
239241
const memo = "InterchainJS transaction test";
240242

241-
const result = await signer.signAndBroadcast([msg], { fee, memo });
243+
const result = await signer.signAndBroadcast({
244+
messages: [msg], fee, memo
245+
});
242246
console.log("Transaction hash:", result.hash);
243247
})();
244248
```
@@ -252,13 +256,13 @@ import { DirectSigner } from '@interchainjs/cosmos/signers/direct';
252256
- Replace any CosmJS imports with the new SDK sub-module imports.
253257
- For wallet generation, use:
254258
``` typescript
255-
import { generateMnemonic } from '@interchainjs/cosmos/utils';
259+
import { Bip39, Random } from '@interchainjs/crypto';
256260
import { Secp256k1Auth } from '@interchainjs/auth/secp256k1';
257261
import { HDPath } from '@interchainjs/types';
258262
```
259263

260264
3. Wallet Generation:
261-
- Generate a mnemonic using generateMnemonic().
265+
- Generate a mnemonic using Bip39 and Random
262266
- Derive auth objects using Secp256k1Auth.fromMnemonic().
263267
- Note: This method completely avoids any dependency on CosmJS wallet implementations.
264268
4. Adapt Signer Interfaces:
@@ -273,7 +277,7 @@ import { HDPath } from '@interchainjs/types';
273277

274278
## 6. Conclusion
275279

276-
This updated migration guide demonstrates how to generate wallets and sign transactions using the new InterchainJS SDK without any dependency on CosmJS. By leveraging built-in utilities such as generateMnemonic, Secp256k1Auth.fromMnemonic, and HDPath, your application can fully transition to a modern, modular, and lightweight approach to interacting with Cosmos blockchains.
280+
This updated migration guide demonstrates how to generate wallets and sign transactions using the new InterchainJS SDK without any dependency on CosmJS. By leveraging built-in utilities such as Secp256k1Auth.fromMnemonic, and HDPath, your application can fully transition to a modern, modular, and lightweight approach to interacting with Cosmos blockchains.
277281

278282
For further details, refer to the GitHub repository README and unit tests (e.g., [token.test.ts](../networks/cosmos/starship/__tests__/token.test.ts)).
279283

0 commit comments

Comments
 (0)