Skip to content
Permalink
Browse files
chore: replace Math.random by random32Bytes (#234)
  • Loading branch information
vrolland committed Jun 22, 2020
1 parent 74835f0 commit 20c5e95e8fa9d50584fbe50dee566a6ef3044eff
Showing 16 changed files with 37 additions and 45 deletions.
@@ -43,9 +43,9 @@ export default class PaymentNetworkBTCAddressBased
*
* @returns the extensions data object
*/
public createExtensionsDataForCreation(
public async createExtensionsDataForCreation(
paymentNetworkCreationParameters: ExtensionTypes.PnAddressBased.ICreationParameters,
): ExtensionTypes.IAction {
): Promise<ExtensionTypes.IAction> {
return this.btcAddressBased.createExtensionsDataForCreation(paymentNetworkCreationParameters);
}

@@ -43,9 +43,9 @@ export default class PaymentNetworkBTCAddressBased
*
* @returns the extensions data object
*/
public createExtensionsDataForCreation(
public async createExtensionsDataForCreation(
paymentNetworkCreationParameters: ExtensionTypes.PnAddressBased.ICreationParameters,
): ExtensionTypes.IAction {
): Promise<ExtensionTypes.IAction> {
return this.btcAddressBased.createExtensionsDataForCreation(paymentNetworkCreationParameters);
}

@@ -26,9 +26,9 @@ export default class PaymentNetworkDeclarative
* @param paymentNetworkCreationParameters Parameters to create the extension
* @returns The extensionData object
*/
public createExtensionsDataForCreation(
public async createExtensionsDataForCreation(
paymentNetworkCreationParameters: ExtensionTypes.PnAnyDeclarative.ICreationParameters,
): ExtensionTypes.IAction {
): Promise<ExtensionTypes.IAction> {
return this.extension.createCreationAction({
paymentInfo: paymentNetworkCreationParameters.paymentInfo,
refundInfo: paymentNetworkCreationParameters.refundInfo,
@@ -29,9 +29,9 @@ export default class PaymentNetworkERC20AddressBased
* @param paymentNetworkCreationParameters Parameters to create the extension
* @returns The extensionData object
*/
public createExtensionsDataForCreation(
public async createExtensionsDataForCreation(
paymentNetworkCreationParameters: ExtensionTypes.PnAddressBased.ICreationParameters,
): ExtensionTypes.IAction {
): Promise<ExtensionTypes.IAction> {
return this.extension.createCreationAction({
paymentAddress: paymentNetworkCreationParameters.paymentAddress,
refundAddress: paymentNetworkCreationParameters.refundAddress,
@@ -59,11 +59,12 @@ export default class PaymentNetworkERC20ProxyContract implements PaymentTypes.IP
* @param paymentNetworkCreationParameters Parameters to create the extension
* @returns The extensionData object
*/
public createExtensionsDataForCreation(
public async createExtensionsDataForCreation(
paymentNetworkCreationParameters: PaymentTypes.IReferenceBasedCreationParameters,
): ExtensionTypes.IAction {
): Promise<ExtensionTypes.IAction> {
// If no salt is given, generate one
const salt = paymentNetworkCreationParameters.salt || Utils.crypto.generate8randomBytes();
const salt =
paymentNetworkCreationParameters.salt || (await Utils.crypto.generate8randomBytes());

return this.extension.createCreationAction({
paymentAddress: paymentNetworkCreationParameters.paymentAddress,
@@ -46,11 +46,12 @@ export default class PaymentNetworkETHInputData
* @param paymentNetworkCreationParameters Parameters to create the extension
* @returns The extensionData object
*/
public createExtensionsDataForCreation(
public async createExtensionsDataForCreation(
paymentNetworkCreationParameters: PaymentTypes.IReferenceBasedCreationParameters,
): ExtensionTypes.IAction {
): Promise<ExtensionTypes.IAction> {
// If no salt is given, generate one
const salt = paymentNetworkCreationParameters.salt || Utils.crypto.generate8randomBytes();
const salt =
paymentNetworkCreationParameters.salt || (await Utils.crypto.generate8randomBytes());

return this.extension.createCreationAction({
paymentAddress: paymentNetworkCreationParameters.paymentAddress,
@@ -43,7 +43,7 @@ describe('api/btc/mainnet-address-based', () => {
it('can createExtensionsDataForCreation', async () => {
const spy = sandbox.on(mockAdvancedLogic.extensions.addressBasedBtc, 'createCreationAction');

btcAddressedBased.createExtensionsDataForCreation({ paymentAddress: 'address bitcoin' });
await btcAddressedBased.createExtensionsDataForCreation({ paymentAddress: 'address bitcoin' });

expect(spy).to.have.been.called.once;
});
@@ -46,7 +46,7 @@ describe('api/btc/testnet-address-based', () => {
'createCreationAction',
);

btcAddressedBased.createExtensionsDataForCreation({ paymentAddress: 'address bitcoin' });
await btcAddressedBased.createExtensionsDataForCreation({ paymentAddress: 'address bitcoin' });

expect(spy).to.have.been.called.once;
});
@@ -82,7 +82,7 @@ describe('api/declarative', () => {
it('can createExtensionsDataForCreation', async () => {
const spy = sandbox.on(mockAdvancedLogic.extensions.declarative, 'createCreationAction');

declarative.createExtensionsDataForCreation({
await declarative.createExtensionsDataForCreation({
paymentInfo: 'payment instruction',
refundInfo: 'refund instruction',
});
@@ -47,7 +47,9 @@ describe('api/erc20/address-based', () => {
it('can createExtensionsDataForCreation', async () => {
const spy = sandbox.on(mockAdvancedLogic.extensions.addressBasedErc20, 'createCreationAction');

erc20AddressedBased.createExtensionsDataForCreation({ paymentAddress: 'ethereum address' });
await erc20AddressedBased.createExtensionsDataForCreation({
paymentAddress: 'ethereum address',
});

expect(spy).to.have.been.called.once;
});
@@ -40,7 +40,7 @@ describe('api/erc20/proxy-contract', () => {
it('can createExtensionsDataForCreation', async () => {
const spy = sandbox.on(mockAdvancedLogic.extensions.proxyContractErc20, 'createCreationAction');

erc20ProxyContract.createExtensionsDataForCreation({
await erc20ProxyContract.createExtensionsDataForCreation({
paymentAddress: 'ethereum address',
salt: 'ea3bc7caf64110ca',
});
@@ -55,7 +55,7 @@ describe('api/erc20/proxy-contract', () => {
it('can createExtensionsDataForCreation without salt', async () => {
const spy = sandbox.on(mockAdvancedLogic.extensions.proxyContractErc20, 'createCreationAction');

erc20ProxyContract.createExtensionsDataForCreation({
await erc20ProxyContract.createExtensionsDataForCreation({
paymentAddress: 'ethereum address',
});

@@ -47,7 +47,7 @@ describe('api/eth/input-data', () => {
it('can createExtensionsDataForCreation', async () => {
const spy = sandbox.on(mockAdvancedLogic.extensions.ethereumInputData, 'createCreationAction');

ethInputData.createExtensionsDataForCreation({ paymentAddress: 'ethereum address' });
await ethInputData.createExtensionsDataForCreation({ paymentAddress: 'ethereum address' });

expect(spy).to.have.been.called.once;
});
@@ -381,7 +381,7 @@ export default class RequestNetwork {
if (paymentNetwork) {
// create the extensions data for the payment network
copiedRequestParameters.extensionsData.push(
paymentNetwork.createExtensionsDataForCreation(
await paymentNetwork.createExtensionsDataForCreation(
paymentNetworkCreationParameters.parameters,
),
);
@@ -31,7 +31,7 @@ export interface IReferenceBasedCreationParameters {

/** Interface of the class to manage a payment network */
export interface IPaymentNetwork<TEventParameters = any> {
createExtensionsDataForCreation: (paymentNetworkCreationParameters: any) => any;
createExtensionsDataForCreation: (paymentNetworkCreationParameters: any) => Promise<any>;
createExtensionsDataForAddRefundInformation: (parameters: any) => any;
createExtensionsDataForAddPaymentInformation: (parameters: any) => any;
getBalance(request: RequestLogic.IRequest): Promise<IBalanceWithEvents<TEventParameters>>;
@@ -77,19 +77,7 @@ async function generate32BufferKey(): Promise<string> {
*
* @returns a string of 8 random bytes
*/
function generate8randomBytes(): string {
const base16 = 16;

const generate4randomBytes = (): string => {
// A 4 byte random integer
const randomInteger = Math.floor(Math.random() * Math.pow(2, 4 * 8));

// Convert to hexadecimal and padded with 0
return randomInteger.toString(base16).padStart(8, '0');
};

// Do it in 2 passes because an integer doesn't have enough bits
const high = generate4randomBytes();
const low = generate4randomBytes();
return high + low;
async function generate8randomBytes(): Promise<string> {
const random32Bytes = await CryptoWrapper.random32Bytes();
return random32Bytes.slice(0, 8).toString('hex');
}
@@ -111,26 +111,26 @@ describe('Utils.crypto', () => {
/* tslint:disable:no-unused-expression */
/* tslint:disable:no-magic-numbers */
describe('generate8randomBytes', () => {
it('generates a 16 charaters long string', () => {
it('generates a 16 characters long string', async () => {
// Do it 20 times because it's random. It's ok, it takes a few milliseconds
for (let i = 0; i < 100; i++) {
expect(crypto.generate8randomBytes().length).to.be.equal(16);
expect((await crypto.generate8randomBytes()).length).to.be.equal(16);
}
});

it('generates a 16 charater of hexademical number', () => {
it('generates a 16 character of hexademical number', async () => {
// Regex for "at least 16 hexadecimal numbers". Used to validate the salt
const eightHexRegex = /[0-9a-f]{16}/;

// Do it 20 times because it's random. It's ok, it takes a few milliseconds
for (let i = 0; i < 100; i++) {
expect(eightHexRegex.test(crypto.generate8randomBytes())).to.be.true;
expect(eightHexRegex.test(await crypto.generate8randomBytes())).to.be.true;
}
});

it('generates unique strings', () => {
const first = crypto.generate8randomBytes();
const second = crypto.generate8randomBytes();
it('generates unique strings', async () => {
const first = await crypto.generate8randomBytes();
const second = await crypto.generate8randomBytes();
expect(first).to.not.equal(second);
});
});

0 comments on commit 20c5e95

Please sign in to comment.