Transactions

Fone Network is a transaction-only solution. Transactions are at the heart of the blockchain, allowing users to send the native currency (FONE) from one wallet to another.

Transactions involve the amount of FONE, recipient address, timestamp, and optional message, all digitally signed by the initiator's private key. A fixed fee of 0.15 FONE, known as the "Network Fee," is applied upon sending.

Overview of a Transaction

A transaction involves several key components:

• Sender: The wallet address of the user sending FONE.

• Recipient: The wallet address of the user receiving FONE.

• Amount: The amount of FONE being sent.

• Message: An optional message accompanying the transaction.

• Signature: A digital signature ensuring that the transaction is authentic and authorized by the sender.

• Timestamp: The exact time the transaction is created, ensuring it is valid and not reused.

The Transaction Code

Here’s the code that processes and validates a transaction:

const EC = require('elliptic').ec
const ec = new EC('secp256k1')
const sha256 = require("sha256")
const privateKey, recipient, amount, message

const timestamp = Date.now();  // Generate current timestamp
const wallet = ec.keyFromPrivate(privateKey);  // Get wallet from private key
const fullAddress = wallet.getPublic('hex');  // Full public address of sender
const sender = wallet.getPublic().getX().toString(16);  // Shortened public key (X-coordinate)
const hash = sha256(sender + recipient + amount + message + timestamp).toString();  // Hash the transaction details
const sign = wallet.sign(hash);  // Sign the transaction hash
const signature = sign.toDER('hex');  // Convert the signature to hex format

Step-by-Step Breakdown

1. Extract Input Data:

• The privateKey, recipient, amount, and message are provided by the user initiating the transaction.

2. Generate Timestamp:

• Date.now() generates the current timestamp, which is used to ensure the transaction is recent and valid.

3. Reconstruct Wallet:

• The wallet is reconstructed using the provided privateKey with ec.keyFromPrivate(privateKey). This allows the sender’s public address to be derived from the private key.

• The fullAddress is the complete public key in hexadecimal format, while the sender is the shortened version (the X-coordinate).

4. Create Transaction Hash:

• A hash is created from the transaction details (sender, recipient, amount, message, and timestamp) using the sha256 hashing algorithm. This hash serves as a unique fingerprint for the transaction.

5. Sign the Transaction:

• The transaction hash is signed with the sender’s private key to generate the signature. This signature ensures that only the private key owner (the sender) can authorize this transaction.

Validating the raw Transaction

Before broadcasting the transaction to the network, the system performs several validation steps:

try {
    const signatureIsValid = foneNetwork.transactionSignature(fullAddress, hash, signature);  // Validate signature

    if (!signatureIsValid) {
        return res.json({ note: 'Invalid signature.' });  // Reject invalid signature
    }

    // Additional checks continue below...
} catch (error) {
    res.status(500).json({ note: 'Error processing request.' });
}

1. Signature Validation:

• The system checks the validity of the signature using the foneNetwork.transactionSignature() function. This ensures that the sender’s private key creates the signature and is valid for the transaction.

• If the signature is invalid, the transaction is rejected.

Checking the Balance

Once the signature is validated, the system ensures that the sender has enough FONE to cover the transaction:

try {
    const balanceData = await foneNetwork.getBalance(sender);  // Retrieve sender's balance
    const availableBalance = balanceData.available || 0;  // Check available balance

    if (amount > availableBalance) {
        return res.json({ note: 'Not enough FONE to send.' });  // Reject if insufficient balance
    }

1. Retrieve Balance:

• The system retrieves the sender’s balance using foneNetwork.getBalance(sender). The available balance is the amount of FONE that can be used to send.

2. Check for Sufficient Funds:

• If the amount being sent exceeds the available balance, the transaction is rejected with the message “Not enough FONE to send.”

Broadcasting the Transaction

If the balance is sufficient, the transaction is created and added to the pool of pending transactions:

const newTransaction = foneNetwork.createNewTransaction(
    sender,
    recipient,
    amount,
    hash,
    timestamp,
    message,
    0,  // Transaction type (standard transfer)
    null  // No vesting applied
);

await foneNetwork.addTransactionToPendingTransactions(newTransaction);  // Add transaction to pending pool

1. Create the Transaction:

• A new transaction is created using foneNetwork.createNewTransaction(). This includes all necessary details such as the sender, recipient, amount, and signature hash.

2. Add to Pending Transactions:

• The transaction is added to the pool of pending transactions using foneNetwork.addTransactionToPendingTransactions(newTransaction). The network’s validators will pick it up and confirm it in a new block.

3. Broadcast Confirmation:

• Once added to the pending transactions pool, the system responds with a success message: 'Transaction created and broadcast successfully.'

Summary

• Transaction Hash: Each transaction has a unique hash based on the sender, recipient, amount, message, and timestamp.

• Signature Validation: The transaction must be signed with the sender’s private key and validated before it can be processed.

• Balance Check: The system ensures the sender has enough FONE to cover the transaction.

• Pending Transactions: Once validated, the transaction is added to the pool of pending transactions, where it will be processed by the network.

Last updated