代码拉取完成,页面将自动刷新
package sui
import (
"crypto/ed25519"
"encoding/hex"
"errors"
"gitee.com/fu-ce-wei/wallet-grpc/core/base"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/coming-chat/go-sui/v2/account"
"github.com/coming-chat/go-sui/v2/sui_types"
)
type Account struct {
account *account.Account
}
func NewAccountWithMnemonic(mnemonic string) (*Account, error) {
account, err := account.NewAccountWithMnemonic(mnemonic)
if err != nil {
return nil, err
}
return &Account{account: account}, nil
}
// rename for support android.
// Android cannot support both NewAccountWithMnemonic(string) and NewAccountWithPrivateKey(string)
func AccountWithPrivateKey(prikey string) (*Account, error) {
seed, err := types.HexDecodeString(prikey)
if err != nil {
return nil, err
}
if len(seed) != ed25519.SeedSize {
return nil, base.ErrInvalidPrivateKey
}
scheme, err := sui_types.NewSignatureScheme(0)
if err != nil {
return nil, err
}
account := account.NewAccount(scheme, seed)
return &Account{account: account}, nil
}
// MARK - Implement the protocol Account
// @return privateKey data
func (a *Account) PrivateKey() ([]byte, error) {
return a.account.KeyPair.PrivateKey()[:32], nil
}
// @return privateKey string that will start with 0x.
func (a *Account) PrivateKeyHex() (string, error) {
return types.HexEncodeToString(a.account.KeyPair.PrivateKey()[:32]), nil
}
// @return publicKey data
func (a *Account) PublicKey() []byte {
return a.account.KeyPair.PublicKey()
}
// @return publicKey string that will start with 0x.
func (a *Account) PublicKeyHex() string {
return types.HexEncodeToString(a.account.KeyPair.PublicKey())
}
func (a *Account) Address() string {
return a.account.Address
}
func (a *Account) Sign(message []byte, password string) ([]byte, error) {
return a.account.Sign(message), nil
}
func (a *Account) SignHex(messageHex string, password string) (*base.OptionalString, error) {
msg, err := types.HexDecodeString(messageHex)
if err != nil {
return nil, errors.New("Invalid message hex string")
}
signature := a.account.Sign(msg)
signString := hex.EncodeToString(signature)
return &base.OptionalString{Value: signString}, nil
}
// MARK - Implement the protocol AddressUtil
// @param publicKey can start with 0x or not.
func (a *Account) EncodePublicKeyToAddress(publicKey string) (string, error) {
return EncodePublicKeyToAddress(publicKey)
}
// @return publicKey that will start with 0x.
func (a *Account) DecodeAddressToPublicKey(address string) (string, error) {
return DecodeAddressToPublicKey(address)
}
func (a *Account) IsValidAddress(address string) bool {
return IsValidAddress(address)
}
func AsSuiAccount(account base.Account) *Account {
if r, ok := account.(*Account); ok {
return r
} else {
return nil
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。