Created
March 8, 2020 07:07
-
-
Save Ankarrr/bd6f4c0037ec744e4f2bb33176986219 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ethers } from 'ethers'; | |
const priKey = ''; | |
// Contract info | |
const personalWalletAddress = '0x4378Faec5cCfCC6B9E1A8174435eB4354398EDdd'; | |
// 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' } ]; | |
// Create wallet | |
const provider = ethers.getDefaultProvider('ropsten'); | |
const wallet = new ethers.Wallet(priKey, provider); | |
// Prepare data to sign | |
const to = '0x4378Faec5cCfCC6B9E1A8174435eB4354398EDdd'; // to address | |
const value = 100000000000000; // ETH | |
const txData = '0x'; // tx data | |
const nonce = 0; // tx nonce | |
// Convert data to hash | |
const hash = ethers.utils.solidityKeccak256(['address', 'uint256', 'bytes', 'uint256'], [to, value, txData, nonce]); | |
console.log('hash: ', 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