Blockchain Wallet
❓ Key Question / Problem / Issue
- what is blockchain wallet
- how blockchain wallet works
- how secure is blockchain wallet
- how wallet security works
- how do we relate blockchain wallet to a game account
✅ Expected Output/Definition of Done
UploadDetailed Information about blockchain wallet
how to integrate user blockchain wallet to our system anonymously
🎁 Resulting Solution
Write writing results below
References Links
What is Blockchain Wallet and How Does It Work? [Updated]
my Blockchain.info just got hacked
Blockchain Demo: Public / Private Keys & Signing
Blockchain Series - How MetaMask Creates Accounts
Intro to Digital Signatures | ECDSA Explained
What is Wallet
A Wallet is a software that, on the behalf of a user, sign transactions and prove ownership on the Blockchain network.
A quick note – any wallet doesn't actually store cryptocurrency directly. Cryptocurrencies, by definition, exist only on blockchains. Wallets store the private access keys to those cryptocurrencies.
How Wallet works
A Wallet manages a private/public key pair which is used to sign transactions and prove ownership
Connecting to blockchain network does not always require a wallet, if the request to the network is read only, then a provider, which does not store private/public key of user, is enough
How to connect blockchain wallet to an Application
A game account cannot directly interact with a wallet. Instead, the game engine itself can be programmed to interact with user wallet’s of choice. This interaction can be programmed to make the game retrieve the user’s public address (public key), which also can stored on the game account so that the game can link the public key to user’s account. Simple diagram might explain this concept better:
Example of linking user’s public key to data in smart contract
A game also can request user’s signature from the wallet so the game can authorize the transaction in the smart contract. E.g buy a NFT
Many javascript library can provide programmer an API that enables the software to interact with any wallet of user’s choice, for example this code below uses ethers.js
import { providers } from ethers
import Web3Modal from web3modal
/**
* getProviderOrSigner: this function can be called to to get provider or signer of the wallet.
*/
export default function getProviderOrSigner() {
// Initiate web3Modal.js, a library that enables us to connect to user wallet.
web3modal = new Web3Modal({
network: 'rinkeby', // can be adujusted to connect to other blockchain network
providerOptions: {},
disableInjectedProvider: false
})
}
// Connect to wallet if user is authorizing the app to connect.
// User will be prompted by the wallet by this time
const provider = web3modal.connect()
// Connect the wallet to the app
const web3Provider = providers.Web3Provider(provider)
// if signer is needed then get signer
const web3Signer = web3Provider.signer()
return { provider: web3Provider, signer: web3Signer }
}
The code above is used to connect to the user wallet, which then can be used to interact with the game blockchain network. A game typically have it’s smart contract stored on some blockchain network. For example, in this code below, the app will connect to the game smart contract to try to receive user balance in the game.
/**
* getBalanceAgateCoinDev: Check user balance in the game
*/
const getBalanceAgateCoinDev = async () => {
try {
const { provider, signer } = getProviderOrSigner()
const tokenContract = new Contract(TOKEN_CONTRACT_ADDRESS, TOKEN_CONTRACT_ABI, provider)
// You need signer for user's address, a.ka. public key
const address = await signer.getAddress()
// with ethers.js, we can directly interact with the smart contract using ABI
// getBalanceOf is an ERC20 standard function
const balance = await tokenContract.getBalanceOf(address);
console.log('Your balance in the game is": + balance)
} catch (e) {
console.log(e)
}
}
This retrieve function can be extended to not only get the balance of user’s account, but also can be extended to view user’s mapping of their NFT.
How Wallet account address, public key, and private key works
This Explanation use Ethereum Network (Implementation wallet in other network can be differ)
A Wallet have :
-
Account Address : a unique identifier (in Ethereum Network using hexadecimal number) that can be share to anyone to identify your account (essentially like email address). Every Account Address has its own public key and private key.
(example Account address on metamask)
-
Private Key : used to sign transaction like sending cryptocurrency, etc. (in Ethereum Network, Private key is in hexadecimal number)
-
Public Key : derived from Private key using Elliptic Curve Digital Signature Algorithm (ECDSA) to verify transaction. (in Ethereum Network, Public key is in hexadecimal number)
Signing & Verifying Transaction
-
Each time an account address send transaction to other account address, the transaction is signed (using ECDSA) with the sender Private Key. The other account can verify (using ECDSA) the transaction using the sender Public Key.
-
Illustrations
- Account A signed the message using Private Key A and Account B verify the message from A using Public Key A.
- Alice sign message using Alice’s private key and Bob verify the message using Alice’s public key
Identifying wallet owner for token/smart contract ownership.
First, a primer. A smart contract and token is a different thing. A smart contract, by definitions, is a set of programming rules that run on blockchain. It is this smart contract that have an address in the chain. Smart contract also can store the owner (deployer) address. In Progressthat contract, a programmer can write a smart contract that produces a token. A smart contract that can produce token is called token smart contract
What is a token then? a token is a representation of something in the blockchain. Mind that it is different from token smart contract. **It doesn’t necessarily must have an address (deployed on blockchain). It can be just a unsigned integer in the token smart contract, or even a string. By representing things as tokens, we can allow smart contracts to interact with them, exchange them, create or destroy them.
Now, how we can identify the token to a wallet user in a smart contract?
Well, At the end of the day, a token contract is not much more a mapping of addresses to balances, plus some methods to add and subtract from those balances. It is these balances that represent the tokens themselves. Someone "has tokens" when their balance in the token contract is non-zero. That’s it! These balances could be considered money (cryptocurrency), experience points in a game, number of NFT’s that someone has, etc.****
NFTs, or non-fungible-token, is a different thing. The token is represented not by number as balance, but by its metadata. But, at the end of the day, it is still a token just like cryptocurrency, it is still mappable to an address in a smart contract, especially using its token ID.
For example, ERC721, a token smart contract protocol for producing NFT, have this mapping of tokenId to an address. Here’s the snippet.
contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
// Mapping from token ID to owner address
mapping(uint256 => address) private _owners;
// Mapping owner address to token count
mapping(address => uint256) private _balances;
...
/**
* @dev get TokenURI (Asset URL) from tokenId
* tokenURI is one of the metadata of the NFT.
*/
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
_requireMinted(tokenId);
string memory baseURI = _baseURI();
return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : "";
}
...
}