Skip to content

Commit

Permalink
update broadcast and sign interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Plevako committed Apr 28, 2022
1 parent 07b05ab commit e61a307
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
14 changes: 8 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"github.com/scorum/scorum-go/key"

"github.com/scorum/scorum-go/apis/account_history"
"github.com/scorum/scorum-go/apis/betting"
"github.com/scorum/scorum-go/apis/blockchain_history"
Expand Down Expand Up @@ -60,23 +62,23 @@ func (client *Client) Close() error {
return client.cc.Close()
}

func (client *Client) BroadcastTransactionSynchronous(ctx context.Context, chainID []byte, operations []types.Operation, wifs ...string) (*network_broadcast.BroadcastResponse, error) {
stx, err := client.createSignedTransaction(ctx, chainID, operations, wifs...)
func (client *Client) BroadcastTransactionSynchronous(ctx context.Context, chainID []byte, operations []types.Operation, keys ...*key.PrivateKey) (*network_broadcast.BroadcastResponse, error) {
stx, err := client.createSignedTransaction(ctx, chainID, operations, keys...)
if err != nil {
return nil, err
}
return client.NetworkBroadcast.BroadcastTransactionSynchronous(ctx, stx.Transaction)
}

func (client *Client) BroadcastTransaction(ctx context.Context, chainID []byte, operations []types.Operation, wifs ...string) error {
stx, err := client.createSignedTransaction(ctx, chainID, operations, wifs...)
func (client *Client) BroadcastTransaction(ctx context.Context, chainID []byte, operations []types.Operation, keys ...*key.PrivateKey) error {
stx, err := client.createSignedTransaction(ctx, chainID, operations, keys...)
if err != nil {
return err
}
return client.NetworkBroadcast.BroadcastTransaction(ctx, stx.Transaction)
}

func (client *Client) createSignedTransaction(ctx context.Context, chainID []byte, operations []types.Operation, wifs ...string) (*sign.SignedTransaction, error) {
func (client *Client) createSignedTransaction(ctx context.Context, chainID []byte, operations []types.Operation, keys ...*key.PrivateKey) (*sign.SignedTransaction, error) {
props, err := client.Chain.GetChainProperties(ctx)
if err != nil {
return nil, fmt.Errorf("get chainID properties: %w", err)
Expand All @@ -100,7 +102,7 @@ func (client *Client) createSignedTransaction(ctx context.Context, chainID []byt
Expiration: &types.Time{Time: &expiration},
})

if err = stx.Sign(chainID, wifs...); err != nil {
if err = stx.Sign(chainID, keys...); err != nil {
return nil, fmt.Errorf("sign transaction: %w", err)
}

Expand Down
11 changes: 8 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"
"time"

"github.com/scorum/scorum-go/key"

"github.com/davecgh/go-spew/spew"
"github.com/scorum/scorum-go/sign"
rpc "github.com/scorum/scorum-go/transport"
Expand Down Expand Up @@ -83,15 +85,17 @@ func TestGetAccountHistory(t *testing.T) {
func TestClient_Broadcast_AccountWitnessVoteOperation(t *testing.T) {
client := newHTTPClient()

roselle := "5JwWJ2m2jGG9RPcpDix5AvkDzQZJoZvpUQScsDzzXWAKMs8Q6jH"
roselle, err := key.PrivateKeyFromString("5JwWJ2m2jGG9RPcpDix5AvkDzQZJoZvpUQScsDzzXWAKMs8Q6jH")
require.NoError(t, err)

ops := []types.Operation{
&types.AccountWitnessVoteOperation{
Account: "roselle",
Witness: "scorumwitness1",
Approve: true,
},
}
_, err := client.BroadcastTransactionSynchronous(context.Background(), sign.TestNetChainID, ops, roselle)
_, err = client.BroadcastTransactionSynchronous(context.Background(), sign.TestNetChainID, ops, roselle)
require.NotNil(t, err)

perr, ok := err.(*rpc.RPCError)
Expand All @@ -104,7 +108,8 @@ func TestClient_Broadcast_Transfer(t *testing.T) {
client := newHTTPClient()
amount, _ := types.AssetFromString("0.000009 SCR")

azucena := "5J7FEcpqc1sZ7ZbKx2kVvBHx2oTjWG2wMU2e2FYX85sGA2qu8KT"
azucena, err := key.PrivateKeyFromString("5J7FEcpqc1sZ7ZbKx2kVvBHx2oTjWG2wMU2e2FYX85sGA2qu8KT")
require.NoError(t, err)
ops := []types.Operation{
&types.TransferOperation{
From: "azucena",
Expand Down
12 changes: 11 additions & 1 deletion examples/deposits/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"syscall"
"time"

"github.com/scorum/scorum-go/key"

scorumgo "github.com/scorum/scorum-go"
"github.com/scorum/scorum-go/apis/account_history"
"github.com/scorum/scorum-go/apis/blockchain_history"
Expand All @@ -36,6 +38,8 @@ var (
mutex sync.Mutex
// history seq cursor
seq uint32

privKey *key.PrivateKey
)

type Deposit struct {
Expand All @@ -48,6 +52,12 @@ type Deposit struct {
}

func main() {
var err error
privKey, err = key.PrivateKeyFromString(paymentWIF)
if err != nil {
log.Fatalf("failed to decode paymentWIF: %s", err.Error())
}

deposits = map[string]*Deposit{
"dep1": {ID: "dep1", Account: "noelle", Balance: decimal.Zero},
"dep2": {ID: "dep2", Account: "gina", Balance: decimal.Zero},
Expand Down Expand Up @@ -214,7 +224,7 @@ func transfer(deposit *Deposit, amount types.Asset) {
}

// broadcast the transfer operation
resp, err := client.BroadcastTransactionSynchronous(context.Background(), sign.TestNetChainID, []types.Operation{&transferOp}, paymentWIF)
resp, err := client.BroadcastTransactionSynchronous(context.Background(), sign.TestNetChainID, []types.Operation{&transferOp}, privKey)
if err != nil {
log.Printf("failed to transfer %s to %s: %v", amount, deposit, err)
revertBalance()
Expand Down
13 changes: 4 additions & 9 deletions sign/signed_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,15 @@ func (tx *SignedTransaction) Digest(chainID []byte) ([]byte, error) {
return digest[:], nil
}

func (tx *SignedTransaction) Sign(chainID []byte, wifs ...string) error {
func (tx *SignedTransaction) Sign(chainID []byte, keys ...*key.PrivateKey) error {
digest, err := tx.Digest(chainID)
if err != nil {
return err
}

sigsHex := make([]string, len(wifs))
for i, wif := range wifs {
privKey, err := key.PrivateKeyFromString(wif)
if err != nil {
return err
}

sig := privKey.Sign(digest)
sigsHex := make([]string, len(keys))
for i, k := range keys {
sig := k.Sign(digest)
sigsHex[i] = hex.EncodeToString(sig)
}

Expand Down

0 comments on commit e61a307

Please sign in to comment.