Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Pass in Ganache.provider but only if
// process.env.JSON_RPC_ENDPOINT is not set
endpoint: process.env.JSON_RPC_ENDPOINT,
logger: testLogger,
provider: !process.env.JSON_RPC_ENDPOINT && getGanache(),
});
const accounts = await environment.eth.getAccounts();
ensure(
keyPairs.has(accounts[0].toLowerCase()),
`Unknown address: ${
accounts[0]
}. Are you running ganache with the right mnemonic: ${testMnemonic}`,
);
const web3Accounts = new Web3Accounts(environment.eth.currentProvider);
const signer = (unsignedTransaction, from = new Address(accounts[0])) =>
web3Accounts
.signTransaction(unsignedTransaction, keyPairs.get(from.toLowerCase()))
.then(t => t.rawTransaction);
const enhancedEnvironment = {
...environment,
wallet: {
address: accounts[0],
sign: signer,
},
};
return enhancedEnvironment;
};
const withNewAccount = async (environment: Environment) => {
const log = getLog(environment);
const web3Accounts = new Web3Accounts(environment.eth.currentProvider);
const account = web3Accounts.create();
const enhancedEnvironment = await withPrivateKeySigner(
environment,
account.privateKey,
);
if (process.env.NODE_ENV !== 'production') {
log.info('New account created with privateKey:', account.privateKey);
}
return enhancedEnvironment;
};
const withPrivateKeySigner = async (
environment: Environment,
privateKey: string,
) => {
const web3Accounts = new Web3Accounts(environment.eth.currentProvider);
const { address } = web3Accounts.privateKeyToAccount(privateKey);
const signer = unsignedTransaction =>
web3Accounts
.signTransaction(unsignedTransaction, privateKey)
.then(t => t.rawTransaction);
const withWallet = {
...environment,
wallet: {
address,
sign: signer,
},
};
const withKeystoreSigner = (
environment: Environment,
{ keystore, password }: WithKeystoreSignerArgs,
) => {
const web3Accounts = new Web3Accounts(environment.eth.currentProvider);
const account = web3Accounts.decrypt(keystore, password);
const sign = async unsignedTransaction => {
const signedTransaction = await account.signTransaction(
unsignedTransaction,
);
return signedTransaction.rawTransaction;
};
const enhancedEnvironment = {
...environment,
wallet: {
address: account.address,
sign,
},
};