Golang
Core SDK - Golang
The XpansionChain Core SDK provides convenient access to XpansionChain's APIs and smart contracts to help projects build better web3 games and marketplaces.
π‘ROLLUPS THIS SDK SUPPORTS
XpansionChain
CONTENTS
Installation
Initialization
Get data (on assets, orders, past transactions, etc.)
Generating Stark (Layer 2) keys
Operations requiring user signatures
How do applications generate and use signers?
Contract requests
Smart contract autogeneration
Further documentation
πSDK LINKS
SDK reference
Package reference
Code examples
Github repository
Installationβ
The supported go versions are 1.18 or above.
Initializationβ
Initialize the Core SDK client with the network on which you want your application to run (see all networks available):
Select one of the following Ethereum networks XpansionChain platform currently supports.
Sandbox
The default test network (currently, it is GoΓ«rli)
Mainnet
Ethereum network
Get data (on assets, orders, past transactions, etc.)β
These methods allow you to read data about events, transactions or current state on XpansionChain (layer 2). They do not require any user authentication because no state is being changed.
Examples of the types of data that are typically retrieved include:
Assets or details of a particular asset
Token balances for a particular user
Orders or details about a particular order
Historical trades and transfers
Examplesβ
Get all collections and get assets from a particular collection:β
Generating Stark (Layer 2) keysβ
Stark keys are required to transact on XpansionChain's StarkEx Layer 2. They are the equivalent of Ethereum keys on L1 and allow users to sign transactions like trade, transfer, etc.
Key registrationβ
On XpansionChain, the goal of generating a Stark key is to register a mapping between the Stark public key and the user's Ethereum public key so that transactions requiring both L1 and L2 signers can be executed by users.
How to generate Stark keys on XpansionChainβ
XpansionChain provides two Stark key generation methods: | Type of Stark key generated: | User connection methods: | When to use this method: | XpansionChain tools: | | --- | --- | --- | --- | | Deterministic - generated using the user's Ethereum key as a seed (which means that the same Ethereum key will always generate the same Stark key) | Users connect with their L1 wallet (ie. Metamask), as the L2 key can simply be obtained from the L1 key. | User experience - users don't have to store or remember Stark keys.
Interoperability - when generating Stark keys for a user, think about how else they will use these keys. If they will be connecting to other applications and those applications connect to users' Stark keys (L2 wallets) via an L1 wallet, then it is best that their Stark keys are generated using this method. | Link SDK
Core SDK's GenerateLegacyKey()
method | | Random and non-reproducible - not generated from a user's Ethereum key | Once this Stark key is registered on XpansionChain (mapped to an Ethereum key), the Stark key owner needs to know and input this.
π¨ NOTE: If this key isn't persisted and stored by the user, it cannot be recovered and a new key cannot be re-registered. | Security - a Stark key generated using this method is completely independent of an Ethereum key, so the compromise of an Ethereum key does not compromise a user's corresponding Stark key.
Isolated use-case - this method is ideal for keys that are only used for one particular function, ie. in the backend of an application that allows tokens to be minted from a collection registered with this key. |
Core SDK's GenerateKey()
method |
Generating or retrieving a deterministic keyβ
If your user has a Stark key that was generated using the deterministic method, the Core SDK provides a way for you to retrieve this key using the GenerateLegacyKey()
method:
Generating a random, non-deterministic keyβ
The Core SDK also provides a way to generate a random, non-reproducible key using the GenerateKey()
method:
π¨π¨π¨ Warning π¨π¨π¨β
If you generate your own Stark private key, you will have to persist it. The key is randomly generated so cannot be deterministically re-generated.
Operations requiring user signaturesβ
As XpansionChain enables applications to execute signed transactions on both Ethereum (layer 1) and StarkEx (layer 2), signers are required for both these layers. In order to generate an Ethereum or Stark signer, a user's Ethereum or Stark private key is required.
There are two types of operations requiring user signatures:
Transactions that update blockchain state
Operations that XpansionChain require authorization for
In order to get user signatures, applications need to generate "signers".
What are transactions that update blockchain state?β
A common transaction type is the transfer of asset ownership from one user to another (ie. asset sale). These operations require users to sign (approve) them to prove that they are valid.
What are operations that require authorization?β
These operations add to or update data in XpansionChain's databases and typically require the authorization of an object's owner (ie. a user creating a project on XpansionChain).
How do applications generate and use signers?β
Signers are abstractions of user accounts that can be used to sign transactions. A user's private key is required to generate them.
There are two ways to get signers in your application:
Generate your own by obtaining and using the user's private keys
Use our Wallet SDK to connect to a user's wallet application
The first option, where an application obtains a user's private key directly, is risky because these keys allow anyone in possession of them full control of an account.
The second option provides an application with an interface to the user's account by prompting the user to connect with their wallet application (ie. mobile or browser wallet). Once connected the app can begin asking the user to sign transactions and messages without having to reveal their private key.
1. Generate L1 and L2 signersβ
The Core SDK provides functionality for applications to generate Stark (L2) signers.
2. Generate signers using the Wallet SDKβ
The Wallet SDK Web provides connections to Metamask and WalletConnect browser wallets.
See this guide for how to set this up.
Examplesβ
Create a project (requires an Ethereum layer 1 signer)β
Deposit tokens from L1 to L2 (requires an Ethereum layer 1 signer)β
Create an order (requires an Ethereum layer 1 and StarkEx layer 2 signer)β
Contract requestsβ
XpansionChain is built as a zkRollup (zero-knowledge rollup) in partnership with StarkWare. The choice of zkRollups is deliberate, as it offers unparalleled scalability without compromising security. This means the following:
Cost-effectiveness: When you mint or trade an NFT on XpansionChain, you enjoy the advantage of zero gas fees.
Security: All transactions are secured by zero-knowledge proofs, which makes XpansionChain the first ever layer 2 solution for NFTs on Ethereum.
The Core SDK provides an interface with the smart contracts necessary for interacting with the XpansionChain platform.
See all smart contracts available in the Core SDK.
Smart contract autogenerationβ
The XpansionChain Solidity contracts can be found in the contracts
folder. Contract bindings in Golang are generated using abigen.
Core
The Core contract is XpansionChain's main interface with the Ethereum blockchain, based on StarkEx.
Registration
The Registration contract is a proxy smart contract for the Core contract that combines transactions related to onchain registration, deposits, and withdrawals. When a user who is not registered onchain attempts to perform a deposit or a withdrawal, the Registration combines requests to the Core contract in order to register the user first. Users who are not registered onchain are not able to perform a deposit or withdrawal.
IERC20
Standard interface for interacting with ERC20 contracts, taken from OpenZeppelin.
IERC721
Standard interface for interacting with ERC721 contracts, taken from OpenZeppelin.
Further documentationβ
See the Developer homepage for general information on building on XpansionChain.
Build on XpansionChain zkEVM:
Documentation
API reference
Support
Build on XpansionChain:
Documentation
API reference
Support
Last updated