Skip to content

Commit

Permalink
Change interface to private to avoid error on gomobile android
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Apr 22, 2020
1 parent 01d9314 commit b708206
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 33 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ func (c *Client) SignTransaction(tx *transaction.Transaction) error {
//
// Duration is changed to signed int for gomobile compatibility.
func (c *Client) NewNanoPay(recipientAddress, fee string, duration int) (*NanoPay, error) {
return NewNanoPay(c.wallet, c, recipientAddress, fee, duration)
return NewNanoPay(c, c.wallet, recipientAddress, fee, duration)
}

// NewNanoPayClaimer is a shortcut for NewNanoPayClaimer using this client as
Expand All @@ -1099,7 +1099,7 @@ func (c *Client) NewNanoPayClaimer(recipientAddress string, claimIntervalMs int3
if len(recipientAddress) == 0 {
recipientAddress = c.wallet.address
}
return NewNanoPayClaimer(recipientAddress, claimIntervalMs, onError, c)
return NewNanoPayClaimer(c, recipientAddress, claimIntervalMs, onError)
}

// GetNonce is the same as package level GetNonce, but using connected node as
Expand Down
4 changes: 2 additions & 2 deletions multiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ func (m *MultiClient) SignTransaction(tx *transaction.Transaction) error {
//
// Duration is changed to signed int for gomobile compatibility.
func (m *MultiClient) NewNanoPay(recipientAddress, fee string, duration int) (*NanoPay, error) {
return NewNanoPay(m.GetDefaultClient().wallet, m, recipientAddress, fee, duration)
return NewNanoPay(m, m.GetDefaultClient().wallet, recipientAddress, fee, duration)
}

// NewNanoPayClaimer is a shortcut for NewNanoPayClaimer using this multiclient
Expand All @@ -715,7 +715,7 @@ func (m *MultiClient) NewNanoPayClaimer(recipientAddress string, claimIntervalMs
if len(recipientAddress) == 0 {
recipientAddress = m.GetDefaultClient().wallet.address
}
return NewNanoPayClaimer(recipientAddress, claimIntervalMs, onError, m)
return NewNanoPayClaimer(m, recipientAddress, claimIntervalMs, onError)
}

// GetNonce is the same as package level GetNonce, but using connected node as
Expand Down
23 changes: 12 additions & 11 deletions nanopay.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const (
// NanoPay is a nano payment channel between a payer and recipient where the
// payment amount can increase monotonically.
type NanoPay struct {
rpcClient rpcClient
senderWallet *Wallet
rpcClient RPCClient
recipientAddress string
recipientProgramHash common.Uint160
fee common.Fixed64
Expand All @@ -45,9 +45,9 @@ type NanoPay struct {
// NanoPayClaimer accepts NanoPay updates and send the latest state to
// blockchain periodically.
type NanoPayClaimer struct {
rpcClient rpcClient
recipientAddress string
recipientProgramHash common.Uint160
rpcClient RPCClient

lock sync.Mutex
amount common.Fixed64
Expand All @@ -59,9 +59,10 @@ type NanoPayClaimer struct {
tx *transaction.Transaction
}

// NewNanoPay creates a NanoPay with a payer wallet, recipient wallet address,
// txn fee, duration in unit of blocks, and an optional rpc client.
func NewNanoPay(senderWallet *Wallet, rpcClient RPCClient, recipientAddress, fee string, duration int) (*NanoPay, error) {
// NewNanoPay creates a NanoPay with a rpcClient (client, multiclient or
// wallet), payer wallet, recipient wallet address, txn fee, duration in unit of
// blocks, and an optional rpc client.
func NewNanoPay(rpcClient rpcClient, senderWallet *Wallet, recipientAddress, fee string, duration int) (*NanoPay, error) {
programHash, err := common.ToScriptHash(recipientAddress)
if err != nil {
return nil, err
Expand All @@ -73,8 +74,8 @@ func NewNanoPay(senderWallet *Wallet, rpcClient RPCClient, recipientAddress, fee
}

np := &NanoPay{
senderWallet: senderWallet,
rpcClient: rpcClient,
senderWallet: senderWallet,
recipientAddress: recipientAddress,
recipientProgramHash: programHash,
fee: feeFixed64,
Expand Down Expand Up @@ -131,19 +132,19 @@ func (np *NanoPay) IncrementAmount(delta string) (*transaction.Transaction, erro
return tx, nil
}

// NewNanoPayClaimer creates a NanoPayClaimer with a given recipient wallet
// address, claim interval in millisecond, onError channel, and a rpcClient that
// is used for making RPC requests.
func NewNanoPayClaimer(recipientAddress string, claimIntervalMs int32, onError *OnError, rpcClient RPCClient) (*NanoPayClaimer, error) {
// NewNanoPayClaimer creates a NanoPayClaimer with a given rpcClient (client,
// multiclient or wallet), recipient wallet address, claim interval in
// millisecond, onError channel.
func NewNanoPayClaimer(rpcClient rpcClient, recipientAddress string, claimIntervalMs int32, onError *OnError) (*NanoPayClaimer, error) {
receiver, err := common.ToScriptHash(recipientAddress)
if err != nil {
return nil, err
}

npc := &NanoPayClaimer{
rpcClient: rpcClient,
recipientAddress: recipientAddress,
recipientProgramHash: receiver,
rpcClient: rpcClient,
}

go func() {
Expand Down
37 changes: 20 additions & 17 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
)

// Signer is the interface that can sign transactions.
type Signer interface {
type signer interface {
PubKey() []byte
SignTransaction(tx *transaction.Transaction) error
}

// RPCClient is the RPC client interface that implements most RPC methods
// (except a few ones that is supposed to call with seed node only).
type RPCClient interface {
type rpcClient interface {
GetNonce(txPool bool) (int64, error)
GetNonceByAddress(address string, txPool bool) (int64, error)
Balance() (*Amount, error)
Expand All @@ -35,9 +35,9 @@ type RPCClient interface {

// SignerRPCClient is a RPCClient that can also sign transactions and made RPC
// requests that requires signatures.
type SignerRPCClient interface {
Signer
RPCClient
type signerRPCClient interface {
signer
rpcClient
Transfer(address, amount string, config *TransactionConfig) (string, error)
RegisterName(name string, config *TransactionConfig) (string, error)
TransferName(name string, recipientPubKey []byte, config *TransactionConfig) (string, error)
Expand Down Expand Up @@ -283,8 +283,9 @@ func SendRawTransaction(txn *transaction.Transaction, config RPCConfigInterface)

// Transfer sends asset to a wallet address with a transaction fee. Amount is
// the string representation of the amount in unit of NKN to avoid precision
// loss. For example, "0.1" will be parsed as 0.1 NKN.
func Transfer(s SignerRPCClient, address, amount string, config *TransactionConfig) (string, error) {
// loss. For example, "0.1" will be parsed as 0.1 NKN. The signerRPCClient can
// be a client, multiclient or wallet.
func Transfer(s signerRPCClient, address, amount string, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
Expand Down Expand Up @@ -338,8 +339,9 @@ func Transfer(s SignerRPCClient, address, amount string, config *TransactionConf
// NKN with a given transaction fee. The name will be valid for 1,576,800 blocks
// (around 1 year). Register name currently owned by this pubkey will extend the
// duration of the name to current block height + 1,576,800. Registration will
// fail if the name is currently owned by another account.
func RegisterName(s SignerRPCClient, name string, config *TransactionConfig) (string, error) {
// fail if the name is currently owned by another account. The signerRPCClient
// can be a client, multiclient or wallet.
func RegisterName(s signerRPCClient, name string, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
Expand Down Expand Up @@ -376,8 +378,8 @@ func RegisterName(s SignerRPCClient, name string, config *TransactionConfig) (st

// TransferName transfers a name owned by this signer's pubkey to another public
// key with a transaction fee. The expiration height of the name will not be
// changed.
func TransferName(s SignerRPCClient, name string, recipientPubKey []byte, config *TransactionConfig) (string, error) {
// changed. The signerRPCClient can be a client, multiclient or wallet.
func TransferName(s signerRPCClient, name string, recipientPubKey []byte, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
Expand Down Expand Up @@ -413,8 +415,8 @@ func TransferName(s SignerRPCClient, name string, recipientPubKey []byte, config
}

// DeleteName deletes a name owned by this signer's pubkey with a given
// transaction fee.
func DeleteName(s SignerRPCClient, name string, config *TransactionConfig) (string, error) {
// transaction fee. The signerRPCClient can be a client, multiclient or wallet.
func DeleteName(s signerRPCClient, name string, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
Expand Down Expand Up @@ -453,10 +455,10 @@ func DeleteName(s SignerRPCClient, name string, config *TransactionConfig) (stri
// the same key pair and identifier will be able to receive messages from this
// topic. If this (identifier, public key) pair is already subscribed to this
// topic, the subscription expiration will be extended to current block height +
// duration.
// duration. The signerRPCClient can be a client, multiclient or wallet.
//
// Duration is changed to signed int for gomobile compatibility.
func Subscribe(s SignerRPCClient, identifier, topic string, duration int, meta string, config *TransactionConfig) (string, error) {
func Subscribe(s signerRPCClient, identifier, topic string, duration int, meta string, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
Expand Down Expand Up @@ -500,8 +502,9 @@ func Subscribe(s SignerRPCClient, identifier, topic string, duration int, meta s
}

// Unsubscribe from a topic for an identifier. Client using the same key pair
// and identifier will no longer receive messages from this topic.
func Unsubscribe(s SignerRPCClient, identifier, topic string, config *TransactionConfig) (string, error) {
// and identifier will no longer receive messages from this topic. The
// signerRPCClient can be a client, multiclient or wallet.
func Unsubscribe(s signerRPCClient, identifier, topic string, config *TransactionConfig) (string, error) {
config, err := MergeTransactionConfig(config)
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (w *Wallet) NewNanoPayClaimer(recipientAddress string, claimIntervalMs int3
if len(recipientAddress) == 0 {
recipientAddress = w.Address()
}
return NewNanoPayClaimer(recipientAddress, claimIntervalMs, onError, w)
return NewNanoPayClaimer(w, recipientAddress, claimIntervalMs, onError)
}

// GetNonce is the same as package level GetNonce, but using this wallet's
Expand Down

0 comments on commit b708206

Please sign in to comment.