Skip to content

Commit

Permalink
Create a top-level crypto library
Browse files Browse the repository at this point in the history
Move various crypto bits to the top-level, so that we can reuse
it for the key, which will be added in a later commit.

Signed-off-by: Sean Young <[email protected]>
  • Loading branch information
Sean Young committed May 29, 2018
1 parent f5e0393 commit a722ba5
Show file tree
Hide file tree
Showing 118 changed files with 9,397 additions and 1,372 deletions.
34 changes: 15 additions & 19 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 12 additions & 28 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,20 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"

"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/crypto"
ptypes "github.com/hyperledger/burrow/permission/types"
"github.com/tendermint/go-wire"
)

var GlobalPermissionsAddress = Address(binary.Zero160)

// Signable is an interface for all signable things.
// It typically removes signatures before serializing.
type Signable interface {
WriteSignBytes(chainID string, w io.Writer, n *int, err *error)
}

// SignBytes is a convenience method for getting the bytes to sign of a Signable.
func SignBytes(chainID string, o Signable) []byte {
buf, n, err := new(bytes.Buffer), new(int), new(error)
o.WriteSignBytes(chainID, buf, n, err)
if *err != nil {
panic(fmt.Sprintf("could not write sign bytes for a signable: %s", *err))
}
return buf.Bytes()
}
var GlobalPermissionsAddress = crypto.Address(binary.Zero160)

type Addressable interface {
// Get the 20 byte EVM address of this account
Address() Address
Address() crypto.Address
// Public key from which the Address is derived
PublicKey() PublicKey
PublicKey() crypto.PublicKey
}

// The default immutable interface to an account
Expand Down Expand Up @@ -75,7 +59,7 @@ type Account interface {
type MutableAccount interface {
Account
// Set public key (needed for lazy initialisation), should also set the dependent address
SetPublicKey(pubKey PublicKey) MutableAccount
SetPublicKey(pubKey crypto.PublicKey) MutableAccount
// Subtract amount from account balance (will panic if amount is greater than balance)
SubtractFromBalance(amount uint64) (MutableAccount, error)
// Add amount to balance (will panic if amount plus balance is a uint64 overflow)
Expand All @@ -99,16 +83,16 @@ type MutableAccount interface {

// ConcreteAccount is the canonical serialisation and bash-in-place object for an Account
type ConcreteAccount struct {
Address Address
PublicKey PublicKey
Address crypto.Address
PublicKey crypto.PublicKey
Sequence uint64
Balance uint64
Code Bytecode
StorageRoot []byte
Permissions ptypes.AccountPermissions
}

func NewConcreteAccount(pubKey PublicKey) ConcreteAccount {
func NewConcreteAccount(pubKey crypto.PublicKey) ConcreteAccount {
return ConcreteAccount{
Address: pubKey.Address(),
PublicKey: pubKey,
Expand All @@ -122,7 +106,7 @@ func NewConcreteAccount(pubKey PublicKey) ConcreteAccount {
}

func NewConcreteAccountFromSecret(secret string) ConcreteAccount {
return NewConcreteAccount(PrivateKeyFromSecret(secret).PublicKey())
return NewConcreteAccount(crypto.PrivateKeyFromSecret(secret, crypto.CurveTypeEd25519).GetPublicKey())
}

// Return as immutable Account
Expand Down Expand Up @@ -230,11 +214,11 @@ type concreteAccountWrapper struct {

var _ Account = concreteAccountWrapper{}

func (caw concreteAccountWrapper) Address() Address {
func (caw concreteAccountWrapper) Address() crypto.Address {
return caw.ConcreteAccount.Address
}

func (caw concreteAccountWrapper) PublicKey() PublicKey {
func (caw concreteAccountWrapper) PublicKey() crypto.PublicKey {
return caw.ConcreteAccount.PublicKey
}

Expand Down Expand Up @@ -273,7 +257,7 @@ func (caw concreteAccountWrapper) MarshalJSON() ([]byte, error) {
// Account mutation via MutableAccount interface
var _ MutableAccount = concreteAccountWrapper{}

func (caw concreteAccountWrapper) SetPublicKey(pubKey PublicKey) MutableAccount {
func (caw concreteAccountWrapper) SetPublicKey(pubKey crypto.PublicKey) MutableAccount {
caw.ConcreteAccount.PublicKey = pubKey
addressFromPubKey := pubKey.Address()
// We don't want the wrong public key to take control of an account so we panic here
Expand Down
11 changes: 5 additions & 6 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (

"fmt"

"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/permission"
"github.com/hyperledger/burrow/permission/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
)

Expand All @@ -36,7 +36,7 @@ func TestAddress(t *testing.T) {
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
}
addr, err := AddressFromBytes(bs)
addr, err := crypto.AddressFromBytes(bs)
assert.NoError(t, err)
word256 := addr.Word256()
leadingZeroes := []byte{
Expand All @@ -45,7 +45,7 @@ func TestAddress(t *testing.T) {
0, 0, 0, 0,
}
assert.Equal(t, leadingZeroes, word256[:12])
addrFromWord256 := AddressFromWord256(word256)
addrFromWord256 := crypto.AddressFromWord256(word256)
assert.Equal(t, bs, addrFromWord256[:])
assert.Equal(t, addr, addrFromWord256)
}
Expand Down Expand Up @@ -118,11 +118,10 @@ func TestMarshalJSON(t *testing.T) {
acc := concreteAcc.Account()
bs, err := json.Marshal(acc)

pubKeyEd25519 := concreteAcc.PublicKey.PubKey.Unwrap().(crypto.PubKeyEd25519)
expected := fmt.Sprintf(`{"Address":"%s","PublicKey":{"type":"ed25519","data":"%X"},`+
expected := fmt.Sprintf(`{"Address":"%s","PublicKey":{"type":"ed25519","data":"%s"},`+
`"Sequence":0,"Balance":0,"Code":"3C172D","StorageRoot":"",`+
`"Permissions":{"Base":{"Perms":0,"SetBit":0},"Roles":[]}}`,
concreteAcc.Address, pubKeyEd25519[:])
concreteAcc.Address, concreteAcc.PublicKey)
assert.Equal(t, expected, string(bs))
assert.NoError(t, err)
}
Loading

0 comments on commit a722ba5

Please sign in to comment.