Skip to content

Instantly share code, notes, and snippets.

@Ankarrr
Created March 6, 2020 18:53
Show Gist options
  • Save Ankarrr/3f7b792ff7860726def26fbbd77c0714 to your computer and use it in GitHub Desktop.
Save Ankarrr/3f7b792ff7860726def26fbbd77c0714 to your computer and use it in GitHub Desktop.
import { ethers } from 'ethers';
import web3 from 'web3-utils';
const priKey = 'xxxxx';
const personalWalletAddress = '0xC1Ad71338509982d1E5738CfC09F5975B5c7d9fd';
// eslint-disable-next-line object-curly-newline
const personalWalletAbi = [{ constant: false, inputs: [ { name: '_to', type: 'address[]' }, { name: '_value', type: 'uint256[]' }, { name: '_idx', type: 'uint256[]' }, { name: '_data', type: 'bytes' }, { name: '_nonce', type: 'uint256' }, { name: '_v', type: 'uint8' }, { name: '_r', type: 'bytes32' }, { name: '_s', type: 'bytes32' } ], name: 'batchDelegateExecute', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [ { name: '_to', type: 'address[]' }, { name: '_value', type: 'uint256[]' }, { name: '_idx', type: 'uint256[]' }, { name: '_data', type: 'bytes' } ], name: 'batchExecute', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [ { name: '_to', type: 'address' }, { name: '_value', type: 'uint256' }, { name: '_data', type: 'bytes' }, { name: '_nonce', type: 'uint256' }, { name: '_v', type: 'uint8' }, { name: '_r', type: 'bytes32' }, { name: '_s', type: 'bytes32' } ], name: 'delegateExecute', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [ { name: '_to', type: 'address' }, { name: '_value', type: 'uint256' }, { name: '_data', type: 'bytes' } ], name: 'execute', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [ { name: '_owner', type: 'address' } ], name: 'setOwner', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [ { name: '_recovery', type: 'address' } ], name: 'setRecovery', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { inputs: [ { name: '_owner', type: 'address' }, { name: '_recovery', type: 'address' } ], payable: false, stateMutability: 'nonpayable', type: 'constructor' }, { payable: true, stateMutability: 'payable', type: 'fallback' }, { anonymous: false, inputs: [ { indexed: false, name: 'to', type: 'address' }, { indexed: false, name: 'value', type: 'uint256' }, { indexed: false, name: 'data', type: 'bytes' } ], name: 'Execute', type: 'event' }, { constant: true, inputs: [ { name: '_owner', type: 'address' } ], name: 'isOwner', outputs: [ { name: '', type: 'bool' } ], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [ { name: '_data', type: 'bytes' }, { name: '_signature', type: 'bytes' } ], name: 'isValidSignature', outputs: [ { name: 'magicValue', type: 'bytes4' } ], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [], name: 'nonce', outputs: [ { name: '', type: 'uint256' } ], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [], name: 'owner', outputs: [ { name: '', type: 'address' } ], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [], name: 'owners', outputs: [ { name: '_owners', type: 'address[]' } ], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [], name: 'recovery', outputs: [ { name: '', type: 'address' } ], payable: false, stateMutability: 'view', type: 'function' } ];
const provider = ethers.getDefaultProvider('ropsten');
const wallet = new ethers.Wallet(priKey, provider);
// Prepare data to sign
const to = '0x4378Faec5cCfCC6B9E1A8174435eB4354398EDdd'; // to address
const value = 1000000; // ETH
const nonce = 0; // tx nonce
const txData = '0x'; // tx data
// Convert data to hash
// const msg = JSON.stringify({
// to, value, txData, nonce,
// });
// Get message to sign
// const hash = ethers.utils.id(msg);
const hash = web3.soliditySha3(to, value, nonce, txData);
console.log(hash);
// Sign
wallet.signMessage(hash).then((result) => {
console.log('signature: ', result);
// Get v, r, s of signature
const sig = ethers.utils.splitSignature(result);
console.log('v, r, s:', sig.v, sig.r, sig.s);
// Execute
const contract = new ethers.Contract(personalWalletAddress, personalWalletAbi, wallet);
return contract.delegateExecute(
to, value, txData, nonce, sig.v, sig.r, sig.s,
);
}).then((result) => {
console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment