Skip to content

Commit

Permalink
Merge pull request #16 from renproject/feat/marshal
Browse files Browse the repository at this point in the history
UTXO OutPoints can be marshalled and un-marshalled
  • Loading branch information
Nadimpalli Susruth authored Sep 11, 2019
2 parents 79ac1f6 + 405d654 commit 4c85a6d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ github.com/renproject/kv v0.1.0 h1:i4XpBL2vkKMpZz7dhYhaPf/I3/3DNfSZkz9GY96UryE=
github.com/renproject/kv v0.1.0/go.mod h1:1kC6F4r7jhMeyZtqc7Of047E/DpfHDphRg/bQHt08hA=
github.com/renproject/phi v0.1.0 h1:ZOn7QeDribk/uV46OhQWcTLxyuLg7P+xR1Hfl5cOQuI=
github.com/renproject/phi v0.1.0/go.mod h1:Hrxx2ONVpfByficRjyRd1trecalYr0lo7Z0akx8UXqg=
github.com/renproject/rzl v0.1.0 h1:/ToITTp5zB04Ghv7FYk9Tnt0vsEo7Gv4pyEtSBVT+Tk=
github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8=
github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
Expand Down
31 changes: 27 additions & 4 deletions types/btctypes/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package btctypes
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"

Expand Down Expand Up @@ -153,7 +154,7 @@ type outPoint struct {
}

func NewOutPoint(txHash types.TxHash, vout uint32) OutPoint {
return outPoint{
return &outPoint{
txHash: txHash,
vout: vout,
}
Expand All @@ -162,12 +163,12 @@ func NewOutPoint(txHash types.TxHash, vout uint32) OutPoint {
func ReadOutPoint(r io.Reader) (OutPoint, error) {
op := outPoint{}
if err := binary.Read(r, binary.LittleEndian, &op.txHash); err != nil {
return op, err
return &op, err
}
if err := binary.Read(r, binary.LittleEndian, &op.vout); err != nil {
return op, err
return &op, err
}
return op, nil
return &op, nil
}

func (op outPoint) TxHash() types.TxHash {
Expand All @@ -192,6 +193,28 @@ func (op outPoint) String() string {
return fmt.Sprintf("%s:%d", op.txHash, op.vout)
}

func (op outPoint) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
TxHash types.TxHash `json:"txHash"`
Vout uint32 `json:"vout"`
}{
TxHash: op.txHash,
Vout: op.vout,
})
}

func (op *outPoint) UnmarshalJSON(data []byte) error {
tmp := struct {
TxHash types.TxHash `json:"txHash"`
Vout uint32 `json:"vout"`
}{}
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
*op = outPoint{txHash: tmp.TxHash, vout: tmp.Vout}
return nil
}

const SigHashForkID txscript.SigHashType = 0x40

// calcBip143SignatureHash computes the sighash digest of a transaction's
Expand Down
24 changes: 24 additions & 0 deletions types/btctypes/utxo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package btctypes_test

import (
"encoding/json"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/renproject/mercury/types/btctypes"
)

var _ = Describe("UTXOs", func() {

Context("when marshaling an outpoint", func() {
It("should marshal and unmarshal into the same value", func() {
outpoint := NewOutPoint("something", 10)

data, err := json.Marshal(outpoint)
Expect(err).ShouldNot(HaveOccurred())
reconstructedOutPoint := NewOutPoint("", 0)
Expect(json.Unmarshal(data, &reconstructedOutPoint)).ShouldNot(HaveOccurred())
Expect(reconstructedOutPoint).To(Equal(outpoint))
})
})
})

0 comments on commit 4c85a6d

Please sign in to comment.