C++ `std::unique_ptr` that represents each object as an NFT on the Ethereum blockchain

Related tags

Cryptography nft_ptr
Overview

C++ std::unique_ptr that represents each object as an NFT on the Ethereum blockchain.

Build nft_ptr Follow us: worthdoingbadly.com | @zhuowei | @[email protected]

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.

  • 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:

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 (Görli 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 Görli test ethers from the Görli 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 (Görli 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 Görli 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
Comments
  • Optimize the whitepaper PDF

    Optimize the whitepaper PDF

    One whole kilobyte? We have no time for such bloat. This pull request optimizes the whitepaper PDF to the smallest possible version that retains the original paper size, opens correctly in both Firefox and Chrome, and also is detected as a pdf by github so it gets served with the correct mime type.

    This doesn't actually open properly in Acrobat Reader, but who cares.

    opened by Selicre 5
  • Rewrite it in Rust

    Rewrite it in Rust

    Whilst C++ is cool and all, these days Rust is really where it's at. Eventually, it would be ideal to move the entire thing to Rust smart pointers, but as a transition, using C++/Rust bindings would be an excellent solution.

    opened by chop0 2
  • Inspiring

    Inspiring

    I have a business selling acres of land on the moon only to the most discerning and smart customers (for $10 you get a nice certificate with a very, very small-print disclaimer) and I’m sure I can incorporate this new C++ doohickey into my business ~~scam~~ scheme somehow. Great work! 👍

    opened by danielearwicker 0
  • NFT for this repository

    NFT for this repository

    The work of art hosted within this repository should have a corresponding NFT so we can buy ownership of a centralised JSON file that we can use to get to this repository

    opened by ashleydavies 1
  • This exists

    This exists

    I feel encouraged to comment on a possible assertion, that, perhaps, this library has yet to be challenged on what I may, with some impertinence, view as the most major problem with this library, which is as per the title may to some most stringently imply, to perhaps some consternation of the most perplexing sort; for which I offer the following elucidation - not to be overly-wrought with words, but requiring perhaps only the most plain and simple explanation; thus, I shall proceed henceforth and directly to explain, in not too large a verbiage, the most consistent problem with this library, and the one for which no issue has yet been raised - yet, certainly, this issue may, in the modest opinion of this poster, be said to possibly be suggested to exist; perhaps even insisted, if one were to commence in rudeness, and not in polite and courteous commentary; and so I shall not waste many words and simply get directly to the point - although some may wish thereafter that I had added more words, to the explaining of what seems to me to be easily understood; but then, what one person may view as easily understandable, another person may view as obtuse or easily misunderstood - by which I certainly mean no insult of the mind, for the intricacies of language (especially the one in which this post was originally, well, posted; that is, English - not to assume that you might not nor even may not read this in another language, for which "translation errors" (with which even the best intent may accidentally leak through - for which we mean no insult to the translator, who has a most difficult job, for which we can only offer our apologies) may render it somewhat less than simple and direct) mean that the writing which is clear and plain to one, may be overly-complex and wordy to another; and yet to another, that same writing may leave out too many of the important explanations; however, given that no perfect statement can thus be made by us, thus we proceed:

    the existence of this library itself.

    • Parfait of Circlewood.

    (yes, this is parody)

    bug 
    opened by MouseProducedGames 0
Owner
Software developer: Android, iOS, Web, Voice
null
Substreams development kit for Ethereum chains, contains Firehose Block model and helpers as well as utilities for Ethereum ABI encoding/decoding.

Substreams Ethereum Substreams development kit for Ethereum chains, contains Rust Firehose Block model and helpers as well as utilities for Ethereum A

StreamingFast 15 Oct 25, 2022
Ethereum (and Ethereum like) indexer using P2P message to fetch blocks and transactions

Ethereum P2P indexer This project is an indexer for Ethereum and Ethereum forks. It takes advantage of the ETH (Ethereum Wire Protocol) to fetch block

null 5 Nov 10, 2023
An Ethereum compatible Substrate blockchain for bounties and governance for the Devcash community.

Substrate Node Template A fresh FRAME-based Substrate node, ready for hacking ?? Getting Started Follow the steps below to get started with the Node T

null 4 Mar 30, 2022
Emerging smart contract language for the Ethereum blockchain.

Emerging smart contract language for the Ethereum blockchain.

null 1.4k Jan 9, 2023
A re-write of polkadot staking miner using subxt to avoid hard dependency to each runtime version

Staking Miner v2 WARNING this library is under active development DO NOT USE IN PRODUCTION. The library is a re-write of polkadot staking miner using

Parity Technologies 19 Dec 28, 2022
std::simd implementation of BLAKE3

BLAKE3-STD the first blake3 implementation on std::simd ONLY COMPILES WITH NIGHTLY [dependencies] blake3-std = "0.0.1" OFFICIAL DOC BLAKE3 is a crypto

LemonHX 16 Nov 23, 2022
A no-std / no-alloc TLS 1.3 client

puny-tls - no-std/no-alloc TLS 1.3 client This is an improvement over tiny-tls-rs to make it more useable. However the only reason this exists is to r

Björn Quentin 2 Aug 22, 2022
Fast array expressions on the stack, no-std compatible

Strobe Fast, low-memory, elementwise array expressions on the stack. Compatible with no-std (and no-alloc) environments. This crate provides array exp

James Logan 3 Sep 23, 2023
Hash trait that is object-safe

Hash trait that is object-safe This crate provides a DynHash trait that can be used in trait objects. Types that implement the standard library's std:

David Tolnay 19 Nov 12, 2023
Substrate NFT !ink smart contract base

Substrate !ink NFT simple implementation This is a simple working version of base NFT smart contract written using latest (as of this date) !ink 3.0.0

POLK4.NET 14 Dec 3, 2022
Drop-in Access Control via NFT Ownership

niftygate - Signature-Based Authenticating Proxy What is it? niftygate is a proxy for HTTP services, that validates signatures, providing an AuthN lay

Chris Olstrom 70 Jan 3, 2023
NFT & Marketplace Contracts with royalties and fungible token support. Sample React app included.

NFT Market Reference Implementation A PoC backbone for NFT Marketplaces on NEAR Protocol. Reference Changelog Changelog Progress: basic purchase of NF

NEAR App Examples 156 Apr 28, 2022
Bespoke toolkit for Non-fungible token (NFT) technology 🚀

Bespoke toolkit for Non-fungible token (NFT) technology ?? What is Onft? Instead of forcing a consensus algorithm or peer networking on you, Onft prov

Owez 5 Jan 9, 2022
Examples and helpers to build NFT contracts on CosmWasm

CosmWasm NFTS This repo is the official repository to work on all NFT standard and examples in the CosmWasm ecosystem. cw721 and cw721-base were moved

CosmWasm 147 Jan 4, 2023
The Metaplex NFT-standard Swiss Army Knife tool.

Metaboss The Solana Metaplex NFT 'Swiss Army Knife' tool. Current supported features: Decode NFT mint account metadata Get a list of mint accounts for

Samuel Vanderwaal 576 Jan 6, 2023
The NFT smart contract powering xyz on Terra

xyz NFT Contract This repository contains the core NFT smart contract that implements xyz, a base layer for metaverses on the Terra blockchain. The xy

null 16 Sep 25, 2022
A PoC backbone for NFT Marketplaces on NEAR Protocol

NFT Market Reference Implementation A PoC backbone for NFT Marketplaces on NEAR Protocol. Reference Changelog Changelog Progress: basic purchase of NF

null 9 May 26, 2022
Solana NFT generative artwork program

resin Solana NFT generative artwork program Installation Depends on imagemagick for art generation, which can be installed here: https://imagemagick.o

null 4 Jun 24, 2022
All the data an IC app needs to make seamless experiences, accessible directly on the IC. DAB is an open internet service for NFT, Token, Canister, and Dapp registries.

DAB ?? Overview An Internet Computer open internet service for data. All the data an IC app needs to make a seamless experience, accessible directly o

Psychedelic 58 Oct 6, 2022