Skip to content

Commit

Permalink
[go-sdk] Add docs to match the TypeScript SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario committed Aug 28, 2024
1 parent a72fda9 commit b79890e
Show file tree
Hide file tree
Showing 13 changed files with 1,371 additions and 79 deletions.
96 changes: 17 additions & 79 deletions apps/nextra/pages/en/build/sdks/go-sdk.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
title: "Go SDK"
---

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

# Aptos Go SDK

## Installing the Go SDK

<Callout type="warning">
The Go SDK is currently in a Beta, and its interfaces are subject to change
</Callout>

Aptos provides an official Go SDK in
the [aptos-go-sdk GitHub](https://github.com/aptos-labs/aptos-go-sdk) repository.
To use the Go SDK, get the main package here:
Expand All @@ -20,78 +16,20 @@ To use the Go SDK, get the main package here:
go get github.com/aptos-labs/aptos-go-sdk
```

## Using the Go SDK

### Creating a client

You can create a client by importing the aptos-go-sdk, and creating a `Client`

```go
package main

import (
"fmt"
"github.com/aptos-labs/aptos-go-sdk"
)

func main() {
client, err := aptos.NewClient(aptos.DevnetConfig)
if err != nil {
panic("Failed to create client:" + err.Error())
}
fmt.Println("client", client)
}
```

You can configure the network with the `aptos.NetworkConfig`, or use a
preexisting `aptos.DevnetConfig`, `aptos.TestnetConfig`,
or `aptos.MainnetConfig`

### Creating a private key

You can create a new `Ed25519` account's private key by
calling `NewEd25519Account()`.

```go
account, err := aptos.NewEd25519Account()
if err != nil {
return err
}
```

### Funding accounts

You can create and fund an account with a faucet on any network that is not
mainnet

```go
account, err := aptos.NewEd25519Account()
err = client.Fund(account.Address, 100_000_000)
```

### Sending a transaction

You can send a AptosCoin via a transaction

```go
account, err := aptos.NewEd25519account()

// Build transaction
signed_txn, err := aptos.APTTransferTransaction(client, account, AccountOne, 100)

// Submit transaction
result, err := client.SubmitTransaction(signed_txn)
hash := result["hash"].(string)

// Wait for the transaction
_, err = client.WaitForTransaction(hash)

// Read transaction by hash
txn, err := client.TransactionByHash(hash)
```

### More examples
## Usage

<Cards>
<Card href="./go-sdk/fetch-data-via-sdk">
<Card.Title>Fetching Data</Card.Title>
<Card.Description>Learn how to fetch data with the Go SDK</Card.Description>
</Card>
<Card href="./go-sdk/building-transactions">
<Card.Title>Submitting Transactions</Card.Title>
<Card.Description>Learn how to submit transactions with the Go SDK</Card.Description>
</Card>
<Card href="./go-sdk/go-examples">
<Card.Title>Examples</Card.Title>
<Card.Description>Explore Go examples provided in the SDK</Card.Description>
</Card>
</Cards>

You can see more examples in
the [`examples/` folder](https://github.com/aptos-labs/aptos-go-sdk/tree/main/examples)
of the Go SDK repository
18 changes: 18 additions & 0 deletions apps/nextra/pages/en/build/sdks/go-sdk/_meta.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default {
"---usage---": {
type: "separator",
title: "Usage",
},
account: {
title: "Accounts",
},
"fetch-data-via-sdk": {
title: "Fetch Data",
},
"building-transactions": {
title: "Transactions",
},
"go-examples": {
title: "Example Code",
},
};
93 changes: 93 additions & 0 deletions apps/nextra/pages/en/build/sdks/go-sdk/account.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
title: "Go SDK - Creating and Managing Accounts"
---

# Creating and Managing Accounts

There are several ways to generate account credentials using the Go SDK. You can
use:
- `aptos.NewEd25519Account()`
- `aptos.NewSecp256k1Account()`
- `aptos.NewEd25519SingleSenderAccount()`
- `aptos.NewAccountFromSigner()`

`Account.NewEd25519Account()` is the most commonly used method to create keys
for a new account. It defaults to `ED25519` key types, but you can also specify
which signing scheme you would prefer like so:

```go
// To derive an ed25519 account
account1 := aptos.NewEd25519Account()

// To derive a secp256k1 account
account2 := aptos.NewSecp256k1Account()
```

Once you have generated credentials, you **must** fund it for the network to know it exists.

In test environments this can be done with a faucet by running the following command:

```go filename="fund.go"
client, err = aptos.NewClient(aptos.DevnetConfig)
if err != nil {
panic("Failed to create client:" + err.Error())
}

// Fund an account with 1 Devnet APT
client.Fund(account1.Address, 100_000_000)
```

## Other Ways To Represent Accounts
If you have a private key, or equivalent representation, you can use that to
create an `Account` struct to manage those credentials while using the Go SDK.

Here are several examples that show how to do so with specific encoding schemes.

### Derive an account from private key

The SDK supports deriving an account from a private key with `NewAccountFromSigner()` method.
In addition, this method supports deriving an account from a private key and account address.
This method uses a local calculation and therefore is used to derive an `Account` that has not had its authentication key rotated.

```go
// to derive an account with a Ed25519 key scheme
privateKey := &aptos.Ed25519PrivateKey{}
err := privateKey.FromHex(privateKeyHex)
if err != nil {
panic("Failed to parse private key:" + err.Error())
}
account := aptos.NewAccountFromSigner(privateKey)

// to derive an account with a Single Sender Ed25519 key scheme
privateKey := &aptos.Ed25519PrivateKey{}
err := privateKey.FromHex(privateKeyHex)
if err != nil {
panic("Failed to parse private key:" + err.Error())
}
singleSigner := &crypto.SingleSigner{Signer: privateKey}
account := aptos.NewAccountFromSigner(singleSigner)

// to derive an account with a Single Sender Secp256k1 key scheme
privateKey := &aptos.Secp256k1PrivateKey{}
err := privateKey.FromHex(privateKeyHex)
if err != nil {
panic("Failed to parse private key:" + err.Error())
}
singleSigner := &crypto.SingleSigner{Signer: privateKey}
account := aptos.NewAccountFromSigner(singleSigner)

// to derive an account with a private key and account address
address := &aptos.AccountAddress{}
err := address.ParseStringRelaxed(addressHex)
if err != nil {
panic("Failed to parse address:" + err.Error())
}
privateKey := &aptos.Ed25519PrivateKey{}
err := privateKey.FromHex(privateKeyHex)
if err != nil {
panic("Failed to parse private key:" + err.Error())
}
account := aptos.NewAccountFromSigner(privateKey, address.AuthKey())
```

{/* TODO: Once derivation path is supported ### Derive an account from derivation path */}
Loading

0 comments on commit b79890e

Please sign in to comment.