Skip to content

Commit

Permalink
fix: offload ramdombytes library from encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsan-javaid authored and janniks committed Mar 28, 2022
1 parent 8cc1774 commit fe3f30e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 69 deletions.
64 changes: 0 additions & 64 deletions packages/common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,67 +413,3 @@ export function hexToBigInt(hex: string): bigint {
export function utf8ToBytes(content: string) {
return new TextEncoder().encode(content);
}

/*
The `concatBytes` and `hexToBytes` methods below were taken from
[noble-secp256k1](https://github.com/paulmillr/noble-secp256k1)
Copyright (c) 2019 Paul Miller (https://paulmillr.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
*/

/**
* Reference: https://github.com/paulmillr/noble-secp256k1/blob/976d3b59bbd30f2d28638e5420b219fd3d829169/index.ts#L669-L682
* Using this function from noble-secp256k1 as its not exported by library till v1.5.5
* Concatenates several Uint8Arrays into one
* @param {byteArrays[]} Uint8Array[] array of arrays
* @output {Uint8Array} instance of bytes concatenated
*/
export function concatBytes(byteArrays: Uint8Array[]): Uint8Array {
const totalSize = byteArrays.reduce((len, bytes) => len + bytes.length, 0);
const resultArray = new Uint8Array(totalSize);
let offset = 0;
for (let i = 0; i < byteArrays.length; i++) {
resultArray.set(byteArrays[i], offset);
offset += byteArrays[i].length;
}
return resultArray;
}

/**
* Reference: https://github.com/paulmillr/noble-secp256k1/blob/976d3b59bbd30f2d28638e5420b219fd3d829169/index.ts#L726-L740
* Using this function from noble-secp256k1 as its not exported by library till v1.5.5
* Converts hex string to Uint8Array
* @param {hex} hex string without 0x prefix
* @output {Uint8Array} instance of bytes
*/
export function hexToBytes(hex: string): Uint8Array {
if (typeof hex !== 'string') {
throw new TypeError('hexToBytes: expected string, got ' + typeof hex);
}
if (hex.slice(0, 2) === '0x') {
throw new Error('input hex should be without 0x prefix');
}
if (hex.length % 2)
throw new Error(`hexToBytes: received invalid unpadded hex, got: ${hex.length}`);

const array = new Uint8Array(hex.length / 2);

for (let i = 0; i < array.length; i++) {
const j = i * 2;
const hexByte = hex.slice(j, j + 2);
const byte = Number.parseInt(hexByte, 16);
if (Number.isNaN(byte) || byte < 0) throw new Error('Invalid byte sequence');
array[i] = byte;
}
return array;
}
2 changes: 0 additions & 2 deletions packages/encryption/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@
"bip39": "^3.0.2",
"bitcoinjs-lib": "^5.2.0",
"bn.js": "^5.2.0",
"randombytes": "^2.1.0",
"ripemd160-min": "^0.0.6",
"sha.js": "^2.4.11"
},
"devDependencies": {
"@peculiar/webcrypto": "^1.1.6",
"@types/elliptic": "^6.4.12",
"@types/jest": "^26.0.22",
"@types/randombytes": "^2.0.0",
"@types/sha.js": "^2.4.0",
"@types/triplesec": "^3.0.0",
"crypto-browserify": "^3.12.0",
Expand Down
11 changes: 8 additions & 3 deletions packages/encryption/src/cryptoRandom.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// @ts-ignore
import { Buffer } from '@stacks/common';
import randombytes from 'randombytes';
import { utils } from '@noble/secp256k1';

export { randombytes as randomBytes };
/**
* Use utils.randomBytes to replace randombytes dependency
* Generates a buffer with random bytes of given length
* @param {bytesLength} an optional bytes length, default is 32 bytes
* @return {Buffer} For return type compatibility converting utils.randomBytes return value to buffer
*/
export const randomBytes = (bytesLength?: number) => Buffer.from(utils.randomBytes(bytesLength));

/** Optional function to generate cryptographically secure random bytes */
export type GetRandomBytes = (count: number) => Buffer;

1 comment on commit fe3f30e

@vercel
Copy link

@vercel vercel bot commented on fe3f30e Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.