Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Add (basic) custom chain support (e.g. Görli) #261

Merged
merged 1 commit into from
Jul 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions modules/node_modules/@colony/purser-core/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 5 additions & 11 deletions modules/node_modules/@colony/purser-ledger/staticMethods.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions modules/node_modules/@colony/purser-metamask/staticMethods.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions modules/node_modules/@colony/purser-trezor/staticMethods.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions modules/tests/mocks/@colony/purser-core/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ export const messageOrDataValidator = jest.fn(
return messageData;
},
);

export const getChainDefinition = jest.fn(() => ({
common: { chainId: 'mocked-chain-id' },
}));
48 changes: 26 additions & 22 deletions modules/tests/purser-ledger/staticMethods/signTransaction.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Transaction as EthereumTx } from 'ethereumjs-tx';

import { transactionObjectValidator } from '@colony/purser-core/helpers';
import {
getChainDefinition,
transactionObjectValidator,
} from '@colony/purser-core/helpers';
import * as utils from '@colony/purser-core/utils';

import { signTransaction } from '@colony/purser-ledger/staticMethods';
Expand Down Expand Up @@ -75,6 +78,7 @@ describe('`Ledger` Hardware Wallet Module Static Methods', () => {
ledgerConnection.mockClear();
handleLedgerConnectionError.mockClear();
transactionObjectValidator.mockClear();
getChainDefinition.mockClear();
derivationPathValidator.mockClear();
utils.warning.mockClear();
});
Expand Down Expand Up @@ -108,6 +112,16 @@ describe('`Ledger` Hardware Wallet Module Static Methods', () => {
expect(derivationPathValidator).toHaveBeenCalled();
expect(derivationPathValidator).toHaveBeenCalledWith(derivationPath);
});
test('Gets the correct chain definition', async () => {
await signTransaction(mockedArgumentsObject);
/*
* Calls the chain definition helper with the correct value
*/
expect(getChainDefinition).toHaveBeenCalled();
expect(getChainDefinition).toHaveBeenCalledWith(
mockedTransactionObject.chainId,
);
});
test('Normalizes the transaction input values', async () => {
await signTransaction(mockedArgumentsObject);
/*
Expand All @@ -127,11 +141,6 @@ describe('`Ledger` Hardware Wallet Module Static Methods', () => {
*/
expect(hexSequenceNormalizer).toHaveBeenCalledWith(nonce);
expect(multipleOfTwoHexValueNormalizer).toHaveBeenCalledWith(nonce);
/*
* Normalizes the destination address
*/
expect(addressNormalizer).toHaveBeenCalled();
expect(addressNormalizer).toHaveBeenCalledWith(to);
/*
* Normalizes the transaction value
*/
Expand Down Expand Up @@ -170,22 +179,17 @@ describe('`Ledger` Hardware Wallet Module Static Methods', () => {
*/
expect(EthereumTx).toHaveBeenCalled();
expect(EthereumTx).toHaveBeenCalledWith(
expect.objectContaining(
{
gasPrice,
gasLimit,
nonce,
value,
data: inputData,
r: String(SIGNATURE.R),
s: String(SIGNATURE.S),
v: chainId,
},
{
chain: chainId,
to,
},
),
expect.objectContaining({
gasPrice,
gasLimit,
nonce,
value,
data: inputData,
r: String(SIGNATURE.R),
s: String(SIGNATURE.S),
v: chainId,
}),
expect.objectContaining({ common: { chainId: 'mocked-chain-id' } }),
);
});
test('Normalizes the signed transaction signature components', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { transactionObjectValidator } from '@colony/purser-core/helpers';
import { Transaction as EthereumTx } from 'ethereumjs-tx';
import {
getChainDefinition,
transactionObjectValidator,
} from '@colony/purser-core/helpers';
import { warning } from '@colony/purser-core/utils';

import { signTransaction } from '@colony/purser-metamask/staticMethods';
Expand All @@ -15,6 +19,7 @@ import {
} from '@colony/purser-core/validators';

import { STD_ERRORS } from '@colony/purser-metamask/defaults';
import { SIGNATURE } from '@colony/purser-core/defaults';

jest.dontMock('@colony/purser-metamask/staticMethods');

Expand All @@ -38,11 +43,23 @@ jest.mock('@colony/purser-metamask/helpers', () =>
require('@mocks/purser-metamask/helpers'),
);

const chainId = 5;

/*
* Mock the injected web3 proxy object
*/
const mockedTransactionHash = 'mocked-transaction-hash';
const mockedRawSignedTransaction = {};
const mockedRawSignedTransaction = {
gas: '1000',
gasPrice: '1',
input: 'mocked-signed-data',
nonce: '10',
r: SIGNATURE.R,
s: SIGNATURE.S,
to: 'mocked-signed-to',
v: SIGNATURE.RECOVERY_EVEN,
value: '0',
};
global.web3 = {
eth: {
sendTransaction: jest.fn((transactionObject, callback) =>
Expand All @@ -67,6 +84,7 @@ const mockedTransactionObject = {
gasPrice,
gasLimit,
to,
chainId,
value,
inputData,
};
Expand All @@ -84,6 +102,7 @@ describe('`Metamask` Wallet Module Static Methods', () => {
addressValidator.mockClear();
safeIntegerValidator.mockClear();
hexSequenceValidator.mockClear();
getChainDefinition.mockClear();
warning.mockClear();
addressNormalizer.mockClear();
hexSequenceNormalizer.mockClear();
Expand Down Expand Up @@ -118,6 +137,16 @@ describe('`Metamask` Wallet Module Static Methods', () => {
mockedTransactionObject,
);
});
test('Gets the correct chain definition', async () => {
await signTransaction(mockedArgumentsObject);
/*
* Calls the chain definition helper with the correct value
*/
expect(getChainDefinition).toHaveBeenCalled();
expect(getChainDefinition).toHaveBeenCalledWith(
mockedTransactionObject.chainId,
);
});
test('Throws if no argument provided', async () => {
expect(signTransaction()).rejects.toThrow();
});
Expand Down Expand Up @@ -182,6 +211,27 @@ describe('`Metamask` Wallet Module Static Methods', () => {
expect(hexSequenceNormalizer).toHaveBeenCalled();
expect(hexSequenceNormalizer).toHaveBeenCalledWith(mockedTransactionHash);
});
test('Creates the unsigned transaction object', async () => {
await signTransaction(mockedArgumentsObject);
/*
* Creates the unsigned transaction, seeding the R,S and V components
*/
expect(EthereumTx).toHaveBeenCalled();
expect(EthereumTx).toHaveBeenCalledWith(
{
data: mockedRawSignedTransaction.input,
r: mockedRawSignedTransaction.r,
s: mockedRawSignedTransaction.s,
v: mockedRawSignedTransaction.v,
to: mockedRawSignedTransaction.to,
gasLimit: expect.anything(),
gasPrice: expect.anything(),
nonce: expect.anything(),
value: expect.anything(),
},
{ common: { chainId: 'mocked-chain-id' } },
);
});
test('Returns the valid hash received from signing', async () => {
const signedTransaction = await signTransaction(mockedArgumentsObject);
expect(global.web3.eth.getTransaction).toHaveBeenCalled();
Expand Down
Loading