mirror of https://github.com/waku-org/js-waku.git
Use `concat` to simplify code
This commit is contained in:
parent
bf63e85e9e
commit
52009b7be9
|
@ -1,6 +1,7 @@
|
|||
import crypto from "crypto";
|
||||
|
||||
import * as secp256k1 from "secp256k1";
|
||||
import { concat } from "uint8arrays/concat";
|
||||
|
||||
import { AbstractKeypair, IKeypair, IKeypairClass, KeypairType } from "./types";
|
||||
|
||||
|
@ -8,20 +9,14 @@ export function secp256k1PublicKeyToCompressed(
|
|||
publicKey: Uint8Array
|
||||
): Uint8Array {
|
||||
if (publicKey.length === 64) {
|
||||
const _publicKey = new Uint8Array(publicKey.length + 1);
|
||||
_publicKey.set([4]);
|
||||
_publicKey.set(publicKey, 1);
|
||||
publicKey = _publicKey;
|
||||
publicKey = concat([[4], publicKey], 65);
|
||||
}
|
||||
return secp256k1.publicKeyConvert(publicKey, true);
|
||||
}
|
||||
|
||||
export function secp256k1PublicKeyToFull(publicKey: Uint8Array): Uint8Array {
|
||||
if (publicKey.length === 64) {
|
||||
const _publicKey = new Uint8Array(publicKey.length + 1);
|
||||
_publicKey.set([4]);
|
||||
_publicKey.set(publicKey, 1);
|
||||
publicKey = _publicKey;
|
||||
publicKey = concat([[4], publicKey], 65);
|
||||
}
|
||||
return secp256k1.publicKeyConvert(publicKey, false);
|
||||
}
|
||||
|
@ -67,11 +62,7 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
|
|||
|
||||
sign(msg: Uint8Array): Uint8Array {
|
||||
const { signature, recid } = secp256k1.ecdsaSign(msg, this.privateKey);
|
||||
|
||||
const result = new Uint8Array(signature.length + 1);
|
||||
result.set(signature);
|
||||
result.set([recid], signature.length);
|
||||
return result;
|
||||
return concat([signature, [recid]], signature.length + 1);
|
||||
}
|
||||
|
||||
verify(msg: Uint8Array, sig: Uint8Array): boolean {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import * as secp from "@noble/secp256k1";
|
||||
import { concat } from "uint8arrays/concat";
|
||||
|
||||
import { randomBytes, sha256, subtle } from "../crypto";
|
||||
import { hexToBytes } from "../utils";
|
||||
|
||||
/**
|
||||
* HKDF as implemented in go-ethereum.
|
||||
*/
|
||||
|
@ -12,17 +12,18 @@ function kdf(secret: Uint8Array, outputLength: number): Promise<Uint8Array> {
|
|||
let willBeResult = Promise.resolve(new Uint8Array());
|
||||
while (written < outputLength) {
|
||||
const counters = new Uint8Array([ctr >> 24, ctr >> 16, ctr >> 8, ctr]);
|
||||
const countersSecret = new Uint8Array(counters.length + secret.length);
|
||||
countersSecret.set(counters, 0);
|
||||
countersSecret.set(secret, counters.length);
|
||||
const countersSecret = concat(
|
||||
[counters, secret],
|
||||
counters.length + secret.length
|
||||
);
|
||||
const willBeHashResult = sha256(countersSecret);
|
||||
willBeResult = willBeResult.then((result) =>
|
||||
willBeHashResult.then((hashResult) => {
|
||||
const _hashResult = new Uint8Array(hashResult);
|
||||
const _res = new Uint8Array(result.length + _hashResult.length);
|
||||
_res.set(result, 0);
|
||||
_res.set(_hashResult, result.length);
|
||||
return _res;
|
||||
return concat(
|
||||
[result, _hashResult],
|
||||
result.length + _hashResult.length
|
||||
);
|
||||
})
|
||||
);
|
||||
written += 32;
|
||||
|
@ -135,24 +136,16 @@ export async function encrypt(
|
|||
const encryptionKey = hash.slice(0, 16);
|
||||
const cipherText = await aesCtrEncrypt(iv, encryptionKey, msg);
|
||||
|
||||
const ivCipherText = new Uint8Array(iv.length + cipherText.length);
|
||||
ivCipherText.set(iv, 0);
|
||||
ivCipherText.set(cipherText, iv.length);
|
||||
const ivCipherText = concat([iv, cipherText], iv.length + cipherText.length);
|
||||
|
||||
const macKey = await sha256(hash.slice(16));
|
||||
const hmac = await hmacSha256Sign(macKey, ivCipherText);
|
||||
const ephemPublicKey = secp.getPublicKey(ephemPrivateKey, false);
|
||||
|
||||
const cipher = new Uint8Array(
|
||||
return concat(
|
||||
[ephemPublicKey, ivCipherText, hmac],
|
||||
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;
|
||||
|
|
Loading…
Reference in New Issue