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

Add new scripts to test features from 'QuaiHDWallet' and 'QiHDWallet' #41

Open
wants to merge 2 commits into
base: Quais6Testing
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ cache
artifacts

#Env files
.env
.env*
415 changes: 60 additions & 355 deletions package-lock.json

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions scripts/QIHDWallet_getAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const hre = require('hardhat')
const quais = require('quais6')
const { MNEMONICPHRASE, QI_DERIVATIONPATH} = require('./constants.js');

async function main() {
// Configure quai network provider based on hardhat network config
const quaisProvider = new quais.JsonRpcProvider(hre.network.config.url)

const mnemonic = quais.Mnemonic.fromPhrase(MNEMONICPHRASE);
const qiWallet = quais.QiHDWallet.fromMnemonic(mnemonic, QI_DERIVATIONPATH);

// connect wallet to provider
const qiConnectedWallet = qiWallet.connect(quaisProvider);

console.log('\nInitializing wallet...');
await qiConnectedWallet.init('cyprus1');

const shardWallets = qiConnectedWallet.shardWalletsMap;

let addrData = shardWallets.get('cyprus1').addressesInfo;

console.log('\nQi addresses: ');
for (let i = 0; i < addrData.length; i++) {
console.log(`Address[${i}]: ${addrData[i].address}`);
}

const nakedAddress = await qiConnectedWallet.getAddress('cyprus1');
console.log('first naked Address:', nakedAddress);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//! NOTE: This script is for developement and only works with a modified version of the 'signTransaction' method in the UTXOHDWallet class
//! In order for this script to work, 'signTransaction' must be modified to return the tx object (instead of the serialized string)

const quais = require('quais6')
const { keccak_256 } = require('@noble/hashes/sha3');
const { mnemonicPhrase, derivationPath, privKey1, privKey2, mockTxHash1, mockTxHash2} = require('./constants.js');
Expand All @@ -20,7 +17,10 @@ async function main() {

// Create the UTXO wallet
const mnemonic = quais.Mnemonic.fromPhrase(mnemonicPhrase);
const utxoWallet = quais.UTXOHDWallet.fromMnemonic(mnemonic, derivationPath);
const qiWallet = quais.QiHDWallet.fromMnemonic(mnemonic, derivationPath);

// print the name of the class of qiWallet
console.log('Class of qiWallet:', qiWallet.constructor.name);

// Get the compressed public key, uncompressed public key and address for the two private keys
const pubkey1 = getCompressedPublicKey(privKey1);
Expand All @@ -34,86 +34,94 @@ async function main() {
// Define the UTXO addresses for the wallet
const utxoAddresses = [
{
address: addr1,
privKey: privKey1,
address: addr1,
privKey: privKey1,
},
{
address: addr2,
privKey: privKey2,
address: addr2,
privKey: privKey2,
},
];

// Define the outpoints for the UTXO addresses
const addressOutPoints = {
addr1: [
{
Txhash: mockTxHash1,
Index: 0,
Denomination: 7,
},
{
Txhash: mockTxHash2,
Index: 0,
Denomination: 7,
}
{
Txhash: mockTxHash1,
Index: 0,
Denomination: 7,
},
{
Txhash: mockTxHash2,
Index: 0,
Denomination: 7,
}
],
};

console.log('Wallet key/addres pairs: ', utxoAddresses);
// Manually populate the UTXO addresses and outpoints into the UTXOHDWallet
utxoWallet.addressOutpoints = addressOutPoints;
utxoWallet.utxoAddresses = utxoAddresses;
console.log('Wallet key/addres pairs: ', utxoWallet.utxoAddresses);
qiWallet.shardWalletsMap.set('cyprus1', {addressesInfo: utxoAddresses, outpoints: addressOutPoints})

// Define tx inputs, outputs and chainId for the UTXO
let txInputs = [
{
txhash: mockTxHash1,
index: 0,
// ! NOTE: The pubkey is in uncompressed format. Only this format will result in a valid address
pubKey: hexToUint8Array(pubkey1uncompressed),
pub_key: hexToUint8Array(pubkey1uncompressed),
previous_out_point:
{
hash: {
value: hexToUint8Array(mockTxHash1),
},
index: 0,
},
},
{
txhash: mockTxHash2,
index: 0,
// ! NOTE: The pubkey is in uncompressed format. Only this format will result in a valid address
pubKey: hexToUint8Array(pubkey2uncompressed),
},
pub_key: hexToUint8Array(pubkey2uncompressed),
previous_out_point:
{
hash: {
value: hexToUint8Array(mockTxHash2),
},
index: 0,
},
}

];

console.log('txInputs:', txInputs)

console.log('\ntxInputs[0].pubKey: ', hexlify(txInputs[0].pub_key));
console.log('txInputs[1].pubKey: ', hexlify(txInputs[1].pub_key));
let txOutputs = [
{
Address: addr2,
Denomination: 7,
address: hexToUint8Array(addr2),
denomination: 7,
},
];

console.log('txOutputs:', txOutputs)
console.log('\ntxOutput.address:', hexlify(txOutputs[0].address));

const chainId = BigInt(969);

// Create the UTXO to be signed
const chainId = BigInt(969);
const utxo = createTransaction(chainId, txInputs, txOutputs, addr1);
console.log('utxo tx Serialized:', utxo.unsignedSerialized);

// calculate the hash of the UTXO (message to be signed)
// calculate the hash of the tx (message to be signed)
const txHash = keccak_256(utxo.unsignedSerialized);
console.log('tx hash to sign:', hexlify(txHash));
console.log('\ntx hash to sign:', hexlify(txHash));

// Sign the tx
console.log('Signing UTXO...');
const signedUTXO = await utxoWallet.signTransaction(utxo);
console.log('Signing tx...');
const signedTx = await qiWallet.signTransaction(utxo);

// Get the signature from the signed UTXO
console.log("Getting the signature...")
console.log('Signature: ', signedUTXO.signature);
const signatureSerialzed = getSignatureSerialized(signedUTXO.signature);
console.log('Unmarshalling signed tx...')
const signedTxUnmarshalled = quais.QiTransaction.from(signedTx);
console.log('Signed tx:', signedTxUnmarshalled);

// Get the signature from the signed tx
const signatureSerialzed = signedTxUnmarshalled.signature;

// Verify the transaction passing the signature, txHash and public key (in compressed format)
console.log('Verifying transaction musig signature...');

// Verify the transaction passing the signature, txHash and public key (in compressed format)
const verified = verifyMusigSignature(signatureSerialzed, txHash, [hexToUint8Array(pubkey1), hexToUint8Array(pubkey2)]);
console.log('Verified:', verified);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//! NOTE: This script is for developement and only works with a modified version of the 'signTransaction' method in the UTXOHDWallet class
//! In order for this script to work, 'signTransaction' must be modified to return the tx object (instead of the serialized string)

const quais = require('quais6')
const { keccak_256 } = require('@noble/hashes/sha3');

Expand Down Expand Up @@ -53,35 +50,37 @@ async function main() {
],
};

console.log('Wallet key/addres pairs: ', utxoAddresses);
// Manually populate the UTXO addresses and outpoints into the UTXOHDWallet
utxoWallet.addressOutpoints = addressOutPoints;
utxoWallet.utxoAddresses = utxoAddresses;
console.log('Wallet key/addres pairs: ', utxoWallet.utxoAddresses);
utxoWallet.shardWalletsMap.set('cyprus1', {addressesInfo: utxoAddresses, outpoints: addressOutPoints})

// Define tx inputs, outputs and chainId for the UTXO
let txInputs = [
{
txhash: mockTxHash1,
index: 0,
// ! NOTE: The pubkey is in uncompressed format. Only this format will result in a valid address
pubKey: hexToUint8Array(pubkey1uncompressed),
pub_key: hexToUint8Array(pubkey1uncompressed),
previous_out_point:
{
hash: {
value: hexToUint8Array(mockTxHash1),
},
index: 0,
},
},
];

console.log('txInputs:', txInputs)

console.log('\ntxInputs[0].pubKey: ', hexlify(txInputs[0].pub_key));
let txOutputs = [
{
Address: addr2,
Denomination: 7,
address: hexToUint8Array(addr2),
denomination: 7,
},
];

console.log('txOutputs:', txOutputs)
console.log('\ntxOutput.address:', hexlify(txOutputs[0].address));

const chainId = BigInt(969);

// Create the UTXO to be signed
const chainId = BigInt(969);
const utxo = createTransaction(chainId, txInputs, txOutputs, addr1);
console.log('utxo tx Serialized:', utxo.unsignedSerialized);

Expand All @@ -90,13 +89,15 @@ async function main() {
console.log('tx hash to sign:', hexlify(txHash));

// Sign the tx
console.log('Signing UTXO...');
const signedUTXO = await utxoWallet.signTransaction(utxo);
console.log('Signing tx...');
const signedTx = await utxoWallet.signTransaction(utxo);

console.log('Unmarshalling signed tx...')
const signedTxUnmarshalled = quais.QiTransaction.from(signedTx);
console.log('Signed tx:', signedTxUnmarshalled);

// Get the signature from the signed UTXO
console.log("Getting the signature...")
console.log('Signature: ', signedUTXO.signature);
const signatureSerialzed = getSignatureSerialized(signedUTXO.signature);
// Get the signature from the signed tx
const signatureSerialzed = signedTxUnmarshalled.signature;

// Verify the transaction passing the signature, txHash and public key (in compressed format)
console.log('Verifying transaction Schnorr signature...');
Expand Down
24 changes: 13 additions & 11 deletions scripts/getUTXO.js → scripts/QiHDWallet_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,34 @@ async function main() {
const mnemonicPhrase = "empower cook violin million wool twelve involve nice donate author mammal salt royal shiver birth olympic embody hello beef suit isolate mixed text spot";
const mnemonic = quais.Mnemonic.fromPhrase(mnemonicPhrase);
const derivationPath = "m/44'/969'/0'/0";
const utxoWallet = quais.UTXOHDWallet.fromMnemonic(mnemonic, derivationPath);
const qiWallet = quais.QiHDWallet.fromMnemonic(mnemonic, derivationPath);

// connect wallet to provider
const utxoConnectedWallet = utxoWallet.connect(quaisProvider);
const connectedQiWallet = qiWallet.connect(quaisProvider);

// Ensure provider is ready
await quaisProvider.ready;
console.log('\nInitializing wallet...');
await connectedQiWallet.init('cyprus1');

const shardWallets = connectedQiWallet.shardWalletsMap;

await utxoConnectedWallet.syncUTXOs('cyprus1', 5);
let addrData = shardWallets.get('cyprus1').addressesInfo;

let addresses = utxoConnectedWallet.utxoAddresses;
console.log('\nQi addresses: ');
for (let i = 0; i < addrData.length; i++) {
console.log(`Address[${i}]: ${addrData[i].address}`);
}

console.log('utxo addresses: ', addresses);

let outpoints = utxoConnectedWallet.addressOutpoints;
let outpoints = shardWallets.get('cyprus1').outpoints;

// get the outpoint for the first address
let [[firstAddress, firstAddressOutpoints]] = Object.entries(outpoints);
let [[firstAddress, firstAddressOutpoints]] = Array.from(outpoints);

console.log('First Address:', firstAddress);
console.log('Total Outpoints for First Address:', firstAddressOutpoints.length);

const denominations = [0.001, 0.005, 0.01, 0.05, 0.1, 0.25, 0.5, 1, 5, 10, 20, 50, 100, 1000, 10000, 100000, 1000000];
const totalValue = firstAddressOutpoints.reduce((acc, {TxHash, Index, Denomination}) => {
const value = BigInt(denominations[Denomination]);
// console.log('TxHash:', TxHash, 'Index:', Index, 'Denomination:', denominations[Denomination], " value:", value);
return acc + value;
}, BigInt(0));

Expand Down
21 changes: 21 additions & 0 deletions scripts/QuaiHDWallet_getAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const hre = require('hardhat')
const quais = require('quais6')
const { MNEMONICPHRASE, QI_DERIVATIONPATH} = require('./constants.js');

async function main() {
const mnemonic = quais.Mnemonic.fromPhrase(MNEMONICPHRASE);
const quaiWallet = quais.QuaiHDWallet.fromMnemonic(mnemonic, QI_DERIVATIONPATH);

console.log("\nCalling quaiWallet.getAddress('cyprus1')...");
let address = await quaiWallet.getAddress('cyprus1');
console.log('getAddress(cyprus1) #1:', address);
address = await quaiWallet.getAddress('cyprus1');
console.log('getAddress(cyprus1) #2:', address);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})
26 changes: 14 additions & 12 deletions scripts/constants.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const mnemonicPhrase = "empower cook violin million wool twelve involve nice donate author mammal salt royal shiver birth olympic embody hello beef suit isolate mixed text spot";
const derivationPath = "m/44'/969'/0'/0";
const privKey1 = "0x2d3236b8ae2cb7ab607886b50f2ebb2732491c26ae4f3ca005d3d7c4607f7d05";
const privKey2 = "0xea5e2852d1dd5db30fadaad7dd203a56addb6201961c3fca35b203a2726e34c2";
const mockTxHash1 = "0x1234567890123456789012345678901234567890123456789012345678901234";
const mockTxHash2 = "0xef125e7cf37f3b8de7725d111b62168147b390fc22448c4f46b649768860c213";
const MNEMONICPHRASE = "empower cook violin million wool twelve involve nice donate author mammal salt royal shiver birth olympic embody hello beef suit isolate mixed text spot";
const QI_DERIVATIONPATH = "m/44'/969'/0'/0";
const QUAI_DERIVATIONPATH = "m/44'/994'/0'/0";
const PRIVKEY1 = "0x2d3236b8ae2cb7ab607886b50f2ebb2732491c26ae4f3ca005d3d7c4607f7d05";
const PRIVKEY2 = "0xea5e2852d1dd5db30fadaad7dd203a56addb6201961c3fca35b203a2726e34c2";
const MOCKTXHASH1 = "0x1234567890123456789012345678901234567890123456789012345678901234";
const MOCKTXHASH2 = "0xef125e7cf37f3b8de7725d111b62168147b390fc22448c4f46b649768860c213";


module.exports = {
mnemonicPhrase,
derivationPath,
privKey1,
privKey2,
mockTxHash1,
mockTxHash2
MNEMONICPHRASE,
QI_DERIVATIONPATH,
QUAI_DERIVATIONPATH,
PRIVKEY1,
PRIVKEY2,
MOCKTXHASH1,
MOCKTXHASH2
}
4 changes: 2 additions & 2 deletions scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const hre = require('hardhat')
const quais = require('quais')
const quais = require('quais6')
const { pollFor } = require('quais-polling')
const GreeterJson = require('../artifacts/contracts/Greeter.sol/Greeter.json')

Expand All @@ -10,7 +10,7 @@ constructorArgs = {

async function main() {
// Configure quai network provider based on hardhat network config
const quaisProvider = new quais.providers.JsonRpcProvider(hre.network.config.url)
const quaisProvider = new quais.JsonRpcProvider(hre.network.config.url)

// Configure quai wallet based on hardhat network config
const walletWithProvider = new quais.Wallet(hre.network.config.accounts[0], quaisProvider)
Expand Down
Loading