Use convenience hash functions in codebase

This commit is contained in:
willclarktech
2020-10-13 13:36:44 +02:00
parent 8e43dfb82a
commit 53d7d34b8a
16 changed files with 39 additions and 37 deletions
+3 -1
View File
@@ -75,7 +75,9 @@ export async function main(originalArgs: readonly string[]): Promise<void> {
"Random",
"Secp256k1",
"Sha256",
"sha256",
"Sha512",
"sha512",
"Slip10",
"Slip10Curve",
"Slip10RawIndex",
@@ -148,7 +150,7 @@ export async function main(originalArgs: readonly string[]): Promise<void> {
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 {};
+2 -2
View File
@@ -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 () => {
+2 -2
View File
@@ -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<string> {
// 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();
}
+3 -3
View File
@@ -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));
});
});
@@ -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);
@@ -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,
+2 -2
View File
@@ -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[] = [];
+3 -3
View File
@@ -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);
@@ -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);
+5 -5
View File
@@ -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:
+2 -2
View File
@@ -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<string> {
// 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();
}
@@ -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);
+2 -2
View File
@@ -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 {
+2 -2
View File
@@ -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;
}
@@ -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);
@@ -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);