0xsequence / sequence-unreal

Sequence Unreal SDK
Apache License 2.0
4 stars 5 forks source link

Sequence Unreal Spec #1

Closed pkieltyka closed 6 months ago

pkieltyka commented 1 year ago

Unreal Engine version support? UE5.1+


For Sequence Web3 Unreal SDK v3, aka sequence-unreal -- we will be going deeper with a closer-to-the-metal implementation compared to https://github.com/0xsequence/web3-unreal-sdk


(1) Ethereum library implementation

The first step is to implement the basics of what is necessary for general Ethereum operations.

Whenever we make a smart contract call on Ethereum, what we're doing is encoding the smart contract function call and arguments with the abi encoder, and then preparing a transaction object, signing it, and then sending it over the http network. This series of steps, is exactly the different parts which will come together to provide the functionality of our library.

The functionality of the Ethereum library includes:

  1. HTTP client for Ethereum node to query the api endpoints on the Ethereum node. Ethereum node use JSON-RPC for its "API"
  2. Encode, sign and send transactions to an Ethereum node
  3. secp256k1 signer support (aka the EOA wallet), where we generate/import a private key, sign data and verify signatures

Now, lets break down the functionality above into the following modules:

a. json rpc http client b. abi encoder / decoder c. secp256k1 key generation/import and signing

(a) json rpc http client

Ethereum nodes come with a HTTP server with a simple JSON-RPC API. It's really just like any other Web API you've used before, but, it uses JSON-RPC instead of REST for the request and response data structures.

You can see the official Ethereum JSON-RPC reference methods here: https://ethereum.org/en/developers/docs/apis/json-rpc/

I also recommend to use the ethkit/ethrpc source as a reference, as this is a full JSON-RPC Provider implementation for Ethereum. You can see the provider interface defined: https://github.com/0xsequence/ethkit/blob/master/ethrpc/interface.go

Using ethkit/ethrpc as a reference, we can implement all of the methods over time, but I suggest to focus on the following methods to start (as this is all we really need):

Another good reference is to look at ethers.js -- see:

(b) abi encoder / decoder

The abi encoder / decoder is used so we can convert between C++ data types to EVM data types. When we make a smart contract call, what we're doing is encoding the contract call and its arguments in ABI encoding, signing it and then sending it to the SendRawTransaction() (aka eth_sendTransaction JSON-RPC method).

For some references see:

  1. https://github.com/ethers-io/ethers.js/tree/main/src.ts/abi
  2. https://github.com/0xsequence/ethkit/tree/master/ethcoder

(c) secp256k1 signer

Finally, we will build here a very simple / small EOA (Externally Owned Account) Signer, aka, "a wallet". We will use this very minimal wallet, so that we can sign transactions which are sent to the payload. For the Sequence wallet, we'll have higher order account type, but we will use the signer here as one of our "keys" inside of the Sequence wallet. More on the Sequence wallet later, for now lets talk about the signer.

We will need the following functions:

  1. Generate secp256k1 private key
  2. Derive secp256k1 private key from a seed -- note: we don't have to worry about implementing a HD wallet (hierarchal deterministic), aka the 12 or 24 seed words.. we don't need a HD wallet for our library at this time, we can just generate a large number and use that as our private key.
  3. Sign
  4. Recover / verify

Reference -- here are some references to creating a minimal wallet signer:


Finally, for implementing the above, there are Ethereum C++ libraries in the wild to search/reference as well.

(2) Implement C++ Sequence library

The Sequence library is really an interface / adapter on top of the https://github.com/0xsequence/wallet-contracts smart contracts. You can think of its entire purpose is to just be able to call smart contract functions and work with data structures built on the wallet-contracts in the repo above.

For the sequence-unreal library, we will implement against the v2 wallet-contracts, you can find a reference implementation of the library to interact with v2 contracts here: https://github.com/0xsequence/sequence.js/tree/v2

The work:

  1. Port https://github.com/0xsequence/sequence.js/tree/master/packages/core and and https://github.com/0xsequence/sequence.js/tree/master/packages/core/tests/v2 to C++

(3) Implement Unreal native wallet UI

Once we have our libraries in order:

  1. Login via email
  2. Logout
  3. Show your mini-wallet
  4. Once the wallet is opened: a. View your coins / collectibles b. View your history c. Send coins / collectibles d. Show "receive" QR code e. Sign a message f. Sign a transaction
  5. Thats it... that should cover everything we need to do :) we can keep it simple and light

An example of another project which I think strikes a decent balance of simplicity: https://www.loom.com/share/ab28ba3c47924bda9a88546e554c66d8 / https://www.usemeta.fi/demos

pkieltyka commented 1 year ago

https://github.com/ethereum/aleth might have some ideas for reference

pkieltyka commented 1 year ago

UPDATED SPEC:

Unreal Engine version support? UE5.1+


For Sequence Web3 Unreal SDK v3, aka sequence-unreal -- we will be going deeper with a closer-to-the-metal implementation compared to https://github.com/0xsequence/web3-unreal-sdk


(1) Ethereum library implementation

The first step is to implement the basics of what is necessary for general Ethereum operations.

Whenever we make a smart contract call on Ethereum, what we're doing is encoding the smart contract function call and arguments with the abi encoder, and then preparing a transaction object, signing it, and then sending it over the http network. This series of steps, is exactly the different parts which will come together to provide the functionality of our library.

The functionality of the Ethereum library includes:

  1. HTTP client for Ethereum node to query the api endpoints on the Ethereum node. Ethereum node use JSON-RPC for its "API"
  2. Encode, sign and send transactions to an Ethereum node
  3. secp256k1 signer support (aka the EOA wallet), where we generate/import a private key, sign data and verify signatures

Now, lets break down the functionality above into the following modules:

a. json rpc http client b. abi encoder / decoder c. secp256k1 key generation/import and signing

(a) json rpc http client

Ethereum nodes come with a HTTP server with a simple JSON-RPC API. It's really just like any other Web API you've used before, but, it uses JSON-RPC instead of REST for the request and response data structures.

You can see the official Ethereum JSON-RPC reference methods here: https://ethereum.org/en/developers/docs/apis/json-rpc/

I also recommend to use the ethkit/ethrpc source as a reference, as this is a full JSON-RPC Provider implementation for Ethereum. You can see the provider interface defined: https://github.com/0xsequence/ethkit/blob/master/ethrpc/interface.go

Using ethkit/ethrpc as a reference, we can implement all of the methods over time, but I suggest to focus on the following methods to start (as this is all we really need):

Another good reference is to look at ethers.js -- see:

(b) abi encoder / decoder

The abi encoder / decoder is used so we can convert between C++ data types to EVM data types. When we make a smart contract call, what we're doing is encoding the contract call and its arguments in ABI encoding, signing it and then sending it to the SendRawTransaction() (aka eth_sendTransaction JSON-RPC method).

For some references see:

  1. https://github.com/ethers-io/ethers.js/tree/main/src.ts/abi
  2. https://github.com/0xsequence/ethkit/tree/master/ethcoder

(c) secp256k1 signer

Finally, we will build here a very simple / small EOA (Externally Owned Account) Signer, aka, "a wallet". We will use this very minimal wallet, so that we can sign transactions which are sent to the payload. For the Sequence wallet, we'll have higher order account type, but we will use the signer here as one of our "keys" inside of the Sequence wallet. More on the Sequence wallet later, for now lets talk about the signer.

We will need the following functions:

  1. Generate secp256k1 private key
  2. Derive secp256k1 private key from a seed -- note: we don't have to worry about implementing a HD wallet (hierarchal deterministic), aka the 12 or 24 seed words.. we don't need a HD wallet for our library at this time, we can just generate a large number and use that as our private key.
  3. Sign
  4. Recover / verify

Reference -- here are some references to creating a minimal wallet signer:


Finally, for implementing the above, there are Ethereum C++ libraries in the wild to search/reference as well.

(2) Implement WaaS http client

Interacting with Sequence wallets will be via the WaaS server backend.

The work:

  1. Port https://gist.github.com/pkieltyka/ab1ac1d1216dcd49d0c74962a8b29077 to C++

(3) Implement Unreal native wallet UI

Once we have our libraries in order:

  1. Login via email
  2. Logout
  3. Show your mini-wallet
  4. Once the wallet is opened: a. View your coins / collectibles b. View your history c. Send coins / collectibles d. Show "receive" QR code e. Sign a message f. Sign a transaction
  5. Thats it... that should cover everything we need to do :) we can keep it simple and light

An example of another project which I think strikes a decent balance of simplicity: https://www.loom.com/share/ab28ba3c47924bda9a88546e554c66d8 / https://www.usemeta.fi/demos