Created
November 18, 2022 16:49
-
-
Save avislash/418367fa94e12d61649100c19e9fac41 to your computer and use it in GitHub Desktop.
Register Hedera Account Using Raw Transactions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"github.com/hashgraph/hedera-sdk-go/v2" | |
) | |
func main() { | |
//Import Existing Keys | |
accountID := os.Getenv("HBAR_ACCT_ID") | |
privateKey := os.Getenv("HBAR_PRIVATE_KEY") | |
//Convert Env Variables to Hedera AccountID and Private Key Types | |
acctID, err := hedera.AccountIDFromString(accountID) | |
if err != nil { | |
panic(fmt.Sprintf("Error converting %s to hedera acctID: %s", accountID, err)) | |
} | |
fmt.Println("AccountID: ", acctID) | |
sk, err := hedera.PrivateKeyFromString(privateKey) | |
if err != nil { | |
panic(fmt.Sprintf("Error converting %s to hedera privateKey: %s", privateKey, err)) | |
} | |
fmt.Println("Public Key: ", sk.PublicKey()) | |
fmt.Println("Public Key To AccountID: ", sk.PublicKey().ToAccountID(0, 0)) | |
//Connect to Hedera Testnet | |
client := hedera.ClientForTestnet() | |
client.SetOperator(acctID, sk) | |
//Create New Keys | |
newAcctPrivateKey, err := hedera.PrivateKeyGenerateEd25519() | |
if err != nil { | |
panic(fmt.Sprintf("Error generating new ED25519 Private Key: %s", err)) | |
} | |
//Register New Account by manually transferring HBAR from existing Account (0.0.acctID) to new account (0.0.newAcctPublicKey) | |
rawAcctID := *(newAcctPrivateKey.PublicKey().ToAccountID(0, 0)) | |
fmt.Println("New PublicKey: ", newAcctPrivateKey.PublicKey()) | |
fmt.Println("RawAcctID: ", rawAcctID) | |
txn := hedera.NewTransferTransaction().AddHbarTransfer(acctID, hedera.HbarFrom(-1000, hedera.HbarUnits.Tinybar)).AddHbarTransfer(rawAcctID, hedera.HbarFrom(1000, hedera.HbarUnits.Tinybar)) | |
fmt.Printf("Transfering HBAR from %s to %s\n", acctID, rawAcctID) | |
txnResp, err := txn.Execute(client) | |
if err != nil { | |
panic(fmt.Sprintf("Error executing transfer txn: %s", err)) | |
} | |
//Registering Account manually does not populate the AccountID field in the TxnReceipt since it's an HBAR Transfer instead of an Account Create Txn | |
//The account ID needs to be parsed from the record. | |
fmt.Println("Fetching Record") | |
txnRecord, err := txnResp.GetRecord(client) | |
if err != nil { | |
panic(fmt.Sprintf("Error fetching txn record: %s", err)) | |
} | |
//Grab the last Transfer in the record. This is the transfer to the new acct | |
newAcctTxfer := txnRecord.Transfers[len(txnRecord.Transfers)-1] | |
if txnRecord.Receipt.Status != hedera.StatusSuccess { | |
panic(fmt.Sprintf("Error Txn ID %s unexpected status: Expected %s | Actual %s", hedera.StatusSuccess, txnRecord.Receipt.Status)) | |
} | |
fmt.Println("Status: ", txnRecord.Receipt.Status) | |
fmt.Println("Txn ID: ", txnRecord.Receipt.TransactionID) | |
fmt.Println("New Account ID: ", newAcctTxfer.AccountID) | |
//Alternative way to grab AccountID is to do an Account Info Query using the raw acctID | |
acctInfoTxn := hedera.NewAccountInfoQuery().SetAccountID(rawAcctID) | |
acctInfo, err := acctInfoTxn.Execute(client) | |
if err != nil { | |
panic(fmt.Sprintf("Error getting acct Info: %s", err)) | |
} | |
fmt.Println("Acct Info Acct ID: ", acctInfo.AccountID) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment