Skip to content

Commit

Permalink
Merge pull request #162 from CirclesUBI/typedData-executeSafeTx
Browse files Browse the repository at this point in the history
Typed data executeSafeTx
  • Loading branch information
llunaCreixent authored Feb 11, 2023
2 parents 5befc02 + 5eb578c commit cdb1757
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 42 deletions.
6 changes: 5 additions & 1 deletion src/token.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ZERO_ADDRESS } from '~/common/constants';
import { SAFE_LAST_VERSION, ZERO_ADDRESS } from '~/common/constants';

import CoreError, { TransferError, ErrorCodes } from '~/common/error';
import checkAccount from '~/common/checkAccount';
import checkOptions from '~/common/checkOptions';
import { getTokenContract } from '~/common/getContracts';
import { getVersion } from '~/safe';

/* Due to block gas limit of 12.500.000 a transitive transaction can have a
* limited number of steps. The limit below gives a 50% buffer between the
Expand Down Expand Up @@ -225,11 +226,14 @@ export default function createTokenModule(web3, contracts, utils) {

const txData = await hub.methods.signup().encodeABI();

const safeVersion = await getVersion(web3, options.safeAddress);

// Call method and return result
return await utils.executeSafeTx(account, {
safeAddress: options.safeAddress,
to: hub.options.address,
txData,
isCRCVersion: safeVersion != SAFE_LAST_VERSION,
});
},

Expand Down
61 changes: 43 additions & 18 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,8 @@ export default function createUtilsModule(web3, contracts, globalOptions) {
* @param {string} userOptions.gasToken - address of ERC20 token
* @param {Object} userOptions.txData - encoded transaction data
* @param {number} userOptions.value - value in Wei
* @param {boolean} userOptions.isCRCVersion - is the Safe v1.1.1+Cirlces, false by default
*
* @return {string} - transaction hash
*/
Expand All @@ -935,9 +937,14 @@ export default function createUtilsModule(web3, contracts, globalOptions) {
type: 'number',
default: 0,
},
isCRCVersion: {
type: 'boolean',
default: false,
},
});

const { to, gasToken, txData, value, safeAddress } = options;
const { to, gasToken, txData, value, safeAddress, isCRCVersion } =
options;
const operation = CALL_OP;
const refundReceiver = ZERO_ADDRESS;

Expand Down Expand Up @@ -982,24 +989,42 @@ export default function createUtilsModule(web3, contracts, globalOptions) {
// Request nonce for Safe
const nonce = await requestNonce(web3, relayServiceEndpoint, safeAddress);

// Get the chainId from the network
const chainId = await web3.eth.getChainId();

// Prepare EIP712 transaction data and sign it
const typedData = formatTypedData(
to,
value,
txData,
operation,
safeTxGas,
dataGas,
gasPrice,
gasToken,
refundReceiver,
nonce,
chainId,
safeAddress,
);
let typedData;
if (isCRCVersion == true) {
// Prepare EIP712 transaction data and sign it
typedData = formatTypedDataCRCVersion(
to,
value,
txData,
operation,
safeTxGas,
dataGas,
gasPrice,
gasToken,
refundReceiver,
nonce,
safeAddress,
);
} else {
// Get the chainId from the network
const chainId = await web3.eth.getChainId();
// Prepare EIP712 transaction data and sign it
typedData = formatTypedData(
to,
value,
txData,
operation,
safeTxGas,
dataGas,
gasPrice,
gasToken,
refundReceiver,
nonce,
chainId,
safeAddress,
);
}

const signature = signTypedData(web3, account.privateKey, typedData);

Expand Down
28 changes: 5 additions & 23 deletions test/safe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ import {
deploySafeAndToken,
fundSafe,
deployCRCVersionSafe,
deployCRCVersionToken,
deployToken,
} from './helpers/transactions';

import {
SAFE_LAST_VERSION,
SAFE_CRC_VERSION,
ZERO_ADDRESS,
} from '~/common/constants';
import getContracts from '~/common/getContracts';
import { SAFE_LAST_VERSION, SAFE_CRC_VERSION } from '~/common/constants';

describe('Safe', () => {
let core;
Expand Down Expand Up @@ -247,33 +242,20 @@ describe('Safe', () => {
let ownerCRCVersion;
let CRCVersionSafeAddress;
let CRCVersionSafeInstance;
let contracts;

beforeAll(async () => {
// Deploy new version (v1.3.0)
const result = await deploySafeAndToken(core, accounts[0]);
safeAddress = result.safeAddress;

// Get the hub
const hubAddress = core.options.hubAddress;
contracts = await getContracts(web3, {
hubAddress: hubAddress,
proxyFactoryAddress: ZERO_ADDRESS,
safeMasterAddress: ZERO_ADDRESS,
});
const { hub } = contracts;

// Deploy a Safe with the CRC version (v1.1.1+Circles)
ownerCRCVersion = getAccount(8);
CRCVersionSafeInstance = await deployCRCVersionSafe(ownerCRCVersion);
CRCVersionSafeAddress = CRCVersionSafeInstance.options.address;
await fundSafe(accounts[0], CRCVersionSafeAddress);
await deployCRCVersionToken(
web3,
ownerCRCVersion,
CRCVersionSafeInstance,
hub,
);
await deployToken(core, ownerCRCVersion, {
safeAddress: CRCVersionSafeAddress,
});
});

it('I should get the last version by default', async () => {
Expand Down

0 comments on commit cdb1757

Please sign in to comment.