Use `concat` to simplify code

This commit is contained in:
Franck Royer 2022-03-25 16:31:00 +11:00
parent bf63e85e9e
commit 52009b7be9
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 16 additions and 32 deletions

View File

@ -1,6 +1,7 @@
import crypto from "crypto"; import crypto from "crypto";
import * as secp256k1 from "secp256k1"; import * as secp256k1 from "secp256k1";
import { concat } from "uint8arrays/concat";
import { AbstractKeypair, IKeypair, IKeypairClass, KeypairType } from "./types"; import { AbstractKeypair, IKeypair, IKeypairClass, KeypairType } from "./types";
@ -8,20 +9,14 @@ export function secp256k1PublicKeyToCompressed(
publicKey: Uint8Array publicKey: Uint8Array
): Uint8Array { ): Uint8Array {
if (publicKey.length === 64) { if (publicKey.length === 64) {
const _publicKey = new Uint8Array(publicKey.length + 1); publicKey = concat([[4], publicKey], 65);
_publicKey.set([4]);
_publicKey.set(publicKey, 1);
publicKey = _publicKey;
} }
return secp256k1.publicKeyConvert(publicKey, true); return secp256k1.publicKeyConvert(publicKey, true);
} }
export function secp256k1PublicKeyToFull(publicKey: Uint8Array): Uint8Array { export function secp256k1PublicKeyToFull(publicKey: Uint8Array): Uint8Array {
if (publicKey.length === 64) { if (publicKey.length === 64) {
const _publicKey = new Uint8Array(publicKey.length + 1); publicKey = concat([[4], publicKey], 65);
_publicKey.set([4]);
_publicKey.set(publicKey, 1);
publicKey = _publicKey;
} }
return secp256k1.publicKeyConvert(publicKey, false); return secp256k1.publicKeyConvert(publicKey, false);
} }
@ -67,11 +62,7 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
sign(msg: Uint8Array): Uint8Array { sign(msg: Uint8Array): Uint8Array {
const { signature, recid } = secp256k1.ecdsaSign(msg, this.privateKey); const { signature, recid } = secp256k1.ecdsaSign(msg, this.privateKey);
return concat([signature, [recid]], signature.length + 1);
const result = new Uint8Array(signature.length + 1);
result.set(signature);
result.set([recid], signature.length);
return result;
} }
verify(msg: Uint8Array, sig: Uint8Array): boolean { verify(msg: Uint8Array, sig: Uint8Array): boolean {

View File

@ -1,8 +1,8 @@
import * as secp from "@noble/secp256k1"; import * as secp from "@noble/secp256k1";
import { concat } from "uint8arrays/concat";
import { randomBytes, sha256, subtle } from "../crypto"; import { randomBytes, sha256, subtle } from "../crypto";
import { hexToBytes } from "../utils"; import { hexToBytes } from "../utils";
/** /**
* HKDF as implemented in go-ethereum. * HKDF as implemented in go-ethereum.
*/ */
@ -12,17 +12,18 @@ function kdf(secret: Uint8Array, outputLength: number): Promise<Uint8Array> {
let willBeResult = Promise.resolve(new Uint8Array()); let willBeResult = Promise.resolve(new Uint8Array());
while (written < outputLength) { while (written < outputLength) {
const counters = new Uint8Array([ctr >> 24, ctr >> 16, ctr >> 8, ctr]); const counters = new Uint8Array([ctr >> 24, ctr >> 16, ctr >> 8, ctr]);
const countersSecret = new Uint8Array(counters.length + secret.length); const countersSecret = concat(
countersSecret.set(counters, 0); [counters, secret],
countersSecret.set(secret, counters.length); counters.length + secret.length
);
const willBeHashResult = sha256(countersSecret); const willBeHashResult = sha256(countersSecret);
willBeResult = willBeResult.then((result) => willBeResult = willBeResult.then((result) =>
willBeHashResult.then((hashResult) => { willBeHashResult.then((hashResult) => {
const _hashResult = new Uint8Array(hashResult); const _hashResult = new Uint8Array(hashResult);
const _res = new Uint8Array(result.length + _hashResult.length); return concat(
_res.set(result, 0); [result, _hashResult],
_res.set(_hashResult, result.length); result.length + _hashResult.length
return _res; );
}) })
); );
written += 32; written += 32;
@ -135,24 +136,16 @@ export async function encrypt(
const encryptionKey = hash.slice(0, 16); const encryptionKey = hash.slice(0, 16);
const cipherText = await aesCtrEncrypt(iv, encryptionKey, msg); const cipherText = await aesCtrEncrypt(iv, encryptionKey, msg);
const ivCipherText = new Uint8Array(iv.length + cipherText.length); const ivCipherText = concat([iv, cipherText], iv.length + cipherText.length);
ivCipherText.set(iv, 0);
ivCipherText.set(cipherText, iv.length);
const macKey = await sha256(hash.slice(16)); const macKey = await sha256(hash.slice(16));
const hmac = await hmacSha256Sign(macKey, ivCipherText); const hmac = await hmacSha256Sign(macKey, ivCipherText);
const ephemPublicKey = secp.getPublicKey(ephemPrivateKey, false); const ephemPublicKey = secp.getPublicKey(ephemPrivateKey, false);
const cipher = new Uint8Array( return concat(
[ephemPublicKey, ivCipherText, hmac],
ephemPublicKey.length + ivCipherText.length + hmac.length ephemPublicKey.length + ivCipherText.length + hmac.length
); );
let index = 0;
cipher.set(ephemPublicKey, index);
index += ephemPublicKey.length;
cipher.set(ivCipherText, index);
index += ivCipherText.length;
cipher.set(hmac, index);
return cipher;
} }
const metaLength = 1 + 64 + 16 + 32; const metaLength = 1 + 64 + 16 + 32;