Skip to content

Commit

Permalink
fix: rm extra test byte, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
quertc committed Aug 23, 2024
1 parent cfde2e5 commit ad57150
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/target/
.envrc
.DS_Store
.DS_Store
node_modules
.yarn
3 changes: 0 additions & 3 deletions src/signers/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@ pub async fn handle_eth_sign_transaction(
let tx_envelope = tx_request.build(&signer).await?;
println!("tx_envelope: {:?}", tx_envelope.tx_type());
println!("tx_envelope: {:?}", tx_envelope);
tx_envelope.signature_hash();

let mut encoded_tx = vec![];
encoded_tx.push(tx_envelope.tx_type() as u8);
tx_envelope.encode(&mut encoded_tx);
println!("encoded_tx: {:?}", encoded_tx);
let rlp_hex = hex::encode_prefixed(encoded_tx);

println!("rlp_hex: {:?}", rlp_hex);

Ok(JsonRpcReply {
Expand Down
123 changes: 123 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const axios = require('axios')
const readlineSync = require('readline-sync');
const { createPublicClient, http, parseEther, createWalletClient, formatEther } = require('viem');
const { privateKeyToAccount } = require('viem/accounts');
const { anvil } = require('viem/chains')

const DEAD_ADDRESS = '0x000000000000000000000000000000000000dEaD'

async function main() {
console.log('Welcome to Upnode signer-proxy integration test')

const publicClient = createPublicClient({
chain: anvil,
transport: http()
})

const testWalletClient = createWalletClient({
chain: anvil,
transport: http(),
})

// test dummy account, don't use in the production!!!
const testAccount = privateKeyToAccount('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80')

let blockNumber, chainId;

try {
blockNumber = await publicClient.getBlockNumber()
chainId = await publicClient.chain.id
} catch (err) {
console.error(err)
console.log("Can't connect to the chain, please start anvil")
}

console.log('Chain', chainId, 'connected block number', blockNumber)

const endpoint = readlineSync.question('Enter signer endpoint: ')

console.log('Getting wallet address...')

const addressResponse = await axios.get(`${endpoint}/address`);
const address = addressResponse.data.address

console.log('Wallet Address:', address)

{
// Transfer some fund to the wallet
const hash = await testWalletClient.sendTransaction({
account: testAccount,
to: address,
value: parseEther('0.0002'),
})

await publicClient.waitForTransactionReceipt({ hash })
}

// Get target balance before
const balanceBefore = await publicClient.getBalance({
address: DEAD_ADDRESS,
})

console.log('Balance Before:', formatEther(balanceBefore))

const nonce = await publicClient.getTransactionCount({ address })

const transaction = {
from: address,
to: DEAD_ADDRESS,
value: parseEther('0.0001').toString(),
gas: 21500,
gasPrice: 1000000000,
nonce,
data: '0x123456',
chainId: chainId,
};

const txResponse = await axios.post(endpoint, {
jsonrpc: '2.0',
method: 'eth_signTransaction',
params: [transaction],
id: 1,
})
const rawTx = txResponse.data.result

console.log('Signed Tx:', rawTx)

{
// Submit signed transaction
const hash = await publicClient.sendRawTransaction({
serializedTransaction: rawTx,
})

console.log('Transaction submitted hash:', hash)
setTimeout(() => console.log("If it has been taking long then there's something wrong..."), 5000)

await publicClient.waitForTransactionReceipt({ hash })
const transaction = await publicClient.getTransaction({ hash })

console.log('Transaction confirmed')

if (transaction.input != '0x123456') {
console.error('!!! Transaction input is not matched !!!')
}
}

// Get target balance before
const balanceAfter = await publicClient.getBalance({
address: DEAD_ADDRESS,
})

console.log('Balance After:', formatEther(balanceAfter))

if (balanceAfter - balanceBefore === parseEther('0.0001')) {
console.log('Test passed')
} else {
console.log('!!! Incorrect balance diff:', formatEther(balanceAfter - balanceBefore), '!!!')
}
}

main().then(() => process.exit(0)).catch(err => {
console.error(err)
process.exit(1)
})
16 changes: 16 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "signer-proxy-integration-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.7.4",
"readline-sync": "^1.4.10",
"viem": "^2.20.0"
}
}
Loading

0 comments on commit ad57150

Please sign in to comment.