https://github.com/zhuowei/nft_ptr Skip to content Sign up Sign up * Why GitHub? Features - + Mobile - + Actions - + Codespaces - + Packages - + Security - + Code review - + Project management - + Integrations - + GitHub Sponsors - + Customer stories- * Team * Enterprise * Explore + Explore GitHub - Learn and contribute + Topics - + Collections - + Trending - + Learning Lab - + Open source guides - Connect with others + The ReadME Project - + Events - + Community forum - + GitHub Education - + GitHub Stars program - * Marketplace * Pricing Plans - + Compare plans - + Contact Sales - + Education - [ ] [search-key] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up Sign up {{ message }} zhuowei / nft_ptr * Notifications * Star 800 * Fork 9 C++ `std::unique_ptr` that represents each object as an NFT on the Ethereum blockchain worthdoingbadly.com 800 stars 9 forks Star Notifications * Code * Issues 5 * Pull requests 1 * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Security * Insights main Switch branches/tags [ ] Branches Tags Nothing to show {{ refName }} default View all branches Nothing to show {{ refName }} default View all tags 2 branches 0 tags Go to file Code Clone HTTPS GitHub CLI [https://github.com/z] Use Git or checkout with SVN using the web URL. [gh repo clone zhuowe] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching Xcode If nothing happens, download Xcode and try again. Go back Launching Visual Studio If nothing happens, download the GitHub extension for Visual Studio and try again. Go back Latest commit @zhuowei zhuowei Update README ... 5df5c6c Apr 11, 2021 Update README If I'm going to sell my commits as NFTs I need to make as many as possible 5df5c6c Git stats * 45 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github/workflows Add Github Actions badge Apr 10, 2021 contracts Implement mintOrMove function in NftPtrToken Apr 3, 2021 example Update README, fix build on Linux Apr 10, 2021 impl Fix Clippy warnings Apr 9, 2021 include Create a simpler example Apr 6, 2021 test Initial commit Mar 27, 2021 .clang-format Create header and test linking Rust staticlib into C++ program Mar 27, 2021 .gitignore Deploy contract for each nft_ptr instance Apr 1, 2021 README.md Update README Apr 11, 2021 ci_build.sh Update README and build script Apr 10, 2021 format.sh Create header and test linking Rust staticlib into C++ program Mar 27, 2021 white_paper.pdf Add logs for short and long examples; add white paper Apr 10, 2021 View code Example: moving between two nft_ptrs Example: constructing an nft_ptr and minting an NFT More examples Why? Performance What works Future steps Obligatory system diagrams Sponsor development What I learned Building Testing (local blockchain) Testing (Gorli testnet) Testing (Gorli testnet + local lite node) README.md C++ std::unique_ptr that represents each object as an NFT on the Ethereum blockchain. Build nft_ptr Follow us: worthdoingbadly.com | @zhuowei | @zhuowei@notnow.dev Example: moving between two nft_ptrs auto ptr1 = make_nft(); nft_ptr ptr2; ptr2 = std::move(ptr1); This transfers the Non-Fungible Token 0x7faa4bc09c90, representing the Cow's memory address, from ptr1 (OpenSea, Etherscan) to ptr2 ( OpenSea, Etherscan). screenshot of OpenSea Trading History showing token transfer [2021-04-09T01:59:48Z INFO nft_ptr_lib] Transferring 0x7faa4bc09c90 (Cow) to 0x7ffee35a7890 (0x1564b0a7c258fc88a96aa9fe1c513101883abb13) from 0x7ffee35a78a8 (0x9ed6006c6f3bb20737bdbe88cc6aa0de00597fef) at PC=0x10c65a946 (main (example.cpp:33)) [2021-04-09T02:00:15Z INFO nft_ptr_lib] Transaction: 0xcbe06fdd54bd9d221993c875022fe2960128874811a25075d692cc638a28f290 [2021-04-09T02:00:15Z INFO nft_ptr_lib] https://testnets.opensea.io/assets/goerli/0x90eaf0ab2c6455a9b794f9dcf97839fa25b4ce2d/0x7faa4bc09c90 After the transfer, ptr1 is set to null, and ptr2 contains the new object, just like std::unique_ptr: std::cout << "Moved: ptr1 = " << ptr1.get() << " ptr2 = " << ptr2.get() << std::endl; ptr2->MakeNoise(); Moved: ptr1 = 0x0 ptr2 = 0x7faa4bc09c90 Moo! Example: constructing an nft_ptr and minting an NFT auto ptr1 = make_nft(); This: * initializes the nft_ptr runtime * creates the first nft_ptr * transfers ownership of the newly created Cow* to the nft_ptr First, it creates an ERC-721 smart contract that represents each memory address as a Non-Fungible Token. [2021-04-09T01:57:48Z INFO nft_ptr_lib] Connected to network id 5 [2021-04-09T01:57:48Z INFO nft_ptr_lib] Account: 0xd54b39c6bb7774aba2be4b49dc2667332b737909 [2021-04-09T01:57:48Z INFO nft_ptr_lib] https://goerli.etherscan.io/address/0xd54b39c6bb7774aba2be4b49dc2667332b737909 [2021-04-09T01:57:48Z INFO nft_ptr_lib] Deploying NFT contract! [2021-04-09T01:58:18Z INFO nft_ptr_lib] Token contract deployed at 0x90eaf0ab2c6455a9b794f9dcf97839fa25b4ce2d [2021-04-09T01:58:18Z INFO nft_ptr_lib] https://goerli.etherscan.io/token/0x90eaf0ab2c6455a9b794f9dcf97839fa25b4ce2d Next, it creates another smart contract, that represents the nft_ptr instance which can own NftPtr tokens: [2021-04-09T01:58:18Z INFO nft_ptr_lib] Deploying contract for nft_ptr 7ffee35a78a8 Cow main (example.cpp:25) [2021-04-09T01:58:48Z INFO nft_ptr_lib] Deployed contract for nft_ptr 7ffee35a78a8 Cow main (example.cpp:25) at 0x9ed6006c6f3bb20737bdbe88cc6aa0de00597fef [2021-04-09T01:58:48Z INFO nft_ptr_lib] https://goerli.etherscan.io/token/0x9ed6006c6f3bb20737bdbe88cc6aa0de00597fef Finally, it calls new Cow(), and mints an NFT for this memory address, owned by the new nft_ptr. [2021-04-09T01:58:48Z INFO nft_ptr_lib] Transferring 0x7faa4bc09c90 (Cow) to 0x7ffee35a78a8 (0x9ed6006c6f3bb20737bdbe88cc6aa0de00597fef) from 0x0 (0xd54b39c6bb7774aba2be4b49dc2667332b737909) at PC=0x10c65a76f (main (example.cpp:25)) [2021-04-09T01:59:18Z INFO nft_ptr_lib] Transaction: 0x0a148cee1abe8d4b5721996ea3a107c87b526ded155dc2e3895f1f42983bd2e8 [2021-04-09T01:59:18Z INFO nft_ptr_lib] https://testnets.opensea.io/assets/goerli/0x90eaf0ab2c6455a9b794f9dcf97839fa25b4ce2d/0x7faa4bc09c90 More examples A full example program can be found at example/example.cpp, along with a sample of its output when run. A longer example, which shows using nft_ptr with function calls and STL containers, can be found at example/long_example.cpp along with its output. Why? * Biggest issue facing $125 billion security industry: Memory safety. + "~70% of the vulnerabilities addressed through a security update each year continue to be memory safety issues." - Microsoft Security Response Center * The world's largest codebases are written in C++ + Browsers, operating systems, databases, financial systems * C++ memory management is hard to understand, opaque, and not secure * As we all know, adding blockchain to a problem automatically makes it simple, transparent, and cryptographically secure. * Thus, we extend std::unique_ptr, the most popular C++ smart pointer used for memory management, with blockchain support * Non-Fungible Tokens and std::unique_ptr have the exact same semantics: + each token/object is unique, not fungible with other tokens/ objects + each token/object is owned by one owner/unique_ptr + others may view the NFT/use the object, but only the owner can transfer/destroy the NFT/object. + absolutely no protection against just pirating the image represented by the NFT/copying the pointer out of the unique_ptr * Written in Rust for the hipster cred. * Made with by a Blockchain Expert who wrote like 100 lines of Solidity in 2017 (which didn't work) For more information, please read our white paper. Performance nft_ptr has negligible performance overhead compared to std::unique_ptr, as shown by this benchmark on our example program: Implementation Runtime std::unique_ptr 0.005 seconds nft_ptr 3 minutes What works * Deploying ERC-721 smart contract on program start * Create smart contract for each nft_ptr instance * Call smart contract to create token when a pointer is transferred into an nft_ptr * Transfer token when pointer moved between nft_ptrs Future steps nft_ptr instances are themselves ERC-20 tokens with 0 supply, for forward compatibility with our next library, nft_shared_ptr. nft_shared_ptr will implement reference counting with security by selling shares to the owned object until the SEC complains. Obligatory system diagrams How we call from C++ to Rust to Solidity: +-----+ +------+ +--------+ +---------------+ | | extern "C" | | rust-web3 | | | | | C++ +------------->| Rust +------------->| Wallet +------->| NFT Contracts | | | | | | | | | +-----+ +------+ +--------+ +---------------+ How the NftPtrToken contract and the NftPtrOwner contracts interact: +-------------+ +-------------------+ | NftPtrToken | | NftPtrOwner | | | Owns | | | 0x41414141<--+---------+ nft_ptr | | | +-------------------+ | | | | Owns +-------------------+ | 0x42424242<--+---------+ NftPtrOwner | | | | | | | | nft_ptr | | (1 instance | +-------------------+ | per program)| ... | | +-------------+ (1 instance per nft_ptr) Sponsor development For a limited time, you can buy any Git commit from this repository as a Non-Fungible Token on my Content-First Multimedia Proof-of-Authority revision-controlled realtime collaborative private enterprise blockchain (a shared Google Doc). You can also help by going full r/roastme on my code: this is only my second Rust project, and I would appreciate guidance on my journey to carcinization. What I learned * how C++ smart pointers are implemented * how to implement a Non-Fungible Token * how the Ethereum ecosystem has evolved since I wrote my last smart contract in 2017 * how to integrate my previous Solidity, Truffle, and Ganache workflow with new tools such as OpenZeppelin and hosted wallets * how to write a (trivial) program in Rust without fighting the borrow checker once * how to use rust-web3, serde_json, and the openssl crates * how to call Rust from C Building All instructions tested on macOS 11.2.1. You need: * Rustup * npm * Truffle * OpenSSL (brew install openssl@1.1) * and a C++17 compiler (Tested with Clang from Xcode 14.3). cd contracts npm install truffle compile ./dumpbytecode cd ../impl rustup override set nightly cargo build cd ../example ./build.sh Testing (local blockchain) Download and run Ganache to setup a private local blockchain. Then, run cd example RUST_BACKTRACE=1 RUST_LOG=info ./example Testing (Gorli testnet) To run this against a public test blockchain, the easiest way is to use a hosted node. Create a new keystore file on MyEtherWallet and get some Gorli test ethers from the Gorli faucet. Do not use an existing wallet or password! nft_ptr is very insecure; do not re-use a wallet or a password you care about, even for these worthless fake test ethers. Run the example using your new keystore and a hosted node: RUST_BACKTRACE=1 RUST_LOG=info NFT_PTR_HTTP="https://nodes.mewapi.io/rpc/goerli" \ NFT_PTR_NUM_CONFIRMATIONS=1 \ NFT_PTR_KEYSTORE="/path/to/your/MewWallet.keystore" \ NFT_PTR_PASSWORD="sample password" \ exec ./example Testing (Gorli testnet + local lite node) You can also run the example against a local lite node. Download Geth and start a lite node connected to the Gorli testnet: ./geth --goerli --syncmode light Stop Geth and import your testnet wallet: cp ~/Downloads/MewWallet.keystore ~/Library/Ethereum/goerli/keystore/ Restart Geth and unlock your testnet wallet: This is insecure! ./geth --goerli --syncmode light --unlock 0x
--http --allow-insecure-unlock Enter your password, then hit Enter. It should say Unlocked account address=0x
Finally run with local HTTP transport: cd example ./run.sh About C++ `std::unique_ptr` that represents each object as an NFT on the Ethereum blockchain worthdoingbadly.com Resources Readme Releases No releases published Packages 0 No packages published Languages * Rust 49.9% * C++ 18.7% * Solidity 14.7% * JavaScript 13.0% * Shell 2.8% * Python 0.9% * (c) 2021 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. 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.