The Kin Go SDK enables developers use Kin inside their backend servers. It contains support for blockchain actions such as creating accounts, sending payments, as well a webhook handlers to assist with implementing Agora webhooks.
- Go
go get -u github.com/kinecosystem/kin-go
The SDK contains two main components, a Client
and web hook handlers. The Client
is used for blockchain actions, such as creating accounts sending payments, while the web hook handlers are meant for developers who wish to make
use of Agora Webhooks. For a high-level overview of using Agora, please refer to the website documentation.
The main component of this library is the Client
, which facilitates access to the Kin blockchain.
At a minimum, the client needs to be instantiated with an Environment
.
package main
import (
"github.com/kinecosystem/kin-go/client"
)
func main() {
client, err := client.New(client.EnvironmentTest)
}
Apps with registered app indexes should initialize the client with their index:
package main
import (
"github.com/kinecosystem/kin-go/client"
)
func main() {
client, err := client.New(client.EnvironmentTest, client.WithAppIndex(1))
}
The createAccount method creates an account with the provided private key.
account, err := client.NewPrivateKey()
err = client.CreateAccount(context.Background(), account)
The GetTransaction
method gets transaction data by transaction hash.
txHash, err := base58.Decode("")
transactionData, err := client.GetTransaction(context.Background(), txHash)
The GetBalance
method gets the balance of the provided account, in quarks
account, err := client.PublicKeyFromString("")
quarks, err = client.GetBalance(context.Background(), account)
The SubmitPayment
method submits the provided payment to Agora.
var sender client.PrivateKey
var dest client.PublicKey
txHash, err := client.SubmitPayment(context.Background(), client.Payment{
Sender: sender,
Destination: dest,
Type: kin.TransactionTypeEarn,
// Note: should use client.KinToQuarks when using non-constants.
Quarks: client.MustKinToQuarks("1.0"),
});
A Payment
has the following required properties:
Sender
: The private key of the account from which the payment will be sent.Destination
: The public key of the account to which the payment will be sent.Type
: The transaction type of the payment.Quarks
: The amount of the payment, in quarks.
Additionally, it has some optional properties:
Invoice
: An Invoice to associate with this payment. Cannot be set ifmemo
is set.Memo
A text memo to include in the transaction. Cannot be set ifinvoice
is set.WithSenderCreate()
can be provided to create a token account owned by the destination, if none exist.
The SubmitEarnBatch
method submits a batch of earns to Agora from a single account. It batches the earns into fewer
transactions where possible and submits as many transactions as necessary to submit all the earns.
var sender client.PrivateKey
var dest1, dest2 client.PublicKey
result, err := client.SubmitEarnBatch(context.Background(), client.EarnBatch{
Sender: sender,
Earns: []client.Earn{
{
Destination: dest1,
// Note: should use client.KinToQuarks when using non-constants.
Quarks: client.MustKinToQuarks("1.0"),
},
{
Destination: dest2,
// Note: should use client.KinToQuarks when using non-constants.
Quarks: client.MustKinToQuarks("1.0"),
},
},
})
A few examples for creating an account and different ways of submitting payments and batched earns can be found in examples/client
.
The SDK offers handler functions to assist developers with implementing the Agora webhooks
Only apps that have been assigned an app index can make use of Agora webhooks.
There are currently two handlers:
- Events with
EventsHandler
- Sign Transaction with
SignTransactionHandler
When configuring a webhook, a webhook secret can be specified.
To consume events from Agora:
import (
"http",
"github.com/kinecosystem/kin-go/client"
"github.com/kinecosystem/agora-common/webhook/events"
)
const webhookSecret = ""
func eventsHandler(events []events.Event) error {
// process events
}
// Note: If an empty secret is provided to the handler, all events will be processed.
// Otherwise, the request signature will be validated to ensure it came from agora.
http.HandleFunc("/events", client.EventsHandler(webhookSecret, eventsHandler))
To verify and sign transactions related to your app:
import (
"http",
"github.com/kinecosystem/kin-go/client"
"github.com/kinecosystem/agora-common/webhook/events"
)
const webhookSecret = ""
func signHandler(req client.SignTransactionRequest, resp* client.SignTransactionResponse) error {
// decide whether or not to sign() or reject() the request.
}
// Note: If an empty secret is provided to the handler, all events will be processed.
// Otherwise, the request signature will be validated to ensure it came from agora.
http.HandleFunc("/sign_transaction", client.SignTransactionHandler(webhookSecret, signHandler))
A simple example server implementing both the Events and Sign Transaction webhooks can be found in examples/webhook/main.go
.