Git Product home page Git Product logo

Comments (3)

yihau avatar yihau commented on August 20, 2024 1

Hi!
For now you can use the code below

package main

import (
	"context"
	"crypto/ed25519"
	"fmt"

	"github.com/portto/solana-go-sdk/client"
	"github.com/portto/solana-go-sdk/client/rpc"
	"github.com/portto/solana-go-sdk/common"
	"github.com/portto/solana-go-sdk/types"
)

func main() {
	// connection
	c := client.NewClient(rpc.DevnetRPCEndpoint)

	// fee payer
	feePayer := types.AccountFromPrivateKeyBytes([]byte{})

	// compose message
	message := types.NewMessage(
		feePayer.PublicKey,
		[]types.Instruction{},
		"RECNET BLOCK HASH HERE",
	)

	// serialize message for signing
	serilizedMessage, err := message.Serialize()
	if err != nil {
		// handle error here
	}

	// create tx with signatures
	tx, err := types.CreateTransaction(
		message,
		map[common.PublicKey]types.Signature{
			feePayer.PublicKey: ed25519.Sign(feePayer.PrivateKey, serilizedMessage),
			// other pubkey: other sig
		})
	if err != nil {
		// handle error here
	}

	// serialize tx
	rawTx, err := tx.Serialize()
	if err != nil {
		// handle error here
	}

	// send it
	sig, err := c.SendRawTransaction(context.Background(), rawTx)
	if err != nil {
		// handle error here
	}
	fmt.Println(sig)
}

I will refactor for this steps. I think it is so clumsy.

from solana-go-sdk.

Lazar955 avatar Lazar955 commented on August 20, 2024 1

Thanks for the fast response, yep that's exactly what I am looking for!

from solana-go-sdk.

yihau avatar yihau commented on August 20, 2024 1

I've added a new way to make the process more elegant at v1.7.0

package main

import (
	"context"
	"fmt"

	"github.com/portto/solana-go-sdk/client"
	"github.com/portto/solana-go-sdk/client/rpc"
	"github.com/portto/solana-go-sdk/common"
	"github.com/portto/solana-go-sdk/program/sysprog"
	"github.com/portto/solana-go-sdk/types"
)

var feePayer = types.AccountFromPrivateKeyBytes()

func main() {
	// connection
	c := client.NewClient(rpc.DevnetRPCEndpoint)

	a := types.NewAccount()

	message := types.NewMessage(
		feePayer.PublicKey,
		[]types.Instruction{
			sysprog.CreateAccount(
				feePayer.PublicKey,
				a.PublicKey,
				common.SystemProgramID,
				1e9,
				1,
			),
		},
		"RECENT BLOCK HASH HERE",
	)
	rawMessage, err := message.Serialize()
	if err != nil {
		// handle error
	}

	// you can pass signer here 
	tx, err := types.NewTransaction(message, []types.Account{feePayer})
	if err != nil {
		// handle error
	}

	// or add signature later!
	tx.AddSignature(a.Sign(rawMessage))

	sig, err := c.SendTransaction2(context.Background(), tx)
	if err != nil {
		// handle error
	}
	fmt.Println(sig)
}

there are two new function

  • AddSignature => you can add extra sig to tx directly, no pubkey => sig anymore 🎉
  • SendTransaction2 => you can pass tx struct to it and it will send the tx.

from solana-go-sdk.

Related Issues (20)

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.