Skip to content

Commit ee8cf9f

Browse files
authored
Merge pull request #1902 from cosmos/non-async
Remove unnecessary async for Secp256k1 functions (Simon's version)
2 parents fd70ac1 + 5893bed commit ee8cf9f

File tree

12 files changed

+111
-126
lines changed

12 files changed

+111
-126
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ and this project adheres to
8181

8282
in case you want to support multiple versions. ([#1866])
8383

84+
- @cosmjs/crypto: Make
85+
`Secp256k1.verifySignature`/`.createSignature`/`.makeKeypair` synchonous and
86+
let them not return a Promise.
87+
8488
[#1883]: https://github.com/cosmos/cosmjs/issues/1883
8589
[#1866]: https://github.com/cosmos/cosmjs/issues/1866
8690

packages/amino/src/secp256k1hdwallet.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ describe("Secp256k1HdWallet", () => {
255255
};
256256
const { signed, signature } = await wallet.signAmino(defaultAddress, signDoc);
257257
expect(signed).toEqual(signDoc);
258-
const valid = await Secp256k1.verifySignature(
258+
const valid = Secp256k1.verifySignature(
259259
Secp256k1Signature.fromFixedLength(fromBase64(signature.signature)),
260260
sha256(serializeSignDoc(signed)),
261261
defaultPubkey,

packages/amino/src/secp256k1hdwallet.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export class Secp256k1HdWallet implements OfflineAminoSigner {
269269
}
270270

271271
public async getAccounts(): Promise<readonly AccountData[]> {
272-
const accountsWithPrivkeys = await this.getAccountsWithPrivkeys();
272+
const accountsWithPrivkeys = this.getAccountsWithPrivkeys();
273273
return accountsWithPrivkeys.map(({ algo, pubkey, address }) => ({
274274
algo: algo,
275275
pubkey: pubkey,
@@ -278,14 +278,14 @@ export class Secp256k1HdWallet implements OfflineAminoSigner {
278278
}
279279

280280
public async signAmino(signerAddress: string, signDoc: StdSignDoc): Promise<AminoSignResponse> {
281-
const accounts = await this.getAccountsWithPrivkeys();
281+
const accounts = this.getAccountsWithPrivkeys();
282282
const account = accounts.find(({ address }) => address === signerAddress);
283283
if (account === undefined) {
284284
throw new Error(`Address ${signerAddress} not found in wallet`);
285285
}
286286
const { privkey, pubkey } = account;
287287
const message = sha256(serializeSignDoc(signDoc));
288-
const signature = await Secp256k1.createSignature(message, privkey);
288+
const signature = Secp256k1.createSignature(message, privkey);
289289
const signatureBytes = new Uint8Array([...signature.r(32), ...signature.s(32)]);
290290
return {
291291
signed: signDoc,
@@ -347,27 +347,25 @@ export class Secp256k1HdWallet implements OfflineAminoSigner {
347347
return JSON.stringify(out);
348348
}
349349

350-
private async getKeyPair(hdPath: HdPath): Promise<Secp256k1Keypair> {
350+
private getKeyPair(hdPath: HdPath): Secp256k1Keypair {
351351
const { privkey } = Slip10.derivePath(Slip10Curve.Secp256k1, this.seed, hdPath);
352-
const { pubkey } = await Secp256k1.makeKeypair(privkey);
352+
const { pubkey } = Secp256k1.makeKeypair(privkey);
353353
return {
354354
privkey: privkey,
355355
pubkey: Secp256k1.compressPubkey(pubkey),
356356
};
357357
}
358358

359-
private async getAccountsWithPrivkeys(): Promise<readonly AccountDataWithPrivkey[]> {
360-
return Promise.all(
361-
this.accounts.map(async ({ hdPath, prefix }) => {
362-
const { privkey, pubkey } = await this.getKeyPair(hdPath);
363-
const address = toBech32(prefix, rawSecp256k1PubkeyToRawAddress(pubkey));
364-
return {
365-
algo: "secp256k1" as const,
366-
privkey: privkey,
367-
pubkey: pubkey,
368-
address: address,
369-
};
370-
}),
371-
);
359+
private getAccountsWithPrivkeys(): readonly AccountDataWithPrivkey[] {
360+
return this.accounts.map(({ hdPath, prefix }) => {
361+
const { privkey, pubkey } = this.getKeyPair(hdPath);
362+
const address = toBech32(prefix, rawSecp256k1PubkeyToRawAddress(pubkey));
363+
return {
364+
algo: "secp256k1" as const,
365+
privkey: privkey,
366+
pubkey: pubkey,
367+
address: address,
368+
};
369+
});
372370
}
373371
}

packages/amino/src/secp256k1wallet.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("Secp256k1Wallet", () => {
4242
};
4343
const { signed, signature } = await signer.signAmino(defaultAddress, signDoc);
4444
expect(signed).toEqual(signDoc);
45-
const valid = await Secp256k1.verifySignature(
45+
const valid = Secp256k1.verifySignature(
4646
Secp256k1Signature.fromFixedLength(fromBase64(signature.signature)),
4747
new Sha256(serializeSignDoc(signed)).digest(),
4848
defaultPubkey,

packages/amino/src/secp256k1wallet.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class Secp256k1Wallet implements OfflineAminoSigner {
1919
* @param prefix The bech32 address prefix (human readable part). Defaults to "cosmos".
2020
*/
2121
public static async fromKey(privkey: Uint8Array, prefix = "cosmos"): Promise<Secp256k1Wallet> {
22-
const uncompressed = (await Secp256k1.makeKeypair(privkey)).pubkey;
22+
const uncompressed = Secp256k1.makeKeypair(privkey).pubkey;
2323
return new Secp256k1Wallet(privkey, Secp256k1.compressPubkey(uncompressed), prefix);
2424
}
2525

@@ -52,7 +52,7 @@ export class Secp256k1Wallet implements OfflineAminoSigner {
5252
throw new Error(`Address ${signerAddress} not found in wallet`);
5353
}
5454
const message = new Sha256(serializeSignDoc(signDoc)).digest();
55-
const signature = await Secp256k1.createSignature(message, this.privkey);
55+
const signature = Secp256k1.createSignature(message, this.privkey);
5656
const signatureBytes = new Uint8Array([...signature.r(32), ...signature.s(32)]);
5757
return {
5858
signed: signDoc,

0 commit comments

Comments
 (0)