Skip to content

Commit

Permalink
fix: transaction sign.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangguiguang committed Dec 15, 2023
1 parent c35cf8f commit 5501167
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
14 changes: 9 additions & 5 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"

"github.com/coming-chat/go-aptos/crypto/derivation"
"github.com/coming-chat/go-sui/v2/lib"
"github.com/coming-chat/go-sui/v2/sui_types"
"github.com/tyler-smith/go-bip39"
"golang.org/x/crypto/blake2b"
Expand Down Expand Up @@ -70,12 +69,17 @@ func (a *Account) Sign(data []byte) []byte {
}
}

func (a *Account) SignSecureWithoutEncode(msg lib.Base64Data, intent sui_types.Intent) (sui_types.Signature, error) {
signature, err := sui_types.NewSignatureSecure(
sui_types.NewIntentMessage(intent, msg), &a.KeyPair,
)
func (a *Account) SignSecureWithoutEncode(txnBytes []byte, intent sui_types.Intent) (sui_types.Signature, error) {
message := sui_types.NewIntentMessage(intent, bcsBytes(txnBytes))
signature, err := sui_types.NewSignatureSecure(message, &a.KeyPair)
if err != nil {
return sui_types.Signature{}, err
}
return signature, nil
}

type bcsBytes []byte

func (b bcsBytes) MarshalBCS() ([]byte, error) {
return b, nil
}
39 changes: 39 additions & 0 deletions client/account_sign_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package client

import (
"context"
"testing"

"github.com/coming-chat/go-sui/v2/account"
"github.com/coming-chat/go-sui/v2/types"
"github.com/stretchr/testify/require"
)

func TestAccountSignAndSend(t *testing.T) {
// ManualTest_AccountSignAndSend(t)
}

func ManualTest_AccountSignAndSend(t *testing.T) {
unsafeMnemonic := M1Mnemonic

account, err := account.NewAccountWithMnemonic(unsafeMnemonic)
require.Nil(t, err)
t.Log(account.Address)

cli := TestnetClient(t)
signer := SuiAddressNoErr(account.Address)
coins, err := cli.GetSuiCoinsOwnedByAddress(context.Background(), *signer)
require.Nil(t, err)
require.Greater(t, coins.TotalBalance().Int64(), SUI(0.01).Int64(), "insufficient balance")

coinIds := make([]suiObjectID, len(coins))
for i, c := range coins {
coinIds[i] = c.CoinObjectId
}
gasBudget := types.NewSafeSuiBigInt(uint64(10000000))
txn, err := cli.PayAllSui(context.Background(), *signer, *signer, coinIds, gasBudget)
require.Nil(t, err)

resp := executeTxn(t, cli, txn.TxBytes, account)
t.Log("txn digest =", resp.Digest)
}

0 comments on commit 5501167

Please sign in to comment.