Skip to content

Commit

Permalink
fix: rework examples (#2646)
Browse files Browse the repository at this point in the history
* fix: rework examples

Signed-off-by: Ivaylo Nikolov <[email protected]>

* style: lint issues

Signed-off-by: Ivaylo Nikolov <[email protected]>

* style: update token-reject format

Signed-off-by: Ivaylo Nikolov <[email protected]>

---------

Signed-off-by: Ivaylo Nikolov <[email protected]>
  • Loading branch information
ivaylonikolov7 authored Dec 2, 2024
1 parent 32ab352 commit cba5d67
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 133 deletions.
2 changes: 1 addition & 1 deletion examples/create-account-with-thresholdkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function main() {
const client = Client.forName(process.env.HEDERA_NETWORK);

client.setOperator(operatorId, operatorKey);

// Set logger
const infoLogger = new Logger(LogLevel.Info);
client.setLogger(infoLogger);
Expand Down
43 changes: 10 additions & 33 deletions examples/multi-node-multi-signature-remove.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/*
import {
Client,
PrivateKey,
Expand All @@ -21,14 +20,14 @@ let bobKey;
* @description Create a transaction with multiple nodes and multiple signatures
* and remove one of the signatures from the transaction then add it back
*/
/*

async function main() {
/**
*
* Step 1: Create Client
*
*/
/*

if (
process.env.OPERATOR_ID == null ||
process.env.OPERATOR_KEY == null ||
Expand All @@ -49,7 +48,7 @@ async function main() {
* Step 2: Create keys for two users
*
*/
/*

aliceKey = PrivateKey.generate();
bobKey = PrivateKey.generate();

Expand All @@ -60,7 +59,7 @@ async function main() {
* Step 3: Create an account with the keyList
*
*/
/*

const createAccountTransaction = new AccountCreateTransaction()
.setInitialBalance(new Hbar(2))
.setKey(keyList);
Expand All @@ -73,7 +72,7 @@ async function main() {
* Step 4: Create a transfer transaction with multiple nodes
*
*/
/*

const transferTransaction = new TransferTransaction()
.addHbarTransfer(createReceipt.accountId, new Hbar(-1))
.addHbarTransfer("0.0.3", new Hbar(1))
Expand All @@ -91,7 +90,7 @@ async function main() {
* & Collect multiple signatures (Uint8Array[]) from one key
*
*/
/*

const transferTransactionBytes = transferTransaction.toBytes();

const aliceSignatures = aliceKey.signTransaction(transferTransaction);
Expand All @@ -103,7 +102,6 @@ async function main() {
* & Add the previously collected signatures
*
*/
/*
const signedTransaction = Transaction.fromBytes(transferTransactionBytes);

signedTransaction.addSignature(aliceKey.publicKey, aliceSignatures);
Expand All @@ -115,13 +113,15 @@ async function main() {
console.log(
"Alice Signatures =>",
aliceSignatures.map((aliceSig) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
PrivateKey.fromBytes(aliceSig).toStringDer(),
),
);

console.log(
"Bob Signatures =>",
bobSignatures.map((bobSig) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
PrivateKey.fromBytes(bobSig).toStringDer(),
),
);
Expand All @@ -138,14 +138,13 @@ async function main() {
* Step 7: Remove the signatures for Alice from the transaction
*
*/
/*
const removedAliceSignatures = signedTransaction.removeSignature(
aliceKey.publicKey,
);

console.log("\nREMOVED Alice signatures below: \n");

if (Array.isArray(aliceSignatures) && Array.isArray(bobSignatures)) {
if (Array.isArray(removedAliceSignatures)) {
console.log(
"Alice removed signatures =>",
removedAliceSignatures.map((aliceSig) =>
Expand All @@ -160,26 +159,6 @@ async function main() {
console.log("\n\nSignatures left in the transaction: ");
console.log(signaturesInTheTransactionAfter);

/**
*
* Step 8: Add the removed signature back to the transaction
*
*/
/*
signedTransaction.addSignature(aliceKey.publicKey, removedAliceSignatures);
/**
*
* Step 9: Execute and take the receipt
*
*/
/*
const result = await signedTransaction.execute(client);
const receipt = await result.getReceipt(client);
console.log(`\n \nTransaction status: ${receipt.status.toString()}`);
client.close();
}

Expand All @@ -190,10 +169,9 @@ void main();
* @param {Transaction} signedTransaction - The signed transaction object containing the list of signed transactions.
* @returns {string[]} An array of signatures in DER format.
*/
/*
const getAllSignaturesFromTransaction = (signedTransaction) => {
/** @type {string[]} */
/*

const signatures = [];

signedTransaction._signedTransactions.list.forEach((transaction) => {
Expand All @@ -218,4 +196,3 @@ const getAllSignaturesFromTransaction = (signedTransaction) => {

return signatures;
};
*/
47 changes: 12 additions & 35 deletions examples/multi-node-multi-signature-removeAll.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/* eslint-disable ie11/no-loop-func */
import {
Client,
PrivateKey,
Expand All @@ -17,18 +17,17 @@ dotenv.config();
let aliceKey;
let bobKey;

const NODES = [new AccountId(3), new AccountId(4), new AccountId(5)];
/**
* @description Create a transaction with multiple nodes and multiple signatures
* and remove all of the signatures from the transaction
*/
/*
async function main() {
/**
*
* Step 1: Create Client
*
*/
/*
if (
process.env.OPERATOR_ID == null ||
process.env.OPERATOR_KEY == null ||
Expand All @@ -49,7 +48,6 @@ async function main() {
* Step 2: Create keys for two users
*
*/
/*
aliceKey = PrivateKey.generate();
bobKey = PrivateKey.generate();

Expand All @@ -63,7 +61,6 @@ async function main() {
* Step 3: Create an account with the keyList
*
*/
/*
const createAccountTransaction = new AccountCreateTransaction()
.setInitialBalance(new Hbar(2))
.setKey(keyList);
Expand All @@ -76,16 +73,11 @@ async function main() {
* Step 4: Create a transfer transaction with multiple nodes
*
*/
/*
const transferTransaction = new TransferTransaction()
.addHbarTransfer(createReceipt.accountId, new Hbar(-1))
.addHbarTransfer("0.0.3", new Hbar(1))
// Set multiple nodes
.setNodeAccountIds([
new AccountId(3),
new AccountId(4),
new AccountId(5),
])
.setNodeAccountIds(NODES)
.freezeWith(client);

/**
Expand All @@ -94,7 +86,6 @@ async function main() {
* & Collect multiple signatures (Uint8Array[]) from one key
*
*/
/*
const transferTransactionBytes = transferTransaction.toBytes();

const aliceSignatures = aliceKey.signTransaction(transferTransaction);
Expand All @@ -106,7 +97,6 @@ async function main() {
* & Add the previously collected signatures
*
*/
/*
const signedTransaction = Transaction.fromBytes(transferTransactionBytes);

signedTransaction.addSignature(aliceKey.publicKey, aliceSignatures);
Expand All @@ -115,19 +105,22 @@ async function main() {
console.log("\nADDED users signatures below: \n");

if (Array.isArray(aliceSignatures) && Array.isArray(bobSignatures)) {
console.log(
/*console.log(
"Alice Signatures =>",
aliceSignatures.map((aliceSig) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
PrivateKey.fromBytes(aliceSig).toStringDer(),
),
);
),
);*/
/*
console.log(
"Bob Signatures =>",
bobSignatures.map((bobSig) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
PrivateKey.fromBytes(bobSig).toStringDer(),
),
);
*/
}

const signaturesInTheTransactionBefore =
Expand All @@ -141,7 +134,6 @@ async function main() {
* Step 7: Remove all signatures from the transaction and add them back
*
*/
/*
const allSignaturesRemoved = signedTransaction.removeAllSignatures();

const signaturesInTheTransactionAfter =
Expand All @@ -162,21 +154,7 @@ async function main() {
}),
);
}
// Add the removed signatures back
signedTransaction.addSignature(publicKey, signatures);
}
/**
*
* Step 8: Execute and take the receipt
*
*/
/*
const result = await signedTransaction.execute(client);
const receipt = await result.getReceipt(client);
console.log(`\n \nTransaction status: ${receipt.status.toString()}`);

client.close();
}
Expand All @@ -188,10 +166,10 @@ void main();
* @param {Transaction} signedTransaction - The signed transaction object containing the list of signed transactions.
* @returns {string[]} An array of signatures in DER format.
*/
/*

const getAllSignaturesFromTransaction = (signedTransaction) => {
/** @type {string[]} */
/*

const signatures = [];

signedTransaction._signedTransactions.list.forEach((transaction) => {
Expand All @@ -216,4 +194,3 @@ const getAllSignaturesFromTransaction = (signedTransaction) => {

return signatures;
};
*/
Loading

0 comments on commit cba5d67

Please sign in to comment.