GithubHelp home page GithubHelp logo

zhuowei / nft_ptr Goto Github PK

View Code? Open in Web Editor NEW
2.0K 17.0 36.0 136 KB

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

Home Page: https://worthdoingbadly.com

Solidity 14.66% Shell 2.83% Python 0.87% JavaScript 12.98% C++ 18.69% Rust 49.97%

nft_ptr's Introduction

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<Cow>();
  nft_ptr<Animal> 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<Cow>();

This:

  • initializes the nft_ptr runtime
  • creates the first nft_ptr<Cow>
  • 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<Cow> 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<Cow>.

[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<Animal>   |
|             |          +-------------------+
|             |
|             | Owns     +-------------------+
| 0x42424242<--+---------+ NftPtrOwner       |
|             |          |                   |
|             |          | nft_ptr<Animal>   |
| (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<address> --http --allow-insecure-unlock

Enter your password, then hit Enter. It should say

Unlocked account                         address=0x<address>

Finally run with local HTTP transport:

cd example
./run.sh

nft_ptr's People

Contributors

zhuowei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nft_ptr's Issues

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

shared_ptr would be interesting

What about shared_nft_ptr for when we need to tranfer an NFT but still receive royalties? we need to have a shared pointer

thanks

WASM Runtime

Everyone knows that WASM is the future for sandboxing code to make it safe.

It is highly desirable that as much of this code run in WASM as possible, so that the attack surface is minimized. This is a security oriented project, after all.

This means you would need some (rust or C++) shim for the networking part since WASM has no system APIs.

See wasm-pack tool for compiling rust to wasm.

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)

Offset your carbon footprint

First of all great project, this is how Stroustrup intended C++ to be used! Now to the problem...

We all know that NFTs are bad for the earth and accelerate climate change. No, your choo choo train argument will not convince anyone otherwise.

Luckily I have a proposition how you can offset the resulting carbon footprint caused by this project: It is common knowledge that planting 2 trees suddenly allows you to buy a large SUV that needs 100 litres of gasoline to run one freaking kilometre - without feeling bad about yourself.

Therefore I propose that this project allocates one fresh red-black tree in memory for every NFT created. Now we can allocate gigabytes of blockchain pointers without feeling bad about ourself.

Don't forget to plant the tree the right side up - we computer science weirdos tend to grow trees in the wrong direction...

If you have other ideas how to offset the carbon footprint, let me know.

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! 👍

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.

Clarify legal ownership of NFT

How do we know who can sell NFT certificates for each NFT pointers? We could have NFT certificates pointing to NFT pointers pointing to memory addresses. The readme states ownership is easy to determine, but who actually owns them?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.