A Simple blockchain wallet built on the Celo blockchain with Golang
Functionlities:
Generating a new Celo wallet
funcnewWallet() {
// Generate a new Celo walletprivateKey, publicKey, celoAddress, err:=generateNewWallet()
iferr!=nil {
log.Fatal(err)
}
fmt.Printf("Private key: %x\n", privateKey.D)
fmt.Printf("Public key: %x\n", publicKey)
fmt.Printf("Celo address: %s\n", celoAddress.Hex())
}
funcgenerateNewWallet() (*ecdsa.PrivateKey, []byte, common.Address, error) {
// Generate a private key using the secp256k1 curveprivateKey, err:=crypto.GenerateKey()
iferr!=nil {
returnnil, nil, common.Address{}, err
}
// Derive the public key from the private keypublicKey:=privateKey.Public().(*ecdsa.PublicKey)
// Convert the public key to compressed formatpublicKeyBytes:=secp256k1.CompressPubkey(publicKey.X, publicKey.Y)
// Generate the Celo address from the public keyceloAddress:=crypto.PubkeyToAddress(*publicKey)
returnprivateKey, publicKeyBytes, celoAddress, nil
}
Importing an existing Celo wallet
funcexistingWallet() {
// replace with your own private keyprivateKeyHex:=""// Import an existing Celo walletprivateKey, publicKey, celoAddress, err:=importWallet(privateKeyHex)
iferr!=nil {
log.Fatal(err)
}
fmt.Printf("Private key: %x\n", privateKey.D)
fmt.Printf("Public key: %x\n", publicKey)
fmt.Printf("Celo address: %s\n", celoAddress.Hex())
}
funcimportWallet(privateKeyHexstring) (*ecdsa.PrivateKey, []byte, common.Address, error) {
// Decode the private key from hex stringprivateKeyBytes, err:=hex.DecodeString(privateKeyHex)
iferr!=nil {
returnnil, nil, common.Address{}, err
}
// Load the private keyprivateKey, err:=crypto.ToECDSA(privateKeyBytes)
iferr!=nil {
returnnil, nil, common.Address{}, err
}
// Derive the public key from the private keypublicKey:=privateKey.Public().(*ecdsa.PublicKey)
// Convert the public key to compressed formatpublicKeyBytes:=secp256k1.CompressPubkey(publicKey.X, publicKey.Y)
// Generate the Celo address from the public keyceloAddress:=crypto.PubkeyToAddress(*publicKey)
returnprivateKey, publicKeyBytes, celoAddress, nil
}
funcsend(client*ethclient.Client) {
// (replace with your own private key)privateKeyHex:=""// Load the private keyprivateKeyBytes, err:=hex.DecodeString(privateKeyHex)
iferr!=nil {
log.Fatal(err)
}
privateKey, err:=crypto.ToECDSA(privateKeyBytes)
iferr!=nil {
log.Fatal(err)
}
// Send a transactiontoAddress:=common.HexToAddress("0x8BdDeC1b7841bF9eb680bE911bd22051f6a00815")
value:=big.NewInt(100000000000000000) // 0.1 CELOtxHash, err:=sendTransaction(client, privateKey, toAddress, value)
iferr!=nil {
log.Fatal(err)
}
fmt.Printf("Transaction hash: %s\n", txHash.Hex())
}
funcsendTransaction(client*ethclient.Client, privateKey*ecdsa.PrivateKey, toAddress common.Address, value*big.Int) (common.Hash, error) {
// Create a new transactorfromAddress:=crypto.PubkeyToAddress(privateKey.PublicKey)
nonce, err:=client.PendingNonceAt(context.Background(), fromAddress)
iferr!=nil {
return common.Hash{}, err
}
// Assuming you have an Ethereum client instance named 'client'gasPrice, err:=client.SuggestGasPrice(context.Background())
iferr!=nil {
log.Fatalf("Failed to get the gas price: %v", err)
}
// Increase the gas price by 10% (or more, depending on how quickly you want the transaction to be mined)gasPrice=gasPrice.Mul(gasPrice, big.NewInt(11)).Div(gasPrice, big.NewInt(10))
// Assuming you have an Ethereum client instance named 'client'chainID, err:=client.ChainID(context.Background())
iferr!=nil {
log.Fatalf("Failed to get the chain ID: %v", err)
}
// Assuming you have a private key instance named 'privateKey'auth, err:=bind.NewKeyedTransactorWithChainID(privateKey, chainID)
iferr!=nil {
log.Fatalf("Failed to create the auth object: %v", err)
}
auth.Nonce=big.NewInt(int64(nonce))
auth.Value=valueauth.GasLimit=uint64(21000)
auth.GasPrice=gasPricetx:=types.NewTransaction(auth.Nonce.Uint64(), toAddress, auth.Value, auth.GasLimit, auth.GasPrice, nil)
signedTx, err:=auth.Signer(auth.From, tx)
iferr!=nil {
return common.Hash{}, err
}
err=client.SendTransaction(context.Background(), signedTx)
iferr!=nil {
return common.Hash{}, err
}
returnsignedTx.Hash(), nil
}