|
| 1 | +--- |
| 2 | +title: "Go SDK - Creating and Managing Accounts" |
| 3 | +--- |
| 4 | + |
| 5 | +# Creating and Managing Accounts |
| 6 | + |
| 7 | +There are several ways to generate account credentials using the Go SDK. You can |
| 8 | +use: |
| 9 | +- `aptos.NewEd25519Account()` |
| 10 | +- `aptos.NewSecp256k1Account()` |
| 11 | +- `aptos.NewEd25519SingleSenderAccount()` |
| 12 | +- `aptos.NewAccountFromSigner()` |
| 13 | + |
| 14 | +`Account.NewEd25519Account()` is the most commonly used method to create keys |
| 15 | +for a new account. It defaults to `ED25519` key types, but you can also specify |
| 16 | +which signing scheme you would prefer like so: |
| 17 | + |
| 18 | +```go |
| 19 | +// To derive an ed25519 account |
| 20 | +account1 := aptos.NewEd25519Account() |
| 21 | + |
| 22 | +// To derive a secp256k1 account |
| 23 | +account2 := aptos.NewSecp256k1Account() |
| 24 | +``` |
| 25 | + |
| 26 | +Once you have generated credentials, you **must** fund it for the network to know it exists. |
| 27 | + |
| 28 | +In test environments this can be done with a faucet by running the following command: |
| 29 | + |
| 30 | +```go filename="fund.go" |
| 31 | +client, err = aptos.NewClient(aptos.DevnetConfig) |
| 32 | +if err != nil { |
| 33 | + panic("Failed to create client:" + err.Error()) |
| 34 | +} |
| 35 | + |
| 36 | +// Fund an account with 1 Devnet APT |
| 37 | +client.Fund(account1.Address, 100_000_000) |
| 38 | +``` |
| 39 | + |
| 40 | +## Other Ways To Represent Accounts |
| 41 | +If you have a private key, or equivalent representation, you can use that to |
| 42 | +create an `Account` struct to manage those credentials while using the Go SDK. |
| 43 | + |
| 44 | +Here are several examples that show how to do so with specific encoding schemes. |
| 45 | + |
| 46 | +### Derive an account from private key |
| 47 | + |
| 48 | +The SDK supports deriving an account from a private key with `NewAccountFromSigner()` method. |
| 49 | +In addition, this method supports deriving an account from a private key and account address. |
| 50 | +This method uses a local calculation and therefore is used to derive an `Account` that has not had its authentication key rotated. |
| 51 | + |
| 52 | +```go |
| 53 | +// to derive an account with a Ed25519 key scheme |
| 54 | +privateKey := &aptos.Ed25519PrivateKey{} |
| 55 | +err := privateKey.FromHex(privateKeyHex) |
| 56 | +if err != nil { |
| 57 | + panic("Failed to parse private key:" + err.Error()) |
| 58 | +} |
| 59 | +account := aptos.NewAccountFromSigner(privateKey) |
| 60 | + |
| 61 | +// to derive an account with a Single Sender Ed25519 key scheme |
| 62 | +privateKey := &aptos.Ed25519PrivateKey{} |
| 63 | +err := privateKey.FromHex(privateKeyHex) |
| 64 | +if err != nil { |
| 65 | + panic("Failed to parse private key:" + err.Error()) |
| 66 | +} |
| 67 | +singleSigner := &crypto.SingleSigner{Signer: privateKey} |
| 68 | +account := aptos.NewAccountFromSigner(singleSigner) |
| 69 | + |
| 70 | +// to derive an account with a Single Sender Secp256k1 key scheme |
| 71 | +privateKey := &aptos.Secp256k1PrivateKey{} |
| 72 | +err := privateKey.FromHex(privateKeyHex) |
| 73 | +if err != nil { |
| 74 | + panic("Failed to parse private key:" + err.Error()) |
| 75 | +} |
| 76 | +singleSigner := &crypto.SingleSigner{Signer: privateKey} |
| 77 | +account := aptos.NewAccountFromSigner(singleSigner) |
| 78 | + |
| 79 | +// to derive an account with a private key and account address |
| 80 | +address := &aptos.AccountAddress{} |
| 81 | +err := address.ParseStringRelaxed(addressHex) |
| 82 | +if err != nil { |
| 83 | + panic("Failed to parse address:" + err.Error()) |
| 84 | +} |
| 85 | +privateKey := &aptos.Ed25519PrivateKey{} |
| 86 | +err := privateKey.FromHex(privateKeyHex) |
| 87 | +if err != nil { |
| 88 | + panic("Failed to parse private key:" + err.Error()) |
| 89 | +} |
| 90 | +account := aptos.NewAccountFromSigner(privateKey, address.AuthKey()) |
| 91 | +``` |
| 92 | + |
| 93 | +{/* TODO: Once derivation path is supported ### Derive an account from derivation path */} |
0 commit comments