From 53d7d34b8adad70446b951683b6c60f812322224 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 13 Oct 2020 11:20:27 +0200 Subject: [PATCH] Use convenience hash functions in codebase --- packages/cli/src/cli.ts | 4 +++- packages/cosmwasm/src/cosmwasmclient.spec.ts | 4 ++-- packages/cosmwasm/src/cosmwasmclient.ts | 4 ++-- packages/cosmwasm/src/lcdapi/wasm.spec.ts | 6 +++--- packages/cosmwasm/src/signingcosmwasmclient.spec.ts | 4 ++-- packages/cosmwasm/src/signingcosmwasmclient.ts | 6 +++--- packages/crypto/src/englishmnemonic.spec.ts | 4 ++-- packages/crypto/src/secp256k1.spec.ts | 6 +++--- packages/launchpad-ledger/src/ledgersigner.spec.ts | 4 ++-- packages/launchpad/src/address.ts | 10 +++++----- packages/launchpad/src/cosmosclient.ts | 4 ++-- packages/launchpad/src/secp256k1hdwallet.spec.ts | 4 ++-- packages/launchpad/src/secp256k1hdwallet.ts | 4 ++-- packages/launchpad/src/sequence.ts | 4 ++-- .../proto-signing/src/directsecp256k1wallet.spec.ts | 4 ++-- packages/proto-signing/src/directsecp256k1wallet.ts | 4 ++-- 16 files changed, 39 insertions(+), 37 deletions(-) diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index 50ff5f71..47cbac29 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -75,7 +75,9 @@ export async function main(originalArgs: readonly string[]): Promise { "Random", "Secp256k1", "Sha256", + "sha256", "Sha512", + "sha512", "Slip10", "Slip10Curve", "Slip10RawIndex", @@ -148,7 +150,7 @@ export async function main(originalArgs: readonly string[]): Promise { const readmeContent = fs.readFileSync(process.cwd() + "/README.md"); fs.writeFileSync(process.cwd() + "/README.md", readmeContent); - const hash = new Sha512(new Uint8Array([])).digest(); + const hash = sha512(new Uint8Array([])); const hexHash = toHex(hash); export class NewDummyClass {}; diff --git a/packages/cosmwasm/src/cosmwasmclient.spec.ts b/packages/cosmwasm/src/cosmwasmclient.spec.ts index e09cbacd..ea3e4cb7 100644 --- a/packages/cosmwasm/src/cosmwasmclient.spec.ts +++ b/packages/cosmwasm/src/cosmwasmclient.spec.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { Sha256 } from "@cosmjs/crypto"; +import { sha256 } from "@cosmjs/crypto"; import { Bech32, fromHex, fromUtf8, toAscii, toBase64 } from "@cosmjs/encoding"; import { assertIsBroadcastTxSuccess, @@ -285,7 +285,7 @@ describe("CosmWasmClient", () => { // check info expect(result).toEqual(jasmine.objectContaining(expectedInfo)); // check data - expect(new Sha256(result.data).digest()).toEqual(fromHex(expectedInfo.checksum)); + expect(sha256(result.data)).toEqual(fromHex(expectedInfo.checksum)); }); it("caches downloads", async () => { diff --git a/packages/cosmwasm/src/cosmwasmclient.ts b/packages/cosmwasm/src/cosmwasmclient.ts index f2b26ab0..f053f801 100644 --- a/packages/cosmwasm/src/cosmwasmclient.ts +++ b/packages/cosmwasm/src/cosmwasmclient.ts @@ -1,4 +1,4 @@ -import { Sha256 } from "@cosmjs/crypto"; +import { sha256 } from "@cosmjs/crypto"; import { fromBase64, fromHex, toHex } from "@cosmjs/encoding"; import { AuthExtension, @@ -202,7 +202,7 @@ export class CosmWasmClient { public async getIdentifier(tx: WrappedStdTx): Promise { // We consult the REST API because we don't have a local amino encoder const response = await this.lcdClient.encodeTx(tx); - const hash = new Sha256(fromBase64(response.tx)).digest(); + const hash = sha256(fromBase64(response.tx)); return toHex(hash).toUpperCase(); } diff --git a/packages/cosmwasm/src/lcdapi/wasm.spec.ts b/packages/cosmwasm/src/lcdapi/wasm.spec.ts index 385ea78a..cd34a1a6 100644 --- a/packages/cosmwasm/src/lcdapi/wasm.spec.ts +++ b/packages/cosmwasm/src/lcdapi/wasm.spec.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { Sha256 } from "@cosmjs/crypto"; +import { sha256 } from "@cosmjs/crypto"; import { Bech32, fromAscii, fromHex, fromUtf8, toAscii, toBase64, toHex } from "@cosmjs/encoding"; import { assertIsBroadcastTxSuccess, @@ -165,7 +165,7 @@ describe("WasmExtension", () => { expect(lastCode.creator).toEqual(alice.address0); expect(lastCode.source).toEqual(hackatom.source); expect(lastCode.builder).toEqual(hackatom.builder); - expect(lastCode.data_hash.toLowerCase()).toEqual(toHex(new Sha256(hackatom.data).digest())); + expect(lastCode.data_hash.toLowerCase()).toEqual(toHex(sha256(hackatom.data))); }); }); @@ -179,7 +179,7 @@ describe("WasmExtension", () => { expect(code.creator).toEqual(alice.address0); expect(code.source).toEqual(hackatom.source); expect(code.builder).toEqual(hackatom.builder); - expect(code.data_hash.toLowerCase()).toEqual(toHex(new Sha256(hackatom.data).digest())); + expect(code.data_hash.toLowerCase()).toEqual(toHex(sha256(hackatom.data))); expect(code.data).toEqual(toBase64(hackatom.data)); }); }); diff --git a/packages/cosmwasm/src/signingcosmwasmclient.spec.ts b/packages/cosmwasm/src/signingcosmwasmclient.spec.ts index cfb5ad6a..84bc05fc 100644 --- a/packages/cosmwasm/src/signingcosmwasmclient.spec.ts +++ b/packages/cosmwasm/src/signingcosmwasmclient.spec.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { Sha256 } from "@cosmjs/crypto"; +import { sha256 } from "@cosmjs/crypto"; import { toHex } from "@cosmjs/encoding"; import { assertIsBroadcastTxSuccess, @@ -259,7 +259,7 @@ describe("SigningCosmWasmClient", () => { compressedChecksum, compressedSize, } = await client.upload(wasm); - expect(originalChecksum).toEqual(toHex(new Sha256(wasm).digest())); + expect(originalChecksum).toEqual(toHex(sha256(wasm))); expect(originalSize).toEqual(wasm.length); expect(compressedChecksum).toMatch(/^[0-9a-f]{64}$/); expect(compressedSize).toBeLessThan(wasm.length * 0.5); diff --git a/packages/cosmwasm/src/signingcosmwasmclient.ts b/packages/cosmwasm/src/signingcosmwasmclient.ts index 6c845eb3..834112fc 100644 --- a/packages/cosmwasm/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm/src/signingcosmwasmclient.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { Sha256 } from "@cosmjs/crypto"; +import { sha256 } from "@cosmjs/crypto"; import { toBase64, toHex } from "@cosmjs/encoding"; import { BroadcastMode, @@ -212,9 +212,9 @@ export class SigningCosmWasmClient extends CosmWasmClient { const codeIdAttr = findAttribute(result.logs, "message", "code_id"); return { originalSize: wasmCode.length, - originalChecksum: toHex(new Sha256(wasmCode).digest()), + originalChecksum: toHex(sha256(wasmCode)), compressedSize: compressed.length, - compressedChecksum: toHex(new Sha256(compressed).digest()), + compressedChecksum: toHex(sha256(compressed)), codeId: Number.parseInt(codeIdAttr.value, 10), logs: result.logs, transactionHash: result.transactionHash, diff --git a/packages/crypto/src/englishmnemonic.spec.ts b/packages/crypto/src/englishmnemonic.spec.ts index e365c0b9..ec377d1b 100644 --- a/packages/crypto/src/englishmnemonic.spec.ts +++ b/packages/crypto/src/englishmnemonic.spec.ts @@ -1,7 +1,7 @@ import { fromAscii, fromBase64, fromHex } from "@cosmjs/encoding"; import { EnglishMnemonic } from "./englishmnemonic"; -import { Sha256 } from "./sha"; +import { sha256 } from "./sha"; import wordlists from "./testdata/bip39_wordlists.json"; describe("EnglishMnemonic", () => { @@ -11,7 +11,7 @@ describe("EnglishMnemonic", () => { const bip39EnglishTxt = fromBase64(wordlists.english); // Ensure we loaded the correct english.txt from https://github.com/bitcoin/bips/tree/master/bip-0039 - const checksum = new Sha256(bip39EnglishTxt).digest(); + const checksum = sha256(bip39EnglishTxt); expect(checksum).toEqual(fromHex("2f5eed53a4727b4bf8880d8f3f199efc90e58503646d9ff8eff3a2ed3b24dbda")); const wordsFromSpec: string[] = []; diff --git a/packages/crypto/src/secp256k1.spec.ts b/packages/crypto/src/secp256k1.spec.ts index 84586cb8..0e90d4a2 100644 --- a/packages/crypto/src/secp256k1.spec.ts +++ b/packages/crypto/src/secp256k1.spec.ts @@ -3,7 +3,7 @@ import { fromHex } from "@cosmjs/encoding"; import { Secp256k1 } from "./secp256k1"; import { ExtendedSecp256k1Signature, Secp256k1Signature } from "./secp256k1signature"; -import { Sha256 } from "./sha"; +import { sha256 } from "./sha"; describe("Secp256k1", () => { // How to generate Secp256k1 test vectors: @@ -383,7 +383,7 @@ describe("Secp256k1", () => { for (const [index, row] of data.entries()) { const pubkey = (await Secp256k1.makeKeypair(row.privkey)).pubkey; - const messageHash = new Sha256(row.message).digest(); + const messageHash = sha256(row.message); const isValid = await Secp256k1.verifySignature( Secp256k1Signature.fromDer(row.signature), messageHash, @@ -494,7 +494,7 @@ describe("Secp256k1", () => { for (const [index, row] of data.entries()) { const keypair = await Secp256k1.makeKeypair(row.privkey); - const messageHash = new Sha256(row.message).digest(); + const messageHash = sha256(row.message); // create signature const calculatedSignature = await Secp256k1.createSignature(messageHash, row.privkey); diff --git a/packages/launchpad-ledger/src/ledgersigner.spec.ts b/packages/launchpad-ledger/src/ledgersigner.spec.ts index a1e3a995..ff2851ad 100644 --- a/packages/launchpad-ledger/src/ledgersigner.spec.ts +++ b/packages/launchpad-ledger/src/ledgersigner.spec.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { Secp256k1, Secp256k1Signature, Sha256 } from "@cosmjs/crypto"; +import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto"; import { fromBase64 } from "@cosmjs/encoding"; import { coins, @@ -124,7 +124,7 @@ describe("LedgerSigner", () => { expect(signed).toEqual(signDoc); const valid = await Secp256k1.verifySignature( Secp256k1Signature.fromFixedLength(fromBase64(signature.signature)), - new Sha256(serializeSignDoc(signed)).digest(), + sha256(serializeSignDoc(signed)), fistAccount.pubkey, ); expect(valid).toEqual(true); diff --git a/packages/launchpad/src/address.ts b/packages/launchpad/src/address.ts index 5f48efa0..87fede26 100644 --- a/packages/launchpad/src/address.ts +++ b/packages/launchpad/src/address.ts @@ -1,4 +1,4 @@ -import { Ripemd160, Sha256 } from "@cosmjs/crypto"; +import { ripemd160, sha256 } from "@cosmjs/crypto"; import { Bech32, fromBase64 } from "@cosmjs/encoding"; import { PubKey, pubkeyType } from "./types"; @@ -7,8 +7,8 @@ export function rawSecp256k1PubkeyToAddress(pubkeyRaw: Uint8Array, prefix: strin if (pubkeyRaw.length !== 33) { throw new Error(`Invalid Secp256k1 pubkey length (compressed): ${pubkeyRaw.length}`); } - const hash1 = new Sha256(pubkeyRaw).digest(); - const hash2 = new Ripemd160(hash1).digest(); + const hash1 = sha256(pubkeyRaw); + const hash2 = ripemd160(hash1); return Bech32.encode(prefix, hash2); } @@ -24,14 +24,14 @@ export function pubkeyToAddress(pubkey: PubKey, prefix: string): string { if (pubkeyBytes.length !== 32) { throw new Error(`Invalid Ed25519 pubkey length: ${pubkeyBytes.length}`); } - const hash = new Sha256(pubkeyBytes).digest(); + const hash = sha256(pubkeyBytes); return Bech32.encode(prefix, hash.slice(0, 20)); } case pubkeyType.sr25519: { if (pubkeyBytes.length !== 32) { throw new Error(`Invalid Sr25519 pubkey length: ${pubkeyBytes.length}`); } - const hash = new Sha256(pubkeyBytes).digest(); + const hash = sha256(pubkeyBytes); return Bech32.encode(prefix, hash.slice(0, 20)); } default: diff --git a/packages/launchpad/src/cosmosclient.ts b/packages/launchpad/src/cosmosclient.ts index c59ec73f..8a087332 100644 --- a/packages/launchpad/src/cosmosclient.ts +++ b/packages/launchpad/src/cosmosclient.ts @@ -1,4 +1,4 @@ -import { Sha256 } from "@cosmjs/crypto"; +import { sha256 } from "@cosmjs/crypto"; import { fromBase64, fromHex, toHex } from "@cosmjs/encoding"; import { Uint53 } from "@cosmjs/math"; @@ -207,7 +207,7 @@ export class CosmosClient { public async getIdentifier(tx: WrappedStdTx): Promise { // We consult the REST API because we don't have a local amino encoder const response = await this.lcdClient.encodeTx(tx); - const hash = new Sha256(fromBase64(response.tx)).digest(); + const hash = sha256(fromBase64(response.tx)); return toHex(hash).toUpperCase(); } diff --git a/packages/launchpad/src/secp256k1hdwallet.spec.ts b/packages/launchpad/src/secp256k1hdwallet.spec.ts index f8cb95ea..f053accc 100644 --- a/packages/launchpad/src/secp256k1hdwallet.spec.ts +++ b/packages/launchpad/src/secp256k1hdwallet.spec.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { Secp256k1, Secp256k1Signature, Sha256 } from "@cosmjs/crypto"; +import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto"; import { fromBase64, fromHex } from "@cosmjs/encoding"; import { serializeSignDoc, StdSignDoc } from "./encoding"; @@ -124,7 +124,7 @@ describe("Secp256k1HdWallet", () => { expect(signed).toEqual(signDoc); const valid = await Secp256k1.verifySignature( Secp256k1Signature.fromFixedLength(fromBase64(signature.signature)), - new Sha256(serializeSignDoc(signed)).digest(), + sha256(serializeSignDoc(signed)), defaultPubkey, ); expect(valid).toEqual(true); diff --git a/packages/launchpad/src/secp256k1hdwallet.ts b/packages/launchpad/src/secp256k1hdwallet.ts index 92b89194..32e44569 100644 --- a/packages/launchpad/src/secp256k1hdwallet.ts +++ b/packages/launchpad/src/secp256k1hdwallet.ts @@ -5,7 +5,7 @@ import { pathToString, Random, Secp256k1, - Sha256, + sha256, Slip10, Slip10Curve, stringToPath, @@ -263,7 +263,7 @@ export class Secp256k1HdWallet implements OfflineSigner { if (signerAddress !== this.address) { throw new Error(`Address ${signerAddress} not found in wallet`); } - const message = new Sha256(serializeSignDoc(signDoc)).digest(); + const message = sha256(serializeSignDoc(signDoc)); const signature = await Secp256k1.createSignature(message, this.privkey); const signatureBytes = new Uint8Array([...signature.r(32), ...signature.s(32)]); return { diff --git a/packages/launchpad/src/sequence.ts b/packages/launchpad/src/sequence.ts index a11c1eb2..3ed14221 100644 --- a/packages/launchpad/src/sequence.ts +++ b/packages/launchpad/src/sequence.ts @@ -1,4 +1,4 @@ -import { Secp256k1, Secp256k1Signature, Sha256 } from "@cosmjs/crypto"; +import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto"; import { makeSignDoc, serializeSignDoc } from "./encoding"; import { decodeSignature } from "./signature"; @@ -33,7 +33,7 @@ export async function findSequenceForSignedTx( const signBytes = serializeSignDoc( makeSignDoc(tx.value.msg, tx.value.fee, chainId, tx.value.memo || "", accountNumber, s), ); - const prehashed = new Sha256(signBytes).digest(); + const prehashed = sha256(signBytes); const valid = await Secp256k1.verifySignature(secp256keSignature, prehashed, pubkey); if (valid) return s; } diff --git a/packages/proto-signing/src/directsecp256k1wallet.spec.ts b/packages/proto-signing/src/directsecp256k1wallet.spec.ts index 6f0fbcac..56eae17b 100644 --- a/packages/proto-signing/src/directsecp256k1wallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1wallet.spec.ts @@ -1,4 +1,4 @@ -import { Secp256k1, Secp256k1Signature, Sha256 } from "@cosmjs/crypto"; +import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto"; import { fromBase64, fromHex, toAscii } from "@cosmjs/encoding"; import { DirectSecp256k1Wallet } from "./directsecp256k1wallet"; @@ -61,7 +61,7 @@ describe("DirectSecp256k1Wallet", () => { const signature = await wallet.sign(defaultAddress, message); const valid = await Secp256k1.verifySignature( Secp256k1Signature.fromFixedLength(fromBase64(signature.signature)), - new Sha256(message).digest(), + sha256(message), defaultPubkey, ); expect(valid).toEqual(true); diff --git a/packages/proto-signing/src/directsecp256k1wallet.ts b/packages/proto-signing/src/directsecp256k1wallet.ts index 921b25ec..4cca17dc 100644 --- a/packages/proto-signing/src/directsecp256k1wallet.ts +++ b/packages/proto-signing/src/directsecp256k1wallet.ts @@ -4,7 +4,7 @@ import { HdPath, Random, Secp256k1, - Sha256, + sha256, Slip10, Slip10Curve, } from "@cosmjs/crypto"; @@ -117,7 +117,7 @@ export class DirectSecp256k1Wallet { if (address !== this.address) { throw new Error(`Address ${address} not found in wallet`); } - const hashedMessage = new Sha256(message).digest(); + const hashedMessage = sha256(message); const signature = await Secp256k1.createSignature(hashedMessage, this.privkey); const signatureBytes = new Uint8Array([...signature.r(32), ...signature.s(32)]); return encodeSecp256k1Signature(this.pubkey, signatureBytes);