-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add testing script in examples folder
- Loading branch information
1 parent
e7b62d9
commit d2acbf8
Showing
5 changed files
with
416 additions
and
0 deletions.
There are no files selected for viewing
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
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); | ||
}); |
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
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); | ||
}); |
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
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); | ||
}); |
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
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); | ||
}); |
Oops, something went wrong.