Skip to content

Commit

Permalink
Merge pull request #18 from mysteriumnetwork/improvement/simply-promi…
Browse files Browse the repository at this point in the history
…se-signing

Hide promise signind details for issuer and receiver
  • Loading branch information
tadovas authored Dec 20, 2018
2 parents 011ce6e + 05e39a1 commit a7aaab0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion promises/clearing.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (pc *PromiseClearing) ClearReceivedPromise(promise *ReceivedPromise) error
copy(packedAddressAndSigns[10:32], addressAndSigns)

var extraDataHash [32]byte
copy(extraDataHash[:], promise.ConsumerHash())
copy(extraDataHash[:], promise.Extra.Hash())

_, err = pc.ClearPromise(
packedAddressAndSigns,
Expand Down
1 change: 1 addition & 0 deletions promises/clearing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestPromiseClearingEmitsClearedEvent(t *testing.T) {
backend.Commit()

promise := Promise{
Extra: EmptyExtra{},
Receiver: receiver.Address,
SeqNo: 1,
Amount: 100,
Expand Down
55 changes: 38 additions & 17 deletions promises/promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@ import (
"github.com/mysteriumnetwork/payments/identity"
)

type Promise struct {
ServiceConsumer common.Address
Receiver common.Address
SeqNo int64
Amount int64
type ExtraData interface {
Hash() []byte
}

const issuerPrefix = "Issuer prefix:"
const emptyExtra = "emptyextra"

type EmptyExtra struct {
}

func (EmptyExtra) Hash() []byte {
return crypto.Keccak256([]byte(emptyExtra))
}

func (p *Promise) ConsumerHash() []byte {
return crypto.Keccak256(p.ServiceConsumer.Bytes())
var _ ExtraData = EmptyExtra{}

type Promise struct {
Extra ExtraData
Receiver common.Address
SeqNo int64
Amount int64
}

const issuerPrefix = "Issuer prefix:"

func (p *Promise) Bytes() []byte {
slices := [][]byte{
[]byte(issuerPrefix),
p.ConsumerHash(),
p.Extra.Hash(),
p.Receiver.Bytes(),
abi.U256(big.NewInt(p.SeqNo)),
abi.U256(big.NewInt(p.Amount)),
Expand All @@ -42,13 +52,29 @@ type IssuedPromise struct {
IssuerSignature []byte
}

func (ip *IssuedPromise) Bytes() []byte {
return append([]byte(issuerPrefix), ip.Promise.Bytes()...)
}

func (ip *IssuedPromise) IssuerAddress() (common.Address, error) {
publicKey, err := crypto.Ecrecover(crypto.Keccak256(ip.Bytes()), ip.IssuerSignature)
if err != nil {
return common.Address{}, err
}
pubKey, err := crypto.UnmarshalPubkey(publicKey)
if err != nil {
return common.Address{}, err
}
return crypto.PubkeyToAddress(*pubKey), nil
}

type ReceivedPromise struct {
IssuedPromise
ReceiverSignature []byte
}

func SignByPayer(promise *Promise, payer identity.Signer) (*IssuedPromise, error) {
signature, err := payer.Sign(promise.Bytes())
signature, err := payer.Sign([]byte(issuerPrefix), promise.Bytes())
if err != nil {
return nil, err
}
Expand All @@ -62,15 +88,10 @@ func SignByPayer(promise *Promise, payer identity.Signer) (*IssuedPromise, error
const receiverPrefix = "Receiver prefix:"

func SignByReceiver(promise *IssuedPromise, receiver identity.Signer) (*ReceivedPromise, error) {
publicKey, err := crypto.Ecrecover(crypto.Keccak256(promise.Bytes()), promise.IssuerSignature)
if err != nil {
return nil, err
}
pubKey, err := crypto.UnmarshalPubkey(publicKey)
payerAddr, err := promise.IssuerAddress()
if err != nil {
return nil, err
}
payerAddr := crypto.PubkeyToAddress(*pubKey)
sig, err := receiver.Sign([]byte(receiverPrefix), crypto.Keccak256(promise.Bytes()), payerAddr.Bytes())
return &ReceivedPromise{
*promise,
Expand Down

0 comments on commit a7aaab0

Please sign in to comment.