Is there a way to retrieve JSON on Solidity on ERC-721? - ethereum

I'm practicing on an ERC_721 contract and would like to know if there's a way to store JSON data directly on the contract, take a look at my approach and give your thoughts:
The variation1.json file uploaded to IPFS:
{
"attributes": [
{
"trait_type": "Rank",
"value": "1"
}
],
"description": "A simple NFT",
"image": "Qmdasifhw89rv92enfkq128re3",
"name": "NFT#1"
}
Then I would use an object like the one below to go with the contract that references the file to the urlOfTokenURI
[
{
"file": "variation1.json", "urlOfTokenURI": https://gateway.pinata.cloud/ipfs/Qmdasifhw89rv92enfkqeqe23f"
},
{
"file": "variation2.json", "urlOfTokenURI": "https://gateway.pinata.cloud/ipfs/Qmgvfrg34vt4u785ygbmasdsa"
},
...
]
Then get a random number, retrieve a random urlOfTokenURI and mint the token to the desired address, excluding that token from the list of available tokens:
function mintNFT(address recipient, string memory tokenURI)
public onlyOwner
returns (uint256)
{
_tokenIds.increment();
//retrieve object, get random number and mint with the urlOfTokenURI
//from random number index
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
What's missing here for this to work?
Can someone guide me how to proceed or have a suggestion for another approach? I'm kind of lost.
Don't understand how to mint a random NFT and instantly revealing it to the user.
Sorry in advance if it's a stupid question. I'm learning, btw.
Appreciate the help!

If you need a real random number fn, you need to look for oracle service (e.g. Chainlink)
Yes, it is possible to generate an uri without publishing to IPFS. You can generate a base64 data uri instead. Check out Example from wizard and dragon game and their contract
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/utils/Strings.sol";
...
using Strings for uint256;
using Base64 for bytes;
...
function tokenURI(uint256 id) public view returns (string memory) {
string memory metadata = string(abi.encodePacked(
"{\"name\": \"NFT#",
id.toString(),
"\", \"description\": \"A simple NFT\", \"attributes\":",
"[{\"key\":\"trait_type\",\"value\":\"1\"}]",
"}"
));
return string(abi.encodePacked(
"data:application/json;base64,",
bytes(metadata).base64()
));
}
Base64 lib
pragma solidity ^0.8.0;
library Base64 {
string internal constant TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function base64(bytes memory data) internal pure returns (string memory) {
if (data.length == 0) return "";
// load the table into memory
string memory table = TABLE;
// multiply by 4/3 rounded up
uint256 encodedLen = 4 * ((data.length + 2) / 3);
// add some extra buffer at the end required for the writing
string memory result = new string(encodedLen + 32);
assembly {
// set the actual output length
mstore(result, encodedLen)
// prepare the lookup table
let tablePtr := add(table, 1)
// input ptr
let dataPtr := data
let endPtr := add(dataPtr, mload(data))
// result ptr, jump over length
let resultPtr := add(result, 32)
// run over the input, 3 bytes at a time
for {} lt(dataPtr, endPtr) {}
{
dataPtr := add(dataPtr, 3)
// read 3 bytes
let input := mload(dataPtr)
// write 4 characters
mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr(18, input), 0x3F)))))
resultPtr := add(resultPtr, 1)
mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr(12, input), 0x3F)))))
resultPtr := add(resultPtr, 1)
mstore(resultPtr, shl(248, mload(add(tablePtr, and(shr( 6, input), 0x3F)))))
resultPtr := add(resultPtr, 1)
mstore(resultPtr, shl(248, mload(add(tablePtr, and( input, 0x3F)))))
resultPtr := add(resultPtr, 1)
}
// padding with '='
switch mod(mload(data), 3)
case 1 { mstore(sub(resultPtr, 2), shl(240, 0x3d3d)) }
case 2 { mstore(sub(resultPtr, 1), shl(248, 0x3d)) }
}
return result;
}
}

Related

How to concatenate 2 String in Solidity and encode Keccak256?

i'm working at a smart-contract that has a merkle-tree function in it.
Basically i'm passing the proof from a firebase function that take in input the address and the n° of token reserved by the user, and gives as output the proof to the front-end that execute the smart-contract function.
The only thing i need to figure out is the leaf calculation, on the firebase function it is composed by encoding keccak256 the string in the array, composed by an eth address concatenated with the number of reserved tokens (0x38207d0e84edb04a57482c3769fe192617520373 + 3), in the smart contract instead, the leaf is calculated with the keccak256(abi.encodepacked(0x38207d0e84edb04a57482c3769fe1926175203733, 3), and it's not passing the verifiy...
I think these two methods of calculation are different in some part, can you help me?
I'm going to paste the code of both functions:
SOLIDITY
// 1. Address check, if the proof is valid
function isPrivateListed(
uint256 _addressReservedTokens,
bytes32[] memory _proof
) public view returns (bool) {
bytes32 leaf = keccak256(abi.encodePacked(msg.sender, _addressReservedTokens);
return MerkleProof.verify(_proof, privateListRoot, leaf);
}
// 2. Mint function called from the front-end
function privateMint(
uint256 _mintAmount,
uint256 _addressReservedTokens,
bytes32[] calldata proof
) public payable {
require(privateOpen, "The private mint is not opened");
require(
isPrivateListed(_addressReservedTokens, proof),
"you are not in the private list"
);
uint256 supply = totalSupply();
require(supply + _mintAmount <= maxSupply, "max NFT limit exceeded");
uint256 owned = addressMintedBalance[msg.sender];
require(
owned + _mintAmount <= _addressReservedTokens,
"You have less nft reserved"
);
for (uint256 i = 1; i <= _mintAmount; i++) {
_safeMint(msg.sender, supply + i);
addressMintedBalance[msg.sender]++;
}
}
FIREBASE FUNCTION
// 1. Get parameters from the function called from client side
const walletAddress = data.walletAddress;
let whitelistAddresses = [
'0x38207d0e84edb04a57482c3769fe1926175203733',
'0x943926a8ff0000350d0b879a658fa52bcd4fca186',
];
// 2. Create a new array of `leafNodes` by hashing all indexes of the `whitelistAddresses`
// using `keccak256`. Then creates a Merkle Tree object using keccak256 as the algorithm.
const leafNodes = whitelistAddresses.map((addr) => keccak256(addr));
const merkleTree = new MerkleTree(leafNodes, keccak256, {
sortPairs: true,
});
// 3. Get root hash of the `merkleeTree` in hexadecimal format (0x)
const rootHash = merkleTree.getRoot();
const rootHashHex = merkleTree.getHexRoot();
// ***** ***** ***** CLIENT-SIDE ***** ***** ***** //
const claimingAddress = keccak256(walletAddress);
const hexProof = merkleTree.getHexProof(claimingAddress);
const verifyProof = merkleTree.verify(hexProof, claimingAddress, rootHash);
In solidity you're hashing a (20+32)-bytes element
bytes32 leaf = keccak256(abi.encodePacked(msg.sender, _addressReservedTokens);
Since msg.sender is 20 bytes and _addressReservedTokens is 32 bytes, the output of abi.encodePacked() is something like this:
0x38207d0e84edb04a57482c3769fe1926175203730000000000000000000000000000000000000000000000000000000000000003
This is clearly different than 0x38207d0e84edb04a57482c3769fe1926175203733 used by firebase.
Also I'm not sure if the js keccak256 is interpreting that value as string or as bytes (in solidity it's bytes).
The best fix is probably changing the js code. You can write the correct whitelistAddresses using ethers, see here https://ethereum.stackexchange.com/questions/119990/how-to-mimic-abi-encodepacked-in-ethers

How to pass in arguments to a contract's constructor for initializing characters images and attributes

I have a contract like this that has a constructor like this:
constructor(
string[] memory characterNames,
string[] memory characterImageURIs,
uint256[] memory characterHp,
uint256[] memory characterAttackDmg,
string memory bossName,
string memory bossImageURI,
uint256 bossHp,
uint256 bossAttackDamage
) ERC721("Heroes", "HERO") {
for (uint256 i = 0; i < characterNames.length; i += 1) {
defaultCharacters.push(
CharacterAttributes({
characterIndex: i,
name: characterNames[i],
imageURI: characterImageURIs[i],
hp: characterHp[i],
maxHp: characterHp[i],
attackDamage: characterAttackDmg[i]
})
);
CharacterAttributes memory c = defaultCharacters[i];
console.log(
"Done initializing %s w/ HP %s, img %s",
c.name,
c.hp,
c.imageURI
);
}
bigBoss = BigBoss({
name: bossName,
imageURI: bossImageURI,
hp: bossHp,
maxHp: bossHp,
attackDamage: bossAttackDamage
});
console.log(
"Done initializing boss %s w/ HP %s, img %s",
bigBoss.name,
bigBoss.hp,
bigBoss.imageURI
);
_tokenIds.increment();
}
Question
for the characters names and HP and Attack... how should I type them is it in a JSON or in arrays or what?
for the imageURI how can I parse them to the constructor and where as I'm using Pinata IPFS to host my images?
Please if you can help me with this.
1- type for constructor arguments are already defined:
string[] = array of strings
string =string
uint (uint256) = 256 bits unsigned number ranging from 0 to 2²⁵⁶
2- constructor arguments mean, when you create the contract, you have to pass those parameters inorder to initialize the contracts. So you should already have them in hand.
you store the metadata in ipfs: an example would be like this:
{
"name": "name",
"description": "descruption",
"image": "ipfs://cid/name.jpeg"
}
So when you work in front-end, you make a request to ipfs Uri, to get the metadata:
// make a request with axios library
// this will store the above json data inside `metaData`.data
const metaData = await axios.get(tokenUri)
const data=metaData.data
From metadata, you get the imageUri as data.image

Adding new custom opcode to solidity

I have a problem with adding new opcode to solidity. I'm using solc (on C++) and geth(ethereum on Go). I want to add new opcode, that takes address payable, uint256, uint256, bytes memory and returns bytes memory. So I have a problem with return value.
Some peaces of code below, I will skip some files, to make question shorter.
Solc
libsolidity/codegen/ExpressionCompiler.cpp
// ExpressionCompiler::visit(FunctionCall const& _functionCall)
case FunctionType::Kind::MyOpcode:
{
acceptAndConvert(*arguments[0], *function.parameterTypes()[0], true);
acceptAndConvert(*arguments[1], *function.parameterTypes()[1], true);
acceptAndConvert(*arguments[2], *function.parameterTypes()[2], true);
arguments[3]->accept(*this);
utils().fetchFreeMemoryPointer();
utils().packedEncode(
{arguments[3]->annotation().type},
{TypeProvider::array(DataLocation::Memory, true)}
);
utils().toSizeAfterFreeMemoryPointer();
m_context << Instruction::MYOPCODE;
}
libsolidity/analysis/GlobalContext.cpp
// inline vector<shared_ptr<MagicVariableDeclaration const>> constructMagicVariables()
magicVarDecl("myopcode", TypeProvider::function(strings{"address payable", "uint256", "uint256", "bytes memory"}, strings{"bytes memory"}, FunctionType::Kind::MyOpcode, false, StateMutability::Payable)),
libevmasm/Instruction.cpp
// static std::map<Instruction, InstructionInfo> const c_instructionInfo =
{ Instruction::MYOPCODE, { "MYOPCODE", 0, 5, 1, true, Tier::Base } }
Geth
core/vm/jump_table.go
// func newFrontierInstructionSet() JumpTable {
CALLACTOR: {
execute: opMyOpCode,
dynamicGas: gasCallActor,
minStack: minStack(5, 1),
maxStack: maxStack(5, 1),
memorySize: memoryReturn,
writes: true,
returns: true,
},
core/vm/instructions.go
func opMyOpcode(pc *uint64, interpreter *EVMInterpreter, callContext *callCtx) ([]byte, error) {
inoffset, insize := callContext.stack.pop(), callContext.stack.pop()
params := callContext.memory.GetPtr(int64(inoffset.Uint64()), int64(insize.Uint64()))
secondValue := callContext.stack.pop()
firstValue := callContext.stack.pop()
addr := callContext.stack.pop()
// ... Do smth with input ...
outoffset := inoffset.Uint64() + insize.Uint64()
callContext.memory.Set(outoffset, 1, []byte{0x2})
tmp := make([]byte, 1)
tmp[0] = 0x98
callContext.memory.Set(outoffset + 1, 1, tmp)
callContext.stack.push(uint256.NewInt().SetUint64(outoffset))
return tmp, nil
}
Smart contract
pragma solidity >=0.6.0; // SPDX-License-Identifier: GPL-3
contract test {
event ReturnValue(address payable _from, bytes data);
function f() public returns(bytes memory){
address payable addr1 = payable(msg.sender);
bytes memory params = new bytes(2);
params[0] = 0x21;
params[1] = 0x22;
bytes memory result = myopcode(addr1, 0x11, 0x12, params);
emit ReturnValue(addr1, result);
return result;
}
}
When I run that code I get invalid jump destination. So, what I need to do, to get my code work correctly?
I found a solution, it certainly looks stupid, but this case is already provided by the developers. All you need is utils().returnDataToArray().

Solidity: Error when deploying the voting tutorial; problem with constructor and array?

I tried to reproduce the voting tutorial from the solidity homepage [https://solidity.readthedocs.io/en/v0.5.11/solidity-by-example.html#voting]. I encountered the following problem: The code can be compiled withouth an error, but when i want to "deploy" it in the JVM i always get errors. I think the problem is connected to the constructor who expects a bytes32 array.
Here are the error messages, depending on what parameter i used with the "Deploy" button in remix.
a) "Name1", "Name2"
--> creation of Ballot errored: Error encoding arguments: Error: types/values length mismatch (count={"types":1,"values":2},
value={"types":["bytes32[]"],"values":["Name1","Name2"]},
version=4.0.36)
b) ["Name1", "Name2"]
--> creation of Ballot errored: Error encoding arguments: Error: invalid bytes32 value (arg="", coderType="bytes32", value="Name1",
version=4.0.36)
c) "Name1"
--> creation of Ballot errored: Error encoding arguments: Error: expected array value (arg="", coderType="array", value="Name1",
version=4.0.36)
d) ["Name2"]
--> creation of Ballot errored: Error encoding arguments: Error: invalid bytes32 value (arg="", coderType="bytes32", value="Name2",
version=4.0.36)
e) Name3
--> creation of Ballot errored: Error encoding arguments: SyntaxError: Unexpected token N in JSON at position 1
f) 'Name3'
--> creation of Ballot errored: Error encoding arguments: SyntaxError: Unexpected token ' in JSON at position 1
g) ['Name4']
-->creation of Ballot errored: Error encoding arguments: SyntaxError: Unexpected token ' in JSON at position 2
pragma solidity >=0.4.22 <0.7.0;
/// #title Voting with delegation.
contract Ballot {
// This declares a new complex type which will
// be used for variables later.
// It will represent a single voter.
struct Voter {
uint weight; // weight is accumulated by delegation
bool voted; // if true, that person already voted
address delegate; // person delegated to
uint vote; // index of the voted proposal
}
// This is a type for a single proposal.
struct Proposal {
bytes32 name; // short name (up to 32 bytes)
uint voteCount; // number of accumulated votes
}
address public chairperson;
// This declares a state variable that
// stores a `Voter` struct for each possible address.
mapping(address => Voter) public voters;
// A dynamically-sized array of `Proposal` structs.
Proposal[] public proposals;
/// Create a new ballot to choose one of `proposalNames`.
constructor(bytes32[] memory proposalNames) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;
// For each of the provided proposal names,
// create a new proposal object and add it
// to the end of the array.
for (uint i = 0; i < proposalNames.length; i++) {
// `Proposal({...})` creates a temporary
// Proposal object and `proposals.push(...)`
// appends it to the end of `proposals`.
proposals.push(Proposal({
name: proposalNames[i],
voteCount: 0
}));
}
}
// Give `voter` the right to vote on this ballot.
// May only be called by `chairperson`.
function giveRightToVote(address voter) public {
// If the first argument of `require` evaluates
// to `false`, execution terminates and all
// changes to the state and to Ether balances
// are reverted.
// This used to consume all gas in old EVM versions, but
// not anymore.
// It is often a good idea to use `require` to check if
// functions are called correctly.
// As a second argument, you can also provide an
// explanation about what went wrong.
require(
msg.sender == chairperson,
"Only chairperson can give right to vote."
);
require(
!voters[voter].voted,
"The voter already voted."
);
require(voters[voter].weight == 0);
voters[voter].weight = 1;
}
/// Delegate your vote to the voter `to`.
function delegate(address to) public {
// assigns reference
Voter storage sender = voters[msg.sender];
require(!sender.voted, "You already voted.");
require(to != msg.sender, "Self-delegation is disallowed.");
// Forward the delegation as long as
// `to` also delegated.
// In general, such loops are very dangerous,
// because if they run too long, they might
// need more gas than is available in a block.
// In this case, the delegation will not be executed,
// but in other situations, such loops might
// cause a contract to get "stuck" completely.
while (voters[to].delegate != address(0)) {
to = voters[to].delegate;
// We found a loop in the delegation, not allowed.
require(to != msg.sender, "Found loop in delegation.");
}
// Since `sender` is a reference, this
// modifies `voters[msg.sender].voted`
sender.voted = true;
sender.delegate = to;
Voter storage delegate_ = voters[to];
if (delegate_.voted) {
// If the delegate already voted,
// directly add to the number of votes
proposals[delegate_.vote].voteCount += sender.weight;
} else {
// If the delegate did not vote yet,
// add to her weight.
delegate_.weight += sender.weight;
}
}
/// Give your vote (including votes delegated to you)
/// to proposal `proposals[proposal].name`.
function vote(uint proposal) public {
Voter storage sender = voters[msg.sender];
require(sender.weight != 0, "Has no right to vote");
require(!sender.voted, "Already voted.");
sender.voted = true;
sender.vote = proposal;
// If `proposal` is out of the range of the array,
// this will throw automatically and revert all
// changes.
proposals[proposal].voteCount += sender.weight;
}
/// #dev Computes the winning proposal taking all
/// previous votes into account.
function winningProposal() public view
returns (uint winningProposal_)
{
uint winningVoteCount = 0;
for (uint p = 0; p < proposals.length; p++) {
if (proposals[p].voteCount > winningVoteCount) {
winningVoteCount = proposals[p].voteCount;
winningProposal_ = p;
}
}
}
// Calls winningProposal() function to get the index
// of the winner contained in the proposals array and then
// returns the name of the winner
function winnerName() public view
returns (bytes32 winnerName_)
{
winnerName_ = proposals[winningProposal()].name;
}
}
You can't use strings since the function is expecting an array composed of bytes32 values. Link posted by #Ameesha (https://ethereum.stackexchange.com/questions/50310/how-to-pass-the-value-in-bytes32-array) shows how to encode the strings to bytes32. Additionally, I learned that passing values in single quotes is wrong, and not enclosing values in square brackets is also wrong... the correct way is to use double quotes around each hex value and use square brackets to represent as a list. So, for instance, "Proposal A", "Proposal B", "Proposal C" encodes to:
["0x50726f706f73616c204100000000000000000000000000000000000000000000",
"0x50726f706f73616c204200000000000000000000000000000000000000000000",
"0x50726f706f73616c204300000000000000000000000000000000000000000000"]
Pass the argument of []address as : [ "address","address1" ] instead of [address,address2].

solidity - get return value of delegatecall with assembly

I have a contract A and a contract B.
Contract A declares this function:
function getIntValue() constant returns (uint);
What would be the appropriate assembly code to delegatecall contract A's getIntValue function from B? I'm not yet very experienced with assembly so I only have this so far which doesn't work:
function getContractAIntValue() constant returns (uint c) {
address addr = address(contractA); // contract A is stored in B.
bytes4 sig = bytes4(sha3("getIntValue()")); // function signature
assembly {
let x := mload(0x40) // find empty storage location using "free memory pointer"
mstore(x,sig) // attach function signature
let status := delegatecall(sub(gas, 10000), addr, add(x, 0x04), 0, x, 0x20)
jumpi(invalidJumpLabel, iszero(status)) // error out if unsuccessful delegatecall
c := mload(x)
}
}
Maybe you have solved it cause was asked more than one year ago, but in case some is still looking for it...
address addr = address(contractA); // contract A is stored in B.
bytes memory sig = abi.encodeWithSignature("getIntValue()"); // function signature
// solium-disable-next-line security/no-inline-assembly
assembly {
let result := delegatecall(sub(gas, 10000), addr, add(sig, 0x20), mload(sig), 0, 0)
let size := returndatasize
let ptr := mload(0x40)
returndatacopy(ptr, 0, size)
// revert instead of invalid() bc if the underlying call failed with invalid() it already wasted gas.
// if the call returned error data, forward it
switch result case 0 { revert(ptr, size) }
default { return(ptr, size) }
}