Skip to content

Commit 3c7973c

Browse files
authored
[go-sdk] Add docs to match the TypeScript SDK (#571)
1 parent a72fda9 commit 3c7973c

13 files changed

+1371
-79
lines changed

apps/nextra/pages/en/build/sdks/go-sdk.mdx

+17-79
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
title: "Go SDK"
33
---
44

5-
import { Callout } from "nextra/components";
5+
import { Card, Cards } from '@components/index'
66

77
# Aptos Go SDK
88

99
## Installing the Go SDK
1010

11-
<Callout type="warning">
12-
The Go SDK is currently in a Beta, and its interfaces are subject to change
13-
</Callout>
14-
1511
Aptos provides an official Go SDK in
1612
the [aptos-go-sdk GitHub](https://github.com/aptos-labs/aptos-go-sdk) repository.
1713
To use the Go SDK, get the main package here:
@@ -20,78 +16,20 @@ To use the Go SDK, get the main package here:
2016
go get github.com/aptos-labs/aptos-go-sdk
2117
```
2218

23-
## Using the Go SDK
24-
25-
### Creating a client
26-
27-
You can create a client by importing the aptos-go-sdk, and creating a `Client`
28-
29-
```go
30-
package main
31-
32-
import (
33-
"fmt"
34-
"github.com/aptos-labs/aptos-go-sdk"
35-
)
36-
37-
func main() {
38-
client, err := aptos.NewClient(aptos.DevnetConfig)
39-
if err != nil {
40-
panic("Failed to create client:" + err.Error())
41-
}
42-
fmt.Println("client", client)
43-
}
44-
```
45-
46-
You can configure the network with the `aptos.NetworkConfig`, or use a
47-
preexisting `aptos.DevnetConfig`, `aptos.TestnetConfig`,
48-
or `aptos.MainnetConfig`
49-
50-
### Creating a private key
51-
52-
You can create a new `Ed25519` account's private key by
53-
calling `NewEd25519Account()`.
54-
55-
```go
56-
account, err := aptos.NewEd25519Account()
57-
if err != nil {
58-
return err
59-
}
60-
```
61-
62-
### Funding accounts
63-
64-
You can create and fund an account with a faucet on any network that is not
65-
mainnet
66-
67-
```go
68-
account, err := aptos.NewEd25519Account()
69-
err = client.Fund(account.Address, 100_000_000)
70-
```
71-
72-
### Sending a transaction
73-
74-
You can send a AptosCoin via a transaction
75-
76-
```go
77-
account, err := aptos.NewEd25519account()
78-
79-
// Build transaction
80-
signed_txn, err := aptos.APTTransferTransaction(client, account, AccountOne, 100)
81-
82-
// Submit transaction
83-
result, err := client.SubmitTransaction(signed_txn)
84-
hash := result["hash"].(string)
85-
86-
// Wait for the transaction
87-
_, err = client.WaitForTransaction(hash)
88-
89-
// Read transaction by hash
90-
txn, err := client.TransactionByHash(hash)
91-
```
92-
93-
### More examples
19+
## Usage
20+
21+
<Cards>
22+
<Card href="./go-sdk/fetch-data-via-sdk">
23+
<Card.Title>Fetching Data</Card.Title>
24+
<Card.Description>Learn how to fetch data with the Go SDK</Card.Description>
25+
</Card>
26+
<Card href="./go-sdk/building-transactions">
27+
<Card.Title>Submitting Transactions</Card.Title>
28+
<Card.Description>Learn how to submit transactions with the Go SDK</Card.Description>
29+
</Card>
30+
<Card href="./go-sdk/go-examples">
31+
<Card.Title>Examples</Card.Title>
32+
<Card.Description>Explore Go examples provided in the SDK</Card.Description>
33+
</Card>
34+
</Cards>
9435

95-
You can see more examples in
96-
the [`examples/` folder](https://github.com/aptos-labs/aptos-go-sdk/tree/main/examples)
97-
of the Go SDK repository
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default {
2+
"---usage---": {
3+
type: "separator",
4+
title: "Usage",
5+
},
6+
account: {
7+
title: "Accounts",
8+
},
9+
"fetch-data-via-sdk": {
10+
title: "Fetch Data",
11+
},
12+
"building-transactions": {
13+
title: "Transactions",
14+
},
15+
"go-examples": {
16+
title: "Example Code",
17+
},
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)