Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing scripts for Qi HD Wallet #332

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/signing/pubkey-to-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const quais = require('../../lib/commonjs/quais');


async function main() {
// Check if a public key is provided as a command line argument
if (process.argv.length < 3) {
console.error('Please provide a public key as a command line argument');
process.exit(1);
}

const pubkey = process.argv[2];

// Verify if the provided string is a valid public key of the type 0x0250495cb2f9535c684ebe4687b501c0d41a623d68c118b8dcecd393370f1d90e6
if (!quais.isHexString(pubkey) || pubkey.length !== 68) {
console.error('Invalid public key format');
process.exit(1);
}


try {
// Compute the address from the public key
const address = quais.computeAddress(pubkey);
console.log(`Public Key: ${pubkey}`);
console.log(`Derived Address: ${address}`);
} catch (error) {
console.error('Error computing address:', error.message);
process.exit(1);
}

}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
87 changes: 87 additions & 0 deletions examples/wallets/qi-wallet-alice-bob-send-receive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const quais = require('../../lib/commonjs/quais');
const { printWalletInfo } = require('./utils');
require('dotenv').config();

async function main() {
// Create provider
console.log('RPC URL: ', process.env.RPC_URL);
const provider = new quais.JsonRpcProvider(process.env.RPC_URL);

// Create wallet and connect to provider
const mnemonic = quais.Mnemonic.fromPhrase(process.env.MNEMONIC);
const aliceQiWallet = quais.QiHDWallet.fromMnemonic(mnemonic);
const alicePaymentCode = aliceQiWallet.getPaymentCode(0);
aliceQiWallet.connect(provider);

const bobMnemonic = quais.Mnemonic.fromPhrase("innocent perfect bus miss prevent night oval position aspect nut angle usage expose grace juice");
const bobQiWallet = quais.QiHDWallet.fromMnemonic(bobMnemonic);
bobQiWallet.connect(provider);
const bobPaymentCode = bobQiWallet.getPaymentCode(0);
aliceQiWallet.openChannel(bobPaymentCode);

console.log('Scanning Alice Qi wallet...');
await aliceQiWallet.scan(quais.Zone.Cyprus1);
console.log('Alice Qi wallet scan complete');

printWalletInfo('Alice', aliceQiWallet);

// Bob opens a channel with Alice
bobQiWallet.openChannel(alicePaymentCode);
console.log('Scanning Bob Qi wallet...');
await bobQiWallet.scan(quais.Zone.Cyprus1);
console.log('Bob Qi wallet scan complete');
printWalletInfo('Bob', bobQiWallet);

// Alice sends 50 Qi to Bob
console.log('\nAlice sends 50 Qi to Bob');
const tx = await aliceQiWallet.sendTransaction(bobPaymentCode, 50000n, quais.Zone.Cyprus1, quais.Zone.Cyprus1);
// console.log('Transaction sent: ', tx);
console.log(`Transaction hash: ${tx.hash}`);
console.log(`Tx contains ${tx.txInputs?.length} inputs`);
console.log(`Tx inputs: ${JSON.stringify(tx.txInputs)}`);
console.log(`Tx contains ${tx.txOutputs?.length} outputs`);

console.log('Waiting for transaction to be confirmed...');
const response = await tx.wait();
console.log('Transaction confirmed in block: ', response.blockNumber);

console.log('Syncing Alice Qi wallet...');
await aliceQiWallet.sync(quais.Zone.Cyprus1);
console.log('Alice Qi wallet sync complete');

printWalletInfo('Alice', aliceQiWallet);

console.log('Syncing Bob Qi wallet...');
await bobQiWallet.sync(quais.Zone.Cyprus1);
console.log('Bob Qi wallet sync complete');
printWalletInfo('Bob', bobQiWallet);

console.log('\nBob sends back 25 Qi to Alice');
const tx2 = await bobQiWallet.sendTransaction(alicePaymentCode, 25000n, quais.Zone.Cyprus1, quais.Zone.Cyprus1);
console.log(`Transaction hash: ${tx2.hash}`);
console.log(`Tx contains ${tx2.txInputs?.length} inputs`);
console.log(`Tx inputs: ${JSON.stringify(tx2.txInputs)}`);
console.log(`Tx contains ${tx2.txOutputs?.length} outputs`);

console.log('Waiting for transaction to be confirmed...');
const response2 = await tx2.wait();
console.log('Transaction confirmed in block: ', response2.blockNumber);

console.log('Syncing Alice Qi wallet...');
await aliceQiWallet.sync(quais.Zone.Cyprus1);
console.log('Alice Qi wallet sync complete');

printWalletInfo('Alice', aliceQiWallet);

console.log('Syncing Bob Qi wallet...');
await bobQiWallet.sync(quais.Zone.Cyprus1);
console.log('Bob Qi wallet sync complete');
printWalletInfo('Bob', bobQiWallet);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
73 changes: 73 additions & 0 deletions examples/wallets/qi-wallet-convert-to-quai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const quais = require('../../lib/commonjs/quais');
const { printWalletInfo } = require('./utils');
require('dotenv').config();

async function main() {
// Create provider
console.log('RPC URL: ', process.env.RPC_URL);
const provider = new quais.JsonRpcProvider(process.env.RPC_URL);

// Create Alice's Qi wallet and connect to provider
const mnemonic = quais.Mnemonic.fromPhrase(process.env.MNEMONIC);
const aliceQiWallet = quais.QiHDWallet.fromMnemonic(mnemonic);
aliceQiWallet.connect(provider);

// Initialize Alice's Qi wallet
console.log('\nInitializing Alice Qi wallet...');
await aliceQiWallet.scan(quais.Zone.Cyprus1);
console.log('Alice Qi wallet scan complete');

printWalletInfo('Alice', aliceQiWallet);

// Create Alice's Quai wallet and connect to provider
const aliceQuaiWallet = quais.QuaiHDWallet.fromMnemonic(mnemonic);
aliceQuaiWallet.connect(provider);

// derive quai address
const quaiAddressInfo = await aliceQuaiWallet.getNextAddress(0, quais.Zone.Cyprus1);
console.log('\nAlice Quai address:', quaiAddressInfo.address);

console.log('\nAlice converts 100 Qi to Quai...');

const tx = await aliceQiWallet.convertToQuai(quaiAddressInfo.address, 100000);
// console.log('Transaction sent: ', tx);
console.log(`Transaction hash: ${tx.hash}`);
console.log(`Tx contains ${tx.txInputs?.length} inputs`);
console.log(`Tx contains ${tx.txOutputs?.length} outputs`);
// wait for the transaction to be confirmed
console.log('Waiting for transaction to be confirmed...');
const response = await tx.wait();
console.log('Transaction confirmed in block: ', response.blockNumber);

console.log('Syncing Alice Qi wallet...');
await aliceQiWallet.sync(quais.Zone.Cyprus1);
console.log('Alice Qi wallet sync complete');

printWalletInfo('Alice', aliceQiWallet);

// print Alice's Quai address balance
const balance = await provider.getBalance(quaiAddressInfo.address);
console.log('\nAlice Quai address balance:', quais.formatQuai(balance));

// repeat the same process of converting 100 Qi to Quai
console.log('Alice converts another 100 Qi to Quai...');
const tx2 = await aliceQiWallet.convertToQuai(quaiAddressInfo.address, 100000);
console.log(`Tx contains ${tx2.txInputs?.length} inputs`);
console.log(`Tx contains ${tx2.txOutputs?.length} outputs`);
console.log('Waiting for transaction to be confirmed...');
const response2 = await tx2.wait();
console.log('Transaction confirmed in block: ', response2.blockNumber);

console.log('Syncing Alice Qi wallet...');
await aliceQiWallet.sync(quais.Zone.Cyprus1);
console.log('Alice Qi wallet sync complete');

printWalletInfo('Alice', aliceQiWallet);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
87 changes: 87 additions & 0 deletions examples/wallets/qi-wallet-send-qi-to-bob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const quais = require('../../lib/commonjs/quais');
const { printWalletInfo } = require('./utils');
require('dotenv').config();

async function main() {
// Create provider
console.log('RPC URL: ', process.env.RPC_URL);
const provider = new quais.JsonRpcProvider(process.env.RPC_URL);

// Create wallet and connect to provider
const mnemonic = quais.Mnemonic.fromPhrase(process.env.MNEMONIC);
const aliceQiWallet = quais.QiHDWallet.fromMnemonic(mnemonic);
const alicePaymentCode = aliceQiWallet.getPaymentCode(0);
aliceQiWallet.connect(provider);

const bobMnemonic = quais.Mnemonic.fromPhrase("innocent perfect bus miss prevent night oval position aspect nut angle usage expose grace juice");
const bobQiWallet = quais.QiHDWallet.fromMnemonic(bobMnemonic);
bobQiWallet.connect(provider);
const bobPaymentCode = bobQiWallet.getPaymentCode(0);
aliceQiWallet.openChannel(bobPaymentCode);

console.log('Scanning Alice Qi wallet...');
await aliceQiWallet.scan(quais.Zone.Cyprus1);
console.log('Alice Qi wallet scan complete');

printWalletInfo('Alice', aliceQiWallet);

// Bob opens a channel with Alice
bobQiWallet.openChannel(alicePaymentCode);
console.log('Scanning Bob Qi wallet...');
await bobQiWallet.scan(quais.Zone.Cyprus1);
console.log('Bob Qi wallet scan complete');
printWalletInfo('Bob', bobQiWallet);

// Alice sends 50 Qi to Bob
console.log('\nAlice sends 50 Qi to Bob');
const tx = await aliceQiWallet.sendTransaction(bobPaymentCode, 50000n, quais.Zone.Cyprus1, quais.Zone.Cyprus1);
// console.log('Transaction sent: ', tx);
console.log(`Transaction hash: ${tx.hash}`);
console.log(`Tx contains ${tx.txInputs?.length} inputs`);
console.log(`Tx inputs: ${JSON.stringify(tx.txInputs)}`);
console.log(`Tx contains ${tx.txOutputs?.length} outputs`);

console.log('Waiting for transaction to be confirmed...');
const response = await tx.wait();
console.log('Transaction confirmed in block: ', response.blockNumber);

console.log('Syncing Alice Qi wallet...');
await aliceQiWallet.sync(quais.Zone.Cyprus1);
console.log('Alice Qi wallet sync complete');

printWalletInfo('Alice', aliceQiWallet);

console.log('Syncing Bob Qi wallet...');
await bobQiWallet.sync(quais.Zone.Cyprus1);
console.log('Bob Qi wallet sync complete');
printWalletInfo('Bob', bobQiWallet);

console.log('\nAlice sends another 50 Qi to Bob');
const tx2 = await aliceQiWallet.sendTransaction(bobPaymentCode, 50000n, quais.Zone.Cyprus1, quais.Zone.Cyprus1);
console.log(`Transaction hash: ${tx2.hash}`);
console.log(`Tx contains ${tx2.txInputs?.length} inputs`);
console.log(`Tx inputs: ${JSON.stringify(tx2.txInputs)}`);
console.log(`Tx contains ${tx2.txOutputs?.length} outputs`);

console.log('Waiting for transaction to be confirmed...');
const response2 = await tx2.wait();
console.log('Transaction confirmed in block: ', response2.blockNumber);

console.log('Syncing Alice Qi wallet...');
await aliceQiWallet.sync(quais.Zone.Cyprus1);
console.log('Alice Qi wallet sync complete');

printWalletInfo('Alice', aliceQiWallet);

console.log('Syncing Bob Qi wallet...');
await bobQiWallet.sync(quais.Zone.Cyprus1);
console.log('Bob Qi wallet sync complete');
printWalletInfo('Bob', bobQiWallet);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Loading
Loading