Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Starname (IOV) #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions chain/starname/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package starname

import "github.com/renproject/multichain/chain/cosmos"

type (
// Address re-exports cosmos-compatible address
Address = cosmos.Address

// AddressDecoder re-exports cosmos.AddressDecoder
AddressDecoder = cosmos.AddressDecoder

// AddressEncoder re-exports cosmos.AddressEncoder
AddressEncoder = cosmos.AddressEncoder

// AddressEncodeDecoder re-exports cosmos.AddressEncodeDecoder
AddressEncodeDecoder = cosmos.AddressEncodeDecoder
)

var (
// NewAddressDecoder re-exports cosmos.NewAddressDecoder
NewAddressDecoder = cosmos.NewAddressDecoder

// NewAddressEncoder re-exports cosmos.NewAddressEncoder
NewAddressEncoder = cosmos.NewAddressEncoder

// NewAddressEncodeDecoder re-exports cosmos.NewAddressEnodeDecoder
NewAddressEncodeDecoder = cosmos.NewAddressEncodeDecoder
)
23 changes: 23 additions & 0 deletions chain/starname/address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package starname_test

import (
"github.com/renproject/multichain"
"github.com/renproject/multichain/chain/starname"
"github.com/renproject/pack"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Starname", func() {
Context("when decoding address", func() {
Context("when decoding Starname address", func() {
It("should work", func() {
decoder := starname.NewAddressDecoder("star")
addrStr := "star1478t4fltj689nqu83vsmhz27quk7uggjwe96yk"
_, err := decoder.DecodeAddress(multichain.Address(pack.NewString(addrStr)))
Expect(err).ToNot(HaveOccurred())
})
})
})
})
38 changes: 38 additions & 0 deletions chain/starname/starname.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package starname

import (
"github.com/iov-one/iovns/app"
"github.com/renproject/multichain/api/account"
"github.com/renproject/multichain/chain/cosmos"
)

type (
// Client re-exports cosmos.Client
Client = cosmos.Client

// ClientOptions re-exports cosmos.ClientOptions
ClientOptions = cosmos.ClientOptions

// TxBuilderOptions re-exports cosmos.TxBuilderOptions
TxBuilderOptions = cosmos.TxBuilderOptions
)

var (
// DefaultClientOptions re-exports default cosmos-compatible client options
DefaultClientOptions = cosmos.DefaultClientOptions

// NewGasEstimator re-exports cosmos.NewGasEstimator
NewGasEstimator = cosmos.NewGasEstimator
)

// NewClient returns returns a new Client with starname codec
func NewClient(opts ClientOptions) *Client {
return cosmos.NewClient(opts, app.MakeCodec(), "star")
}

// NewTxBuilder returns an implementation of the transaction builder interface
// from the Cosmos Compat API, and exposes the functionality to build simple
// Starname transactions.
func NewTxBuilder(opts TxBuilderOptions, client *Client) account.TxBuilder {
return cosmos.NewTxBuilder(opts, client)
}
13 changes: 13 additions & 0 deletions chain/starname/starname_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package starname_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestStarname(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Starname (IOV) Suite")
}
126 changes: 126 additions & 0 deletions chain/starname/starname_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package starname_test

import (
"context"
"encoding/hex"
"os"

"github.com/renproject/id"
"github.com/renproject/multichain"
"github.com/renproject/multichain/api/address"
"github.com/renproject/multichain/chain/starname"
"github.com/renproject/pack"
"github.com/renproject/surge"
"github.com/tendermint/tendermint/crypto/secp256k1"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Starname", func() {
Context("when submitting transactions", func() {
Context("when sending IOV", func() {
It("should work", func() {
// create context for the test
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Load private key, and assume that the associated address has
// funds to spend. You can do this by setting IOV_PK to the
// value specified in the `./multichaindeploy/.env` file.
pkEnv := os.Getenv("IOV_PK")
if pkEnv == "" {
panic("IOV_PK is undefined")
}

pkBz, err := hex.DecodeString(pkEnv)
Expect(err).ToNot(HaveOccurred())

var pk secp256k1.PrivKeySecp256k1
copy(pk[:], pkBz)

var privKey id.PrivKey
err = surge.FromBinary(&privKey, pkBz)
Expect(err).NotTo(HaveOccurred())

addr := starname.Address(pk.PubKey().Address())

// random recipient
pkRecipient := secp256k1.GenPrivKey()
addrEncoder := starname.NewAddressEncoder("star")
recipient, err := addrEncoder.EncodeAddress(address.RawAddress(pack.Bytes(pkRecipient.PubKey().Address())))
Expect(err).NotTo(HaveOccurred())

// instantiate a new client and avoid a port collision with terra and
// set BroadcastMode to block to avoid having to loop on client.Tx()
client := starname.NewClient(
starname.DefaultClientOptions().
WithHost("http://0.0.0.0:46657").
WithBroadcastMode("block").
WithCoinDenom("tiov"),
)

nonce, err := client.AccountNonce(ctx, multichain.Address(addr.String()))
Expect(err).NotTo(HaveOccurred())

// create a new cosmos-compatible transaction builder
txBuilder := starname.NewTxBuilder(starname.TxBuilderOptions{
ChainID: "testnet",
}, client)

// build the transaction
payload := pack.NewBytes([]byte("multichain"))
amount := pack.NewU256FromU64(pack.U64(2000000))
tx, err := txBuilder.BuildTx(
ctx,
multichain.Address(addr.String()), // from
recipient, // to
amount, // amount
nonce, // nonce
pack.NewU256FromU64(pack.U64(200000)), // gas limit
pack.NewU256FromU64(pack.U64(1)), // gas price
pack.NewU256FromU64(pack.U64(1)), // gas cap
payload, // memo
)
Expect(err).NotTo(HaveOccurred())

// get the transaction bytes and sign it
sighashes, err := tx.Sighashes()
Expect(err).NotTo(HaveOccurred())
Expect(len(sighashes)).To(Equal(1))
hash := id.Hash(sighashes[0])
sig, err := privKey.Sign(&hash)
Expect(err).NotTo(HaveOccurred())
sigBytes, err := surge.ToBinary(sig)
Expect(err).NotTo(HaveOccurred())
sig65 := pack.Bytes65{}
copy(sig65[:], sigBytes)

// attach the signature to the transaction
pubKey := pk.PubKey().(secp256k1.PubKeySecp256k1)
err = tx.Sign(
[]pack.Bytes65{sig65},
pack.NewBytes(pubKey[:]),
)
Expect(err).NotTo(HaveOccurred())

// submit the transaction to the chain
txHash := tx.Hash()
err = client.SubmitTx(ctx, tx)
Expect(err).NotTo(HaveOccurred())

// We don't need to loop due to instant finality and broadcast
// mode "block". If err == nil then we were successfully able to
// use the multichain to construct and submit a Starname (IOV)
// transaction!
foundTx, confs, err := client.Tx(ctx, txHash)
Expect(err).NotTo(HaveOccurred())
Expect(confs.Uint64()).To(Equal(uint64(1)))
Expect(foundTx.Payload()).To(Equal(multichain.ContractCallData([]byte(string(payload)))))
Expect(foundTx.From()).To(Equal(multichain.Address(addr.String())))
Expect(foundTx.To()).To(Equal(recipient))
Expect(foundTx.Value()).To(Equal(amount))
})
})
})
})
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/filecoin-project/go-jsonrpc v0.1.2-0.20201008195726-68c6a2704e49
github.com/filecoin-project/go-state-types v0.0.0-20201013222834-41ea465f274f
github.com/filecoin-project/lotus v1.1.2
github.com/iov-one/iovns v0.9.7
github.com/ipfs/go-cid v0.0.7
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
github.com/multiformats/go-varint v0.0.6
Expand Down
Loading