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

Remove deprecated block params #393

Merged
merged 4 commits into from
Feb 4, 2025
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
52 changes: 52 additions & 0 deletions examples/crypto/privkey-to-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const {
computeAddress,
SigningKey,
} = require('../../lib/commonjs/quais');

/**
* Key to Address Example
*
* This script demonstrates how to derive a Quai address from a private key.
* It uses the SigningKey class to compute the public key and then derives
* the corresponding address.
*
* Usage:
* ```
* node key-to-address.js <private-key>
* ```
*
* Example:
* ```
* node key-to-address.js 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
* ```
*
* The script will output:
* - The input private key
* - The derived public key
* - The corresponding Quai address
*/

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 key = process.argv[2];

// Compute the address from the public key
console.log(`Private Key: ${key}`);
const pubkey = SigningKey.computePublicKey(key, true);
console.log(`Public Key: ${pubkey}`);
const address = computeAddress(key);
console.log(`Derived Address: ${address}`);

}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
52 changes: 52 additions & 0 deletions examples/crypto/pubkey-to-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const {
computeAddress,
} = require('../../lib/commonjs/quais');

/**
* Public Key to Address Example
*
* This script demonstrates how to derive a Quai address directly from a public key.
* It uses the computeAddress function to derive the corresponding address from
* an uncompressed or compressed public key.
*
* Usage:
* ```
* node pubkey-to-address.js <public-key>
* ```
*
* Example:
* ```
* node pubkey-to-address.js 0x0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
* ```
*
* The script will output:
* - The input public key
* - The derived Quai address
*
* Note: The public key can be either compressed (33 bytes, starting with 02 or 03)
* or uncompressed (65 bytes, starting with 04).
*/

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];

// Compute the address from the public key
const address = computeAddress(pubkey);
console.log(`Public Key: ${pubkey}`);
console.log(`Derived Address: ${address}`);


}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
const quais = require('../../lib/commonjs/quais');
const {
Mnemonic,
QiHDWallet,
Zone,
QiTransaction,
getBytes,
musigCrypto,
hexlify,
} = require('../../lib/commonjs/quais');
require('dotenv').config();
const { keccak_256 } = require('@noble/hashes/sha3');
const { schnorr } = require('@noble/curves/secp256k1');
const { MuSigFactory } = require('@brandonblack/musig');

/**
* MuSig Signature Example for Qi Transactions
*
* This script demonstrates how to create and verify a MuSig (multi-signature) Schnorr signature
* for a Qi transaction. It shows the complete workflow of:
* 1. Creating multiple addresses from a single wallet
* 2. Creating a Qi transaction with multiple inputs
* 3. Signing with MuSig (aggregated signatures)
* 4. Verifying the aggregated signature
*
* Usage:
* First, set up your .env file with:
* MNEMONIC="your twelve word mnemonic phrase here"
*
* Then run:
* ```
* node sign-verify-qi-musig.js
* ```
*
* The script will output:
* - Two signer addresses
* - The transaction to be signed
* - The serialized signed transaction
* - The transaction hash
* - The verification result
*
* Note: This example uses MuSig for aggregating Schnorr signatures, which is
* particularly useful for UTXO-based multi-signature transactions.
*/

async function main() {
// Create wallet
const mnemonic = quais.Mnemonic.fromPhrase(process.env.MNEMONIC);
const qiWallet = quais.QiHDWallet.fromMnemonic(mnemonic);
const mnemonic = Mnemonic.fromPhrase(process.env.MNEMONIC);
const qiWallet = QiHDWallet.fromMnemonic(mnemonic);

// Generate 1 address for each outpoint
const addressInfo1 = await qiWallet.getNextAddress(0, quais.Zone.Cyprus1);
const addressInfo1 = await qiWallet.getNextAddress(0, Zone.Cyprus1);
const addr1 = addressInfo1.address;
const pubkey1 = addressInfo1.pubKey;

const addressInfo2 = await qiWallet.getNextAddress(0, quais.Zone.Cyprus1);
console.log('\n... signer address #1: ', addr1);
const addressInfo2 = await qiWallet.getNextAddress(0, Zone.Cyprus1);
const addr2 = addressInfo2.address;
const pubkey2 = addressInfo2.pubKey;

console.log('\n... signer address #2: ', addr2);
// Define the outpoints for addr1
const outpointsInfo = [
{
Expand All @@ -27,7 +64,7 @@ async function main() {
denomination: 7,
},
address: addr1,
zone: quais.Zone.Cyprus1,
zone: Zone.Cyprus1,
},
{
outpoint: {
Expand All @@ -36,7 +73,7 @@ async function main() {
denomination: 7,
},
address: addr2,
zone: quais.Zone.Cyprus1,
zone: Zone.Cyprus1,
},
];

Expand Down Expand Up @@ -65,27 +102,31 @@ async function main() {
];

// Create the Qi Tx to be signed
const tx = new quais.QiTransaction();
const tx = new QiTransaction();
tx.txInputs = txInputs;
tx.txOutputs = txOutputs;

// Calculate the hash of the Qi tx (message to be signed and verified)
const txHash = keccak_256(tx.unsignedSerialized);
console.log('\n... transaction to sign: ', JSON.stringify(tx, null, 2));

// Sign the tx
const serializedSignedTx = await qiWallet.signTransaction(tx);

// Unmarshall the signed Tx
const signedTx = quais.QiTransaction.from(serializedSignedTx);
const signedTxObj = QiTransaction.from(serializedSignedTx);
console.log('\n... signed transaction (serialized): ', serializedSignedTx);

// Digest to verify
const txHash = getBytes(signedTxObj.digest);
console.log('\n... txHash to verify: ', signedTxObj.digest);

// Get the signature from the signed tx
const signature = signedTx.signature;
const signature = signedTxObj.signature;

const musig = MuSigFactory(quais.musigCrypto);
const pubKeysArray = [quais.getBytes(pubkey1), quais.getBytes(pubkey2)];
const musig = MuSigFactory(musigCrypto);
const pubKeysArray = [getBytes(pubkey1), getBytes(pubkey2)];
const aggPublicKeyObj = musig.keyAgg(pubKeysArray);

let aggPublicKey = quais.hexlify(aggPublicKeyObj.aggPublicKey);
let aggPublicKey = hexlify(aggPublicKeyObj.aggPublicKey);

// Remove the last 32 bytes (64 hex) from the aggPublicKey
let compressedPubKey = aggPublicKey.slice(0, -64);
Expand All @@ -94,7 +135,7 @@ async function main() {
compressedPubKey = '0x' + compressedPubKey.slice(4);

// Verify the schnoor signature
const verified = schnorr.verify(quais.getBytes(signature), txHash, quais.getBytes(compressedPubKey));
const verified = schnorr.verify(getBytes(signature), txHash, getBytes(compressedPubKey));
console.log('Verified:', verified);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,41 @@ const {
Zone,
QiTransaction,
getBytes,
keccak256,
} = require('../../lib/commonjs/quais');
require('dotenv').config();

const { schnorr } = require('@noble/curves/secp256k1');

/**
* Schnorr Signature Example for Qi Transactions
*
* This script demonstrates how to create and verify a Schnorr signature
* for a Qi transaction. It shows the complete workflow of:
* 1. Creating a wallet and deriving an address
* 2. Creating a Qi transaction
* 3. Signing with Schnorr signature
* 4. Verifying the signature
*
* Usage:
* First, set up your .env file with:
* MNEMONIC="your twelve word mnemonic phrase here"
*
* Then run:
* ```
* node sign-verify-qi-schnorr.js
* ```
*
* The script will output:
* - The signer's address
* - The transaction to be signed
* - The serialized signed transaction
* - The transaction hash
* - The verification result
*
* Note: This example uses Schnorr signatures which are more efficient and
* provide better privacy compared to ECDSA signatures for UTXO transactions.
*/

async function main() {
// Create wallet
const mnemonic = Mnemonic.fromPhrase(process.env.MNEMONIC);
Expand All @@ -18,6 +47,7 @@ async function main() {
// Get address info
const addressInfo1 = await qiWallet.getNextAddress(0, Zone.Cyprus1);
const addr1 = addressInfo1.address;
console.log('\n... signer address: ', addr1);
const pubkey1 = addressInfo1.pubKey;

// Define the outpoints for addr1 (this is just an example outpoint)
Expand Down Expand Up @@ -57,24 +87,27 @@ async function main() {
tx.txInputs = txInputs;
tx.txOutputs = txOutputs;

// Calculate the hash of the Qi tx (message to be signed and verified)
const txHash = getBytes(keccak256(tx.unsignedSerialized));
console.log('\n... transaction to sign: ', JSON.stringify(tx, null, 2));

// Sign the tx
const serializedSignedTx = await qiWallet.signTransaction(tx);

console.log('\n... signed transaction (serialized): ', serializedSignedTx);
// Unmarshall the signed Tx
const signedTx = QiTransaction.from(serializedSignedTx);
const signedTxObj = QiTransaction.from(serializedSignedTx);

// Digest to verify
const txHash = getBytes(signedTxObj.digest);
console.log('\n... txHash to verify: ', signedTxObj.digest);

// Get the signature from the signed tx
const signature = signedTx.signature;
const signature = signedTxObj.signature;

// Remove parity byte from pubkey
publicKey = '0x' + pubkey1.slice(4);
const publicKey = '0x' + pubkey1.slice(4);

// Verify the schnoor signature
const verified = schnorr.verify(getBytes(signature), txHash, getBytes(publicKey));
console.log('Verified:', verified);
console.log('\n=> signature is valid: ', verified);
}

main()
Expand Down
Loading
Loading