Git Product home page Git Product logo

minterteam / minter-go-sdk Goto Github PK

View Code? Open in Web Editor NEW
13.0 9.0 8.0 1.57 MB

Minter Blockchain Golang SDK, ๐Ÿ’ณ wallet, ๐Ÿงพ transactions, gRPC and HTTP clients ๐ŸŒ https://t.me/MinterGoSDK

Home Page: https://pkg.go.dev/mod/github.com/MinterTeam/minter-go-sdk/v2

License: MIT License

Go 100.00%
sdk golang transaction minter websockets openapi grpc blockchain minter-wallet signatures

minter-go-sdk's Introduction

Minter Logo

go.dev reference

Overview

This is a pure Go SDK for working with Minter blockchain

Table of contents

Installing

go get github.com/MinterTeam/minter-go-sdk/v2@latest

Minter API

Minter blockchain nodes have built-in API with grpc and http + websocket interfaces. This package will help you with forming requests, parsing responses, as well as when working with other API entities.

Using API v2

Package http_client package implements the API v2 methods usage interface.

go.dev reference

package http_client_test

import (
	"context"
	"github.com/MinterTeam/minter-go-sdk/v2/api"
	"github.com/MinterTeam/minter-go-sdk/v2/api/http_client"
	"github.com/MinterTeam/minter-go-sdk/v2/api/http_client/models"
	"github.com/MinterTeam/minter-go-sdk/v2/transaction"
	"github.com/MinterTeam/minter-go-sdk/v2/wallet"
	"io"
	"math/big"
	"time"
)

func main() {
	client, _ := http_client.New("http://localhost:8843/v2")
	coinID, _ := client.CoinID("SYMBOL")
	w, _ := wallet.Create("1 2 3 4 5 6 7 8 9 10 11 12", "")
	nonce, _ := client.Nonce(w.Address)
	data := transaction.NewSendData().SetCoin(coinID).SetValue(transaction.BipToPip(big.NewInt(1))).MustSetTo(w.Address)
	transactionsBuilder := transaction.NewBuilder(transaction.TestNetChainID)
	tx, _ := transactionsBuilder.NewTransaction(data)
	sign, _ := tx.SetNonce(nonce).Sign(w.PrivateKey)
	encode, _ := sign.Encode()
	hash, _ := sign.Hash()

	subscribeClient, _ := client.Subscribe(api.QueryHash(hash))
	defer subscribeClient.CloseSend()

	res, err := client.SendTransaction(encode)
	if err != nil {
		_, _, _ = client.ErrorBody(err)
	}
	if res.Code != 0 {
		panic(res.Log)
	}

	{
		recv, err := subscribeClient.Recv()
		if err == io.EOF {
			return
		}
		if err == context.Canceled || err == context.DeadlineExceeded {
			return
		}
		if err != nil {
			panic(err)
		}

		marshal, _ := client.Marshal(recv)
		findedTx, _ := api.SubscribeNewTxToTx(marshal)
		_, _ = findedTx.GetTransaction(), findedTx.Data().(*transaction.SendData)
	}
	// or
	{
		time.Sleep(5*time.Second)
		response, _ := client.Transaction(hash)
		_, _ = client.Marshal(response)
		sendData := new(models.SendData)
		_ = response.Data.UnmarshalTo(sendData)
		_, _ = client.Marshal(sendData)
	}
}

Using gRPC

Package grpc_client package implements the gRPC methods usage interface.

go.dev reference

package grpc_client_test

import (
	"github.com/MinterTeam/minter-go-sdk/v2/api"
	"github.com/MinterTeam/minter-go-sdk/v2/api/grpc_client"
	"github.com/MinterTeam/minter-go-sdk/v2/transaction"
	"github.com/MinterTeam/minter-go-sdk/v2/wallet"
	"github.com/MinterTeam/node-grpc-gateway/api_pb"
	grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"io"
	"math/big"
	"time"
)

func main() {
	client, _ := grpc_client.New("localhost:8842")
	coinID, _ := client.CoinID("SYMBOL")
	w, _ := wallet.Create("1 2 3 4 5 6 7 8 9 10 11 12", "")
	nonce, _ := client.Nonce(w.Address)
	data := transaction.NewSendData().SetCoin(coinID).SetValue(transaction.BipToPip(big.NewInt(1))).MustSetTo(w.Address)
	transactionsBuilder := transaction.NewBuilder(transaction.TestNetChainID)
	tx, _ := transactionsBuilder.NewTransaction(data)
	sign, _ := tx.SetNonce(nonce).Sign(w.PrivateKey)
	encode, _ := sign.Encode()
	hash, _ := sign.Hash()

	subscribeClient, _ := client.Subscribe(api.QueryHash(hash))
	defer subscribeClient.CloseSend()

	res, err := client.WithCallOption(
		grpc_retry.WithCodes(codes.FailedPrecondition),
		grpc_retry.WithBackoff(grpc_retry.BackoffLinear(1*time.Second)),
		grpc_retry.WithMax(4),
	).SendTransaction(encode)
	if err != nil {
		_, _, _ = client.ErrorBody(err)
	}
	if res.Code != 0 {
		panic(res.Log)
	}

	{
		recv, err := subscribeClient.Recv()
		if err == io.EOF {
			return
		}
		if code := status.Code(err); code != codes.OK {
			if code == codes.DeadlineExceeded || code == codes.Canceled {
				return
			}
			panic(err)
		}

		marshal, _ := client.Marshal(recv)
		findedTx, _ := api.SubscribeNewTxToTx(marshal)
		_, _ = findedTx.GetTransaction(), findedTx.Data().(*transaction.SendData)
	}
	// or
	{
		time.Sleep(5*time.Second)
		response, _ := client.Transaction(hash)
		_, _ = client.Marshal(response)
		sendData := new(api_pb.SendData)
		_ = response.Data.UnmarshalTo(sendData)
		_, _ = client.Marshal(sendData)
	}
}

Using Transactions

Package transaction is a guide and assistant in working with fields and types of transactions, creating signatures, encoding and decoding with RLP.

go.dev reference

Sign transaction

Returns a signed tx.

โš ๏ธ After sending the transaction, to make sure that the transaction was successfully committed on the blockchain, you need to find the transaction by hash and make sure that the status code is 0.

Single signature
Example
tx, _ := transaction.NewBuilder(transaction.TestNetChainID).NewTransaction(
  transaction.NewSendData().
    SetCoin(0).
    SetValue(transaction.BipToPip(big.NewInt(1))).
    MustSetTo("Mx1b685a7c1e78726c48f619c497a07ed75fe00483"),
)

signedTransaction, _ := tx.SetNonce(1).Sign("07bc17abdcee8b971bb8723e36fe9d2523306d5ab2d683631693238e0f9df142")

encode, _ := signedTransaction.Encode()
Multi signatures
Example
tx, _ := transaction.NewBuilder(transaction.TestNetChainID).NewTransaction(
  transaction.NewSendData().
  SetCoin(0).
  SetValue(transaction.BipToPip(big.NewInt(1))).
  MustSetTo("Mx1b685a7c1e78726c48f619c497a07ed75fe00483")
)

signedTx, _ := tx.SetNonce(1).SetMultiSignatureType().Sign(
  multisigAddress,
  "ae089b32e4e0976ca6888cb1023148bd1a9f1cc28c5d442e52e586754ff48d63",
  "b0a65cd84d57189b70d80fe0b3d5fa3ea6e02fa48041314a587a1f8fdba703d7",
  "4c8dbfb3258f383adf656c2131e5ed77ec482a36125db71fb49d29e0528ff2ba",
)

encode, _ := signedTx.Encode()

You can transfer the transaction to the remaining addresses

signedTx1, _ := tx.Sign(msigAddress, privateKey1)
encode, _ := signedTx.Encode()
// transfer encode transaction
signedTx1, _ = transaction.Decode(encode)
// and continue its signature by the remaining participants
signedTx12, _ := signedTx1.Sign(msigAddress, privateKey2)
signedTx123, _ := signedTx12.Sign(msigAddress, privateKey3)
encode, _ := signedTx123.Encode()

You can collect all signatures in one place without revealing the private key

signedTx1, _ := tx.Clone().Sign(msigAddress, "ae089b32e4e0976ca6888cb1023148bd1a9f1cc28c5d442e52e586754ff48d63")
signedTx2, _ := tx.Clone().Sign(msigAddress, "b0a65cd84d57189b70d80fe0b3d5fa3ea6e02fa48041314a587a1f8fdba703d7")
signedTx3, _ := tx.Clone().Sign(msigAddress, "4c8dbfb3258f383adf656c2131e5ed77ec482a36125db71fb49d29e0528ff2ba")
simpleSignatureData1, _ := signedTx1.SingleSignatureData()
simpleSignatureData2, _ := signedTx2.SingleSignatureData()
simpleSignatureData3, _ := signedTx3.SingleSignatureData()
signedTransaction, _ := tx.Clone().Sign(msigAddress)
signedTx123, _ := signedTransaction.AddSignature(simpleSignatureData1, simpleSignatureData2, simpleSignatureData3)

encode, _ := signedTx123.Encode()

Minter Wallet

Package wallet is a guide and assistant in working with addresses, mnemonic and initial phrases, private and public keys.

go.dev reference

minter-go-sdk's People

Contributors

danil-lashin avatar grkamil avatar klim0v avatar vyuldashev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minter-go-sdk's Issues

Currently the exported `coerce` function does not support having the pre release information as part of the returned data.

Currently the exported coerce function does not support having the pre release information as part of the returned data.

For coin craypto :

const { coerce } = require("semver")

// Current behaviour
coerce("1.9") // 1.9.0
coerce("1.9-dev") // throw

// Future behaviour
coerce("1.9-dev", { coercePrerelease: true }) // 1.9.0-dev

Originally posted by @Nokel81 in npm/node-semver#473

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.