You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
✗ Running node buy-item.js should add a transaction at the end of the transactions.json array with the correct buyerAddress, sellerAddress, price, itemBought, and signature properties. Note: You may need to pass the balance check test right below this (test 8) for this to pass
✗ Running node buy-item.js should not add a transaction if that address doesn't have enough funds to buy the item
✗ Running node sell-item.js should add a transaction at the end of the transactions.json array with the correct buyerAddress, sellerAddress, price, itemSold, and signature properties. Note: You may need to pass the item check test right below this (test 10) for this to pass
✓ Running node sell-item.js should not add a transaction if that address doesn't have that item to sell
Code and Screenshots
sell-item.js
import EC from 'elliptic';
// Add your code below
import { writeFileSync, readFileSync } from 'fs';
import sha256 from 'crypto-js/sha256.js';
import {
getAddressItems,
getTransactions,
getItemPrice,
writeTransactions,
getWalletAddressFromName,
} from './blockchain-helpers.js';
const ec = new EC.ec('p192');
const sellerPrivateKey = process.argv[2];
const itemSold = process.argv[3];
const price = getItemPrice(itemSold);
const transactions = getTransactions();
const walletFile = readFileSync('./wallets.json');
const wallets = JSON.parse(walletFile);
const keys = Object.keys(wallets);
function getTransaction(privateKey) {
for (const key of keys) {
const person = wallets[key];
if (privateKey === person.privateKey) {
const fromKeyPair = ec.keyFromPrivate(sellerPrivateKey, 'hex');
const hash = sha256(price + key + itemSold).toString();
const signature = fromKeyPair.sign(hash).toDER('hex');
const newTransaction = {
buyerAddress: 'null',
sellerAddress: person.publicKey,
itemSold,
signature,
};
return newTransaction;
}
}
return null; // Return null if no matching private key is found
}
function getaddress(privateKey) {
for (const key of keys) {
const person = wallets[key];
if (privateKey === person.privateKey) {
//console.log(key.publicKey);
return key;
}
}
}
const newTransaction = getTransaction(sellerPrivateKey);
if (newTransaction !== null) {
const nameOfAddress = getaddress(sellerPrivateKey);
const address = getWalletAddressFromName(nameOfAddress);
const items = getAddressItems(address);
if (items.hasOwnProperty(itemSold)) {
const value = items[itemSold];
if (value > 0) {
transactions.push(newTransaction);
writeTransactions(transactions);
}
else {
console.log('it does not have that item.')
}
}
} else {
console.log('No matching private key found.');
}
buy-item.js
import { writeFileSync, readFileSync } from 'fs';
import sha256 from 'crypto-js/sha256.js';
import {
getAddressBalance,
getTransactions,
getItemPrice,
writeTransactions,
getWalletAddressFromName,
} from './blockchain-helpers.js';
import EC from 'elliptic';
const ec = new EC.ec('p192');
const buyerPrivateKey = process.argv[2];
const itemBought = process.argv[3];
const price = getItemPrice(itemBought);
const transactions = getTransactions();
const walletFile = readFileSync('./wallets.json');
const wallets = JSON.parse(walletFile);
const keys = Object.keys(wallets);
function getTransaction(privateKey) {
for (const key of keys) {
const person = wallets[key];
if (privateKey === person.privateKey) {
const fromKeyPair = ec.keyFromPrivate(buyerPrivateKey, 'hex');
const hash = sha256(price + key + itemBought).toString();
const signature = fromKeyPair.sign(hash).toDER('hex');
const newTransaction = {
buyerAddress: person.publicKey,
sellerAddress: 'null',
itemBought,
signature,
};
return newTransaction;
}
}
return null; // Return null if no matching private key is found
}
function getaddress(privateKey) {
for (const key of keys) {
const person = wallets[key];
if (privateKey === person.privateKey) {
//console.log(key.publicKey);
return key;
}
}
}
const newTransaction = getTransaction(buyerPrivateKey);
if (newTransaction !== null) {
const nameOfAddress = getaddress(buyerPrivateKey);
const address = getWalletAddressFromName(nameOfAddress);
const accountBalance = getAddressBalance(address);
if (accountBalance >= price) {
transactions.push(newTransaction);
writeTransactions(transactions);
}
else {
console.log("You don't have enough accountBalance.")
}
} else {
console.log('No matching private key found.');
}
my logic
with buying and selling is correct as per the given rules
buy's only when the account balance is more than the price of the buying item.
sells only when the account contains that item.
writing correct public address to the transaction.json.
one thing wrong in my code is when selling the item, the selling price should be less than the 5 coins.
i want to know while buying and selling the items, how to append the balance to the wallet address.
The text was updated successfully, but these errors were encountered:
Can you see any hints in the Console? Usually, it provides a lot more feedback about why a test is failing.
i want to know while buying and selling the items, how to append the balance to the wallet address.
On this: Remember, there are blockchain-helper.js functions. There are many functions in there to work with the wallets.
If you have further questions for help, I suggest using https://forum.freecodecamp.org/ instead of a GitHub issue as it is slightly easier to follow a conversation.
Project
build-a-video-game-marketplace-blockchain
Lesson Number
open project(single lesson)
Question
Code and Screenshots
sell-item.js
buy-item.js
my logic
with buying and selling is correct as per the given rules
i want to know while buying and selling the items, how to append the balance to the wallet address.
The text was updated successfully, but these errors were encountered: