Skip to content

Commit 5412f75

Browse files
committed
Use Uint8Array<ArrayBuffer> whenever possible and add fixUint8Array
1 parent 97d91e1 commit 5412f75

File tree

29 files changed

+105
-60
lines changed

29 files changed

+105
-60
lines changed

packages/amino/src/wallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { sleep } from "@cosmjs/utils";
1313
* This reduces the scope of a potential rainbow attack to all CosmJS users.
1414
* Must be 16 bytes due to implementation limitations.
1515
*/
16-
export const cosmjsSalt: Uint8Array = toAscii("The CosmJS salt.");
16+
export const cosmjsSalt: Uint8Array<ArrayBuffer> = toAscii("The CosmJS salt.");
1717

1818
export interface KdfConfiguration {
1919
/**

packages/cosmwasm-stargate/src/cosmwasmclient.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fromUtf8, toHex } from "@cosmjs/encoding";
1+
import { fixUint8Array, fromUtf8, toHex } from "@cosmjs/encoding";
22
import { Uint53 } from "@cosmjs/math";
33
import {
44
Account,
@@ -496,7 +496,10 @@ export class CosmWasmClient {
496496
events: tx.result.events.map(fromTendermintEvent),
497497
rawLog: tx.result.log || "",
498498
tx: tx.tx,
499-
msgResponses: txMsgData.msgResponses,
499+
msgResponses: txMsgData.msgResponses.map((mr) => ({
500+
typeUrl: mr.typeUrl,
501+
value: fixUint8Array(mr.value),
502+
})),
500503
gasUsed: tx.result.gasUsed,
501504
gasWanted: tx.result.gasWanted,
502505
};

packages/crypto/src/bip39.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fixUint8Array } from "@cosmjs/encoding";
12
import { entropyToMnemonic, mnemonicToEntropy, mnemonicToSeed } from "@scure/bip39";
23
import { wordlist } from "@scure/bip39/wordlists/english";
34

@@ -39,7 +40,7 @@ export class Bip39 {
3940
}
4041

4142
public static decode(mnemonic: EnglishMnemonic): Uint8Array<ArrayBuffer> {
42-
return mnemonicToEntropy(mnemonic.toString(), wordlist);
43+
return fixUint8Array(mnemonicToEntropy(mnemonic.toString(), wordlist));
4344
}
4445

4546
public static async mnemonicToSeed(mnemonic: EnglishMnemonic, password?: string): Promise<Uint8Array> {

packages/crypto/src/hmac.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { fixUint8Array } from "@cosmjs/encoding";
2+
13
import { HashFunction } from "./hash";
24

35
export class Hmac<H extends HashFunction> implements HashFunction {
@@ -6,7 +8,7 @@ export class Hmac<H extends HashFunction> implements HashFunction {
68
private readonly messageHasher: H;
79
private readonly oKeyPad: Uint8Array;
810
private readonly iKeyPad: Uint8Array;
9-
private readonly hash: (data: Uint8Array) => Uint8Array;
11+
private readonly hash: (data: Uint8Array) => Uint8Array<ArrayBuffer>;
1012

1113
public constructor(hashFunctionConstructor: new () => H, originalKey: Uint8Array) {
1214
// This implementation is based on https://en.wikipedia.org/wiki/HMAC#Implementation
@@ -15,7 +17,7 @@ export class Hmac<H extends HashFunction> implements HashFunction {
1517

1618
const blockSize = new hashFunctionConstructor().blockSize;
1719

18-
this.hash = (data) => new hashFunctionConstructor().update(data).digest();
20+
this.hash = (data) => fixUint8Array(new hashFunctionConstructor().update(data).digest());
1921

2022
let key = originalKey;
2123
if (key.length > blockSize) {

packages/crypto/src/keccak.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fixUint8Array } from "@cosmjs/encoding";
12
import { keccak_256 } from "@noble/hashes/sha3.js";
23

34
import { HashFunction } from "./hash";
@@ -20,7 +21,7 @@ export class Keccak256 implements HashFunction {
2021
}
2122

2223
public digest(): Uint8Array<ArrayBuffer> {
23-
return this.impl.digest();
24+
return fixUint8Array(this.impl.digest());
2425
}
2526
}
2627

packages/crypto/src/ripemd.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fixUint8Array } from "@cosmjs/encoding";
12
import { ripemd160 as nobleRipemd160 } from "@noble/hashes/legacy.js";
23

34
import { HashFunction } from "./hash";
@@ -20,7 +21,7 @@ export class Ripemd160 implements HashFunction {
2021
}
2122

2223
public digest(): Uint8Array<ArrayBuffer> {
23-
return this.impl.digest();
24+
return fixUint8Array(this.impl.digest());
2425
}
2526
}
2627

packages/crypto/src/secp256k1.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fromHex, toHex } from "@cosmjs/encoding";
1+
import { fixUint8Array, fromHex, toHex } from "@cosmjs/encoding";
22
import { assert } from "@cosmjs/utils";
33
import { secp256k1 } from "@noble/curves/secp256k1";
44

@@ -106,7 +106,7 @@ export class Secp256k1 {
106106
bytesToUnsignedBigInt(signature.s()),
107107
signature.recovery,
108108
).recoverPublicKey(messageHash);
109-
return pk.toBytes(false);
109+
return fixUint8Array(pk.toBytes(false));
110110
}
111111

112112
/**
@@ -117,9 +117,9 @@ export class Secp256k1 {
117117
public static compressPubkey(pubkey: Uint8Array): Uint8Array<ArrayBuffer> {
118118
switch (pubkey.length) {
119119
case 33:
120-
return pubkey;
120+
return fixUint8Array(pubkey);
121121
case 65:
122-
return secp256k1.Point.fromBytes(pubkey).toBytes(true);
122+
return fixUint8Array(secp256k1.Point.fromBytes(pubkey).toBytes(true));
123123
default:
124124
throw new Error("Invalid pubkey length");
125125
}
@@ -133,9 +133,9 @@ export class Secp256k1 {
133133
public static uncompressPubkey(pubkey: Uint8Array): Uint8Array<ArrayBuffer> {
134134
switch (pubkey.length) {
135135
case 33:
136-
return secp256k1.Point.fromBytes(pubkey).toBytes(false);
136+
return fixUint8Array(secp256k1.Point.fromBytes(pubkey).toBytes(false));
137137
case 65:
138-
return pubkey;
138+
return fixUint8Array(pubkey);
139139
default:
140140
throw new Error("Invalid pubkey length");
141141
}
@@ -144,7 +144,7 @@ export class Secp256k1 {
144144
public static trimRecoveryByte(signature: Uint8Array): Uint8Array<ArrayBuffer> {
145145
switch (signature.length) {
146146
case 64:
147-
return signature;
147+
return fixUint8Array(signature);
148148
case 65:
149149
return signature.slice(0, 64);
150150
default:

packages/crypto/src/secp256k1signature.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { fixUint8Array } from "@cosmjs/encoding";
2+
13
function trimLeadingNullBytes(inData: Uint8Array): Uint8Array<ArrayBuffer> {
24
let numberOfLeadingNullBytes = 0;
35
for (const byte of inData) {
@@ -74,8 +76,8 @@ export class Secp256k1Signature {
7476
}
7577

7678
private readonly data: {
77-
readonly r: Uint8Array;
78-
readonly s: Uint8Array;
79+
readonly r: Uint8Array<ArrayBuffer>;
80+
readonly s: Uint8Array<ArrayBuffer>;
7981
};
8082

8183
public constructor(r: Uint8Array, s: Uint8Array) {
@@ -88,8 +90,8 @@ export class Secp256k1Signature {
8890
}
8991

9092
this.data = {
91-
r: r,
92-
s: s,
93+
r: fixUint8Array(r),
94+
s: fixUint8Array(s),
9395
};
9496
}
9597

packages/crypto/src/sha.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fixUint8Array } from "@cosmjs/encoding";
12
import { sha256 as nobleSha256, sha512 as nobleSha512 } from "@noble/hashes/sha2.js";
23

34
import { HashFunction } from "./hash";
@@ -20,7 +21,7 @@ export class Sha256 implements HashFunction {
2021
}
2122

2223
public digest(): Uint8Array<ArrayBuffer> {
23-
return this.impl.digest();
24+
return fixUint8Array(this.impl.digest());
2425
}
2526
}
2627

@@ -46,7 +47,7 @@ export class Sha512 implements HashFunction {
4647
}
4748

4849
public digest(): Uint8Array<ArrayBuffer> {
49-
return this.impl.digest();
50+
return fixUint8Array(this.impl.digest());
5051
}
5152
}
5253

packages/crypto/src/slip10.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fromHex, toAscii, toHex } from "@cosmjs/encoding";
1+
import { fixUint8Array, fromHex, toAscii, toHex } from "@cosmjs/encoding";
22
import { Uint32, Uint53 } from "@cosmjs/math";
33
import { assert } from "@cosmjs/utils";
44
import { secp256k1 } from "@noble/curves/secp256k1";
@@ -153,7 +153,7 @@ export class Slip10 {
153153
private static serializedPoint(curve: Slip10Curve, p: bigint): Uint8Array<ArrayBuffer> {
154154
switch (curve) {
155155
case Slip10Curve.Secp256k1:
156-
return secp256k1.Point.BASE.multiply(p).toBytes(true);
156+
return fixUint8Array(secp256k1.Point.BASE.multiply(p).toBytes(true));
157157
default:
158158
throw new Error("curve not supported");
159159
}

0 commit comments

Comments
 (0)