Skip to content

Commit 4cb782b

Browse files
author
Fabrice Bascoulergue
committed
Follow cosmjs upstream and improve implementation and related unittests
1 parent c8f1ee6 commit 4cb782b

File tree

12 files changed

+200
-121
lines changed

12 files changed

+200
-121
lines changed

docs/lib/classes/lumclient.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ ___
329329

330330
### txsQuery
331331

332-
`Private`**txsQuery**(`params`: TxSearchParams): *Promise*<TxResponse[]\>
332+
`Private`**txsQuery**(`params`: TxSearchParams): *Promise*<readonly TxResponse[]\>
333333

334334
Run a tx search
335335

@@ -339,7 +339,7 @@ Name | Type | Description |
339339
:------ | :------ | :------ |
340340
`params` | TxSearchParams | Search params |
341341

342-
**Returns:** *Promise*<TxResponse[]\>
342+
**Returns:** *Promise*<readonly TxResponse[]\>
343343

344344
___
345345

docs/lib/classes/lumledgerwallet.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [getAddress](lumledgerwallet.md#getaddress)
2626
- [getAppConfiguration](lumledgerwallet.md#getappconfiguration)
2727
- [getPublicKey](lumledgerwallet.md#getpublickey)
28+
- [sign](lumledgerwallet.md#sign)
2829
- [signMessage](lumledgerwallet.md#signmessage)
2930
- [signTransaction](lumledgerwallet.md#signtransaction)
3031
- [signingMode](lumledgerwallet.md#signingmode)
@@ -128,6 +129,16 @@ Inherited from: [LumWallet](lumwallet.md)
128129

129130
___
130131

132+
### sign
133+
134+
**sign**(): *Promise*<Uint8Array\>
135+
136+
**Returns:** *Promise*<Uint8Array\>
137+
138+
Inherited from: [LumWallet](lumwallet.md)
139+
140+
___
141+
131142
### signMessage
132143

133144
**signMessage**(`msg`: *string*): *Promise*<[*SignMsg*](../interfaces/lumtypes.signmsg.md)\>

docs/lib/classes/lumpaperwallet.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [canChangeAccount](lumpaperwallet.md#canchangeaccount)
2525
- [getAddress](lumpaperwallet.md#getaddress)
2626
- [getPublicKey](lumpaperwallet.md#getpublickey)
27+
- [sign](lumpaperwallet.md#sign)
2728
- [signMessage](lumpaperwallet.md#signmessage)
2829
- [signTransaction](lumpaperwallet.md#signtransaction)
2930
- [signingMode](lumpaperwallet.md#signingmode)
@@ -67,7 +68,7 @@ ___
6768

6869
### privateKey
6970

70-
`Optional` **privateKey**: *undefined* \| *Uint8Array*
71+
`Private` `Optional` **privateKey**: *undefined* \| *Uint8Array*
7172

7273
___
7374

@@ -121,6 +122,22 @@ Inherited from: [LumWallet](lumwallet.md)
121122

122123
___
123124

125+
### sign
126+
127+
**sign**(`data`: *Uint8Array*): *Promise*<Uint8Array\>
128+
129+
#### Parameters:
130+
131+
Name | Type |
132+
:------ | :------ |
133+
`data` | *Uint8Array* |
134+
135+
**Returns:** *Promise*<Uint8Array\>
136+
137+
Inherited from: [LumWallet](lumwallet.md)
138+
139+
___
140+
124141
### signMessage
125142

126143
**signMessage**(`msg`: *string*): *Promise*<[*SignMsg*](../interfaces/lumtypes.signmsg.md)\>

docs/lib/classes/lumwallet.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [canChangeAccount](lumwallet.md#canchangeaccount)
2525
- [getAddress](lumwallet.md#getaddress)
2626
- [getPublicKey](lumwallet.md#getpublickey)
27+
- [sign](lumwallet.md#sign)
2728
- [signMessage](lumwallet.md#signmessage)
2829
- [signTransaction](lumwallet.md#signtransaction)
2930
- [signingMode](lumwallet.md#signingmode)
@@ -92,6 +93,23 @@ wallet public key (secp256k1)
9293

9394
___
9495

96+
### sign
97+
98+
`Abstract`**sign**(`data`: *Uint8Array*): *Promise*<Uint8Array\>
99+
100+
Sign a raw payload.
101+
This method might not be available for all types of wallets such as Ledger.
102+
103+
#### Parameters:
104+
105+
Name | Type | Description |
106+
:------ | :------ | :------ |
107+
`data` | *Uint8Array* | the payload to sign directly |
108+
109+
**Returns:** *Promise*<Uint8Array\>
110+
111+
___
112+
95113
### signMessage
96114

97115
`Abstract`**signMessage**(`msg`: *string*): *Promise*<[*SignMsg*](../interfaces/lumtypes.signmsg.md)\>

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040
"postdefine-proto": "prettier --write \"src/codec/**/*.ts\""
4141
},
4242
"dependencies": {
43-
"@cosmjs/crypto": "^0.24.0-alpha.26",
44-
"@cosmjs/encoding": "^0.24.0-alpha.26",
45-
"@cosmjs/math": "^0.24.0-alpha.26",
46-
"@cosmjs/proto-signing": "^0.24.0-alpha.26",
47-
"@cosmjs/stargate": "^0.24.0-alpha.26",
48-
"@cosmjs/tendermint-rpc": "^0.24.0-alpha.26",
49-
"@cosmjs/utils": "^0.24.0-alpha.26",
43+
"@cosmjs/crypto": "^v0.25.2",
44+
"@cosmjs/encoding": "^0.25.2",
45+
"@cosmjs/math": "^0.25.2",
46+
"@cosmjs/proto-signing": "^0.25.2",
47+
"@cosmjs/stargate": "^0.25.2",
48+
"@cosmjs/tendermint-rpc": "^0.25.2",
49+
"@cosmjs/utils": "^0.25.2",
5050
"@ledgerhq/hw-app-cosmos": "^5.46.0",
5151
"@ledgerhq/hw-transport": "^5.46.0",
5252
"@types/crypto-js": "^4.0.1",

src/client/LumClient.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import {
66
setupBankExtension as StargateSetupBankExtension,
77
setupDistributionExtension as StargateDistributionExtension,
88
setupStakingExtension as StargateStakingExtension,
9-
coinFromProto,
109
AuthExtension,
1110
BankExtension,
1211
StakingExtension,
1312
DistributionExtension,
1413
} from '@cosmjs/stargate';
1514

16-
import { LumWallet, LumUtils, LumTypes } from '..';
15+
import { BaseAccount } from '../codec/cosmos/auth/v1beta1/auth';
16+
import { LumWallet, LumUtils, LumTypes, LumRegistry } from '..';
1717

1818
export class LumClient {
1919
readonly tmClient: Tendermint34Client;
@@ -110,10 +110,11 @@ export class LumClient {
110110
* @param address wallet address
111111
*/
112112
getAccount = async (address: string): Promise<LumTypes.Account | null> => {
113-
const account = await this.queryClient.auth.account(address);
114-
if (!account) {
113+
const anyAccount = await this.queryClient.auth.verified.account(address);
114+
if (!anyAccount) {
115115
return null;
116116
}
117+
const account = LumRegistry.decode(anyAccount) as BaseAccount;
117118
return {
118119
address: account.address,
119120
accountNumber: Uint64.fromString(account.accountNumber.toString()).toNumber(),
@@ -127,10 +128,11 @@ export class LumClient {
127128
* @param address wallet address
128129
*/
129130
getAccountUnverified = async (address: string): Promise<LumTypes.Account | null> => {
130-
const account = await this.queryClient.auth.unverified.account(address);
131-
if (!account) {
131+
const anyAccount = await this.queryClient.auth.account(address);
132+
if (!anyAccount) {
132133
return null;
133134
}
135+
const account = LumRegistry.decode(anyAccount) as BaseAccount;
134136
return {
135137
address: account.address,
136138
accountNumber: Uint64.fromString(account.accountNumber.toString()).toNumber(),
@@ -145,8 +147,8 @@ export class LumClient {
145147
* @param searchDenom Coin denomination (ex: lum)
146148
*/
147149
getBalance = async (address: string, searchDenom: string): Promise<LumTypes.Coin | null> => {
148-
const balance = await this.queryClient.bank.balance(address, searchDenom);
149-
return balance ? coinFromProto(balance) : null;
150+
const balance = await this.queryClient.bank.verified.balance(address, searchDenom);
151+
return balance ? balance : null;
150152
};
151153

152154
/**
@@ -156,8 +158,8 @@ export class LumClient {
156158
* @param searchDenom Coin denomination (ex: lum)
157159
*/
158160
getBalanceUnverified = async (address: string, searchDenom: string): Promise<LumTypes.Coin | null> => {
159-
const balance = await this.queryClient.bank.unverified.balance(address, searchDenom);
160-
return balance ? coinFromProto(balance) : null;
161+
const balance = await this.queryClient.bank.balance(address, searchDenom);
162+
return balance ? balance : null;
161163
};
162164

163165
/**
@@ -166,8 +168,8 @@ export class LumClient {
166168
* @param address wallet address
167169
*/
168170
getAllBalancesUnverified = async (address: string): Promise<LumTypes.Coin[]> => {
169-
const balances = await this.queryClient.bank.unverified.allBalances(address);
170-
return balances.map(coinFromProto);
171+
const balances = await this.queryClient.bank.allBalances(address);
172+
return balances;
171173
};
172174

173175
/**
@@ -176,16 +178,16 @@ export class LumClient {
176178
* @param searchDenom Coin denomination (ex: lum)
177179
*/
178180
getSupply = async (searchDenom: string): Promise<LumTypes.Coin | null> => {
179-
const supply = await this.queryClient.bank.unverified.supplyOf(searchDenom);
180-
return supply ? coinFromProto(supply) : null;
181+
const supply = await this.queryClient.bank.supplyOf(searchDenom);
182+
return supply ? supply : null;
181183
};
182184

183185
/**
184186
* Get all coins supplies
185187
*/
186188
getAllSupplies = async (): Promise<LumTypes.Coin[]> => {
187-
const supplies = await this.queryClient.bank.unverified.totalSupply();
188-
return supplies.map(coinFromProto);
189+
const supplies = await this.queryClient.bank.totalSupply();
190+
return supplies;
189191
};
190192

191193
/**
@@ -234,9 +236,9 @@ export class LumClient {
234236
*
235237
* @param params Search params
236238
*/
237-
private txsQuery = async (params: LumTypes.TxSearchParams): Promise<LumTypes.TxResponse[]> => {
239+
private txsQuery = async (params: LumTypes.TxSearchParams): Promise<readonly LumTypes.TxResponse[]> => {
238240
const results = await this.tmClient.txSearch(params);
239-
return results.txs as LumTypes.TxResponse[];
241+
return results.txs;
240242
};
241243

242244
/**

src/registry/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { AminoTypes } from '@cosmjs/stargate';
33

44
import { Tx } from '../codec/cosmos/tx/v1beta1/tx';
55
import { PubKey } from '../codec/cosmos/crypto/secp256k1/keys';
6+
import { BaseAccount } from '../codec/cosmos/auth/v1beta1/auth';
67
import { MsgSend, MsgMultiSend } from '../codec/cosmos/bank/v1beta1/tx';
78
import { MsgFundCommunityPool, MsgSetWithdrawAddress, MsgWithdrawDelegatorReward, MsgWithdrawValidatorCommission } from '../codec/cosmos/distribution/v1beta1/tx';
89
import { MsgBeginRedelegate, MsgCreateValidator, MsgDelegate, MsgEditValidator, MsgUndelegate } from '../codec/cosmos/staking/v1beta1/tx';
910
import { MsgCancelBeam, MsgClaimBeam, MsgOpenBeam, MsgUpdateBeam } from '../codec/chain/beam/beam';
1011

1112
const registryTypes: Iterable<[string, GeneratedType]> = [
1213
['/cosmos.crypto.ed25519.PubKey', PubKey as GeneratedType],
14+
['/cosmos.auth.v1beta1.BaseAccount', BaseAccount as GeneratedType],
1315
['/cosmos.bank.v1beta1.MsgSend', MsgSend as GeneratedType],
1416
['/cosmos.bank.v1beta1.MsgMultiSend', MsgMultiSend as GeneratedType],
1517
['/cosmos.distribution.v1beta1.MsgFundCommunityPool', MsgFundCommunityPool as GeneratedType],

src/wallet/LumLedgerWallet.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export class LumLedgerWallet extends LumWallet {
4343
return true;
4444
};
4545

46+
sign = async (): Promise<Uint8Array> => {
47+
throw new Error('Feature not supported.');
48+
};
49+
4650
signTransaction = async (doc: LumTypes.Doc): Promise<Uint8Array> => {
4751
if (!this.hdPath) {
4852
throw new Error('No account selected.');

src/wallet/LumPaperWallet.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { LumWallet } from '.';
44

55
export class LumPaperWallet extends LumWallet {
66
private readonly mnemonic?: string;
7-
public privateKey?: Uint8Array;
7+
private privateKey?: Uint8Array;
88

99
/**
1010
* Create a LumPaperWallet instance based on a mnemonic or a private key
@@ -44,6 +44,14 @@ export class LumPaperWallet extends LumWallet {
4444
throw new Error('No available mnemonic or private key.');
4545
};
4646

47+
sign = async (data: Uint8Array): Promise<Uint8Array> => {
48+
if (!this.privateKey || !this.publicKey) {
49+
throw new Error('No account selected.');
50+
}
51+
const signature = await LumUtils.generateSignature(data, this.privateKey);
52+
return signature;
53+
};
54+
4755
signTransaction = async (doc: LumTypes.Doc): Promise<Uint8Array> => {
4856
if (!this.privateKey || !this.publicKey) {
4957
throw new Error('No account selected.');

src/wallet/LumWallet.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ export abstract class LumWallet {
5151
*/
5252
abstract useAccount(hdPath: string, addressPrefix: string): Promise<boolean>;
5353

54+
/**
55+
* Sign a raw payload.
56+
* This method might not be available for all types of wallets such as Ledger.
57+
*
58+
* @param data the payload to sign directly
59+
*/
60+
abstract sign(data: Uint8Array): Promise<Uint8Array>;
61+
5462
/**
5563
* Sign a transaction document using a LumWallet
5664
*

0 commit comments

Comments
 (0)