Skip to content

Commit

Permalink
add neo3 wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
joeqian10 committed Aug 11, 2022
1 parent a9ebb76 commit c1a3aa1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
91 changes: 91 additions & 0 deletions wallet/neo3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2021 The poly network Authors
* This file is part of The poly network library.
*
* The poly network is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The poly network is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with The poly network . If not, see <http://www.gnu.org/licenses/>.
*/

package wallet

import (
"fmt"
"github.com/joeqian10/neo3-gogogo/crypto"
"github.com/joeqian10/neo3-gogogo/helper"
"github.com/joeqian10/neo3-gogogo/tx"
"github.com/joeqian10/neo3-gogogo/wallet"
"github.com/polynetwork/bridge-common/chains/neo3"
"github.com/polynetwork/bridge-common/log"
)

const EMPTY = ""

type Neo3Wallet struct {
sdk *neo3.SDK
*wallet.NEP6Wallet
config *Config
}

func NewNeo3Wallet(config *Config, sdk *neo3.SDK) (*Neo3Wallet, error) {
ps := helper.ProtocolSettings{
Magic: config.Neo3Magic,
AddressVersion: helper.DefaultAddressVersion,
}
w, err := wallet.NewNEP6Wallet(config.Neo3Path, &ps, nil, nil)
if err != nil {
return nil, fmt.Errorf("neo3.NewNEP6Wallet error: %v", err)
}
err = w.Unlock(config.Neo3Pwd)
if err != nil {
return nil, fmt.Errorf("neo3.NEP6Wallet.Unlock error: %v", err)
}
if len(w.Accounts) == 0 {
return nil, fmt.Errorf("no available account in neo3 wallet")
}
return &Neo3Wallet{sdk: sdk, NEP6Wallet: w, config: config}, nil
}

func (w *Neo3Wallet) SendTransaction(script []byte) (string, error) {
return w.SendTransactionWithAccount(script, w.Account())
}

func (w *Neo3Wallet) SendTransactionWithAccount(script []byte, account *wallet.NEP6Account) (string, error) {
wh := wallet.NewWalletHelperFromWallet(w.sdk.Node(), w.NEP6Wallet)
a := account.GetScriptHash()
gasAmount, err := wh.GetBalanceFromAccount(tx.GasToken, a)
if err != nil {
return EMPTY, fmt.Errorf("WalletHelper.GetBalanceFromAccount error: %v", err)
}
pair := wallet.AccountAndBalance{
Account: a,
Value: gasAmount,
}
trx, err := wh.MakeTransaction(script, nil, []tx.ITransactionAttribute{}, []wallet.AccountAndBalance{pair})
if err != nil {
return EMPTY, fmt.Errorf("WalletHelper.MakeTransaction: %v", err)
}
trx, err = wh.SignTransaction(trx, w.config.Neo3Magic)
if err != nil {
return EMPTY, fmt.Errorf("WalletHelper.SignTransaction: %v", err)
}
res := w.sdk.Node().SendRawTransaction(crypto.Base64Encode(trx.ToByteArray()))
if res.HasError() {
return EMPTY, fmt.Errorf("neo3.SendRawTransaction error: %s", res.GetErrorInfo())
}
hash := trx.GetHash().String()
log.Info("send neo3 transaction, hash: %s", hash)
return hash, nil
}

func (w *Neo3Wallet) Account() *wallet.NEP6Account {
return &w.Accounts[0]
}
6 changes: 3 additions & 3 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ type Config struct {
NetFee float64

// neo3 wallet
WalletPath string
WalletPwd string
NeoMagic uint32
Neo3Path string
Neo3Pwd string
Neo3Magic uint32

// ONT wallet
GasPrice uint64
Expand Down

0 comments on commit c1a3aa1

Please sign in to comment.