1 Star 0 Fork 0

符策委/wallet-grpc

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
account.go 2.74 KB
一键复制 编辑 原始数据 按行查看 历史
符策委 提交于 2023-10-13 14:07 +08:00 . factor: mod master;
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
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fu-ce-wei/wallet-grpc.git
git@gitee.com:fu-ce-wei/wallet-grpc.git
fu-ce-wei
wallet-grpc
wallet-grpc
3f3ae683dd35

搜索帮助