Skip to content

Commit

Permalink
fix quaiwallet signing unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoacosta74 authored and rileystephens28 committed Dec 13, 2024
1 parent 5ec5fc8 commit a520d91
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
24 changes: 17 additions & 7 deletions examples/signing/sign-verify-quai.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,34 @@ async function main() {
// Create tx
const addressInfo1 = await quaiWallet.getNextAddress(0, quais.Zone.Cyprus1);
const from = addressInfo1.address;
console.log('from: ', from);
const txObj = new quais.QuaiTransaction(from);
txObj.chainId = BigInt(969);
txObj.nonce = BigInt(0);
txObj.gasLimit = BigInt(1000000);
(txObj.minerTip = BigInt(10000000000)),
(txObj.gasPrice = BigInt(30000000000000)),
(txObj.to = '0x002F4783248e2D6FF1aa6482A8C0D7a76de3C329');
txObj.minerTip = BigInt(10000000000);
txObj.gasPrice = BigInt(30000000000000);
txObj.to = '0x002F4783248e2D6FF1aa6482A8C0D7a76de3C329';
txObj.value = BigInt(4200000);


// transaction to sign
console.log('txObj: ', JSON.stringify(txObj, null, 2));

// Sign the tx
const signedTxSerialized = await quaiWallet.signTransaction(txObj);
const signedTxSerialized = await quaiWallet.signTransaction(txObj);
console.log('signedTxSerialized: ', signedTxSerialized);

// Unmarshall the signed tx
const signedTx = quais.QuaiTransaction.from(signedTxSerialized);
const signedTxObj = quais.QuaiTransaction.from(signedTxSerialized);

console.log('signedTxObj: ', signedTxObj);

// Get the signature
const signature = signedTx.signature;
const signature = signedTxObj.signature;

// Verify the signature
const txHash = signedTx.digest;
const txHash = signedTxObj.digest;
const signerAddress = quais.recoverAddress(txHash, signature);

if (signerAddress === from) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,35 @@ import { loadTests } from '../utils.js';

import { TestCaseQuaiTransaction, TestCaseQuaiTypedData, Zone, TestCaseQuaiMessageSign } from '../types.js';

import { recoverAddress } from '../../index.js';
import { QuaiTransaction, recoverAddress, Signature } from '../../index.js';

import { Mnemonic, QuaiHDWallet } from '../../index.js';

describe('Test transaction signing', function () {
const tests = loadTests<TestCaseQuaiTransaction>('quai-transaction');
for (const test of tests) {
let txHash: string;
let signature: Signature;
it(`tests signing an EIP-155 transaction: ${test.name}`, async function () {
const mnemonic = Mnemonic.fromPhrase(test.mnemonic);
const quaiWallet = QuaiHDWallet.fromMnemonic(mnemonic);
quaiWallet.getNextAddressSync(test.params.account, test.params.zone);
const txData = test.transaction;
const signed = await quaiWallet.signTransaction(txData);
assert.equal(signed, test.signed, 'signed');
const signedTxSerialized = await quaiWallet.signTransaction(txData);
assert.equal(signedTxSerialized, test.signed, 'signed');

const signedTxObj = QuaiTransaction.from(signedTxSerialized);
txHash = signedTxObj.digest;
signature = signedTxObj.signature;
});

it(`tests verifying the signature: ${test.name}`, function () {
const signerAddress = recoverAddress(txHash, signature);
assert.equal(
signerAddress,
test.transaction.from,
`Signer address expected to be ${test.transaction.from} but got ${signerAddress}`,
);
});
}
});
Expand Down
Binary file modified testcases/quai-sign-message.json.gz
Binary file not shown.
Binary file modified testcases/quai-transaction.json.gz
Binary file not shown.
Binary file modified testcases/sign-typed-data.json.gz
Binary file not shown.

0 comments on commit a520d91

Please sign in to comment.