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

all: Add Protocol 19 XDR and update StrKey to support Signed Payloads #4279

Merged
merged 24 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
51a51f7
Add stable Protocol 19 XDR
Shaptic Mar 14, 2022
07d6b7e
Update codebase to use new XDR for predicates/timebounds
Shaptic Mar 14, 2022
4a76ed8
Add condition types where appropriate
Shaptic Mar 14, 2022
e176717
Add stringification method for Preconditions
Shaptic Mar 15, 2022
d25222f
Add helper to upgrade timebounds correctly
Shaptic Mar 15, 2022
c31d001
Add encode/decode helper for CAP-40 signed payloads
Shaptic Mar 15, 2022
c660957
Update SignerKey helpers to account for signed payloads
Shaptic Mar 15, 2022
ec466d1
Correct maxPayload: it should include length bytes
Shaptic Mar 15, 2022
f6609ab
Add DB migration: signers can be longer now
Shaptic Mar 15, 2022
1101eda
Improve signed payload length tests, fix migration
Shaptic Mar 15, 2022
1755132
Add reliable way to retrieve txenv timebounds
Shaptic Mar 16, 2022
07d3e31
Disable generating the V3 extension on accounts
Shaptic Mar 16, 2022
f9285b6
Make precondition stringification prettier
Shaptic Mar 16, 2022
278c89b
Various code cleanups
Shaptic Mar 16, 2022
41656fa
Rename helpers and attach to appropriate object
Shaptic Mar 17, 2022
8a135fd
Add descriptions and subtests
Shaptic Mar 17, 2022
273fa99
Use XDR marshalling to simplify signed payload packing
Shaptic Mar 17, 2022
09b9954
Drop unnecessary function.. refactors! \o/
Shaptic Mar 17, 2022
86943d5
Incorporate PR feedback
Shaptic Mar 18, 2022
1e35b18
Make signer/payload private
Shaptic Mar 18, 2022
f542bbd
Use Buffer() since not all keys are the same length
Shaptic Mar 18, 2022
7299c45
Revert "Use Buffer() since not all keys are the same length"
Shaptic Mar 18, 2022
ef43250
Replace the known-size buffer in a more direct way
Shaptic Mar 18, 2022
78f96e0
Avoid wasting an allocation
Shaptic Mar 18, 2022
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
847 changes: 756 additions & 91 deletions gxdr/xdr_generated.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion network/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ func HashTransactionV0(tx xdr.TransactionV0, passphrase string) ([32]byte, error
if err != nil {
return [32]byte{}, err
}

v1Tx := xdr.Transaction{
SourceAccount: sa,
Fee: tx.Fee,
Memo: tx.Memo,
Operations: tx.Operations,
SeqNum: tx.SeqNum,
TimeBounds: tx.TimeBounds,
Cond: xdr.NewPreconditionsWithTimeBounds(tx.TimeBounds),
}
return HashTransaction(v1Tx, passphrase)
}
Expand Down
3 changes: 2 additions & 1 deletion network/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func TestHashTransaction(t *testing.T) {
Memo: txe.Memo(),
Operations: txe.Operations(),
SeqNum: xdr.SequenceNumber(txe.SeqNum()),
TimeBounds: txe.TimeBounds(),
Cond: xdr.NewPreconditionsWithTimeBounds(txe.TimeBounds()),
}

actual, err = HashTransaction(tx, TestNetworkPassphrase)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
Expand Down
9 changes: 6 additions & 3 deletions services/horizon/internal/db2/history/fee_bump_scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,12 @@ func FeeBumpScenario(tt *test.T, q *Q, successful bool) FeeBumpFixture {
Type: xdr.MemoTypeMemoNone,
},
SeqNum: 97,
TimeBounds: &xdr.TimeBounds{
MinTime: 2,
MaxTime: 4,
Cond: xdr.Preconditions{
Type: xdr.PreconditionTypePrecondTime,
TimeBounds: &xdr.TimeBounds{
MinTime: 2,
MaxTime: 4,
},
},
Operations: []xdr.Operation{
{
Expand Down
23 changes: 23 additions & 0 deletions services/horizon/internal/db2/schema/bindata.go

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- +migrate Up

-- CAP-40 signed payload strkeys can be 165 characters long, see
-- strkey/main.go:maxEncodedSize
ALTER TABLE accounts_signers
ALTER COLUMN signer TYPE character varying(165);

-- +migrate Down
ALTER TABLE accounts_signers
ALTER COLUMN signer TYPE character varying(64);
Original file line number Diff line number Diff line change
Expand Up @@ -2120,7 +2120,7 @@ func (s *CreateClaimableBalanceEffectsTestSuite) SetupTest() {
},
},
}
relBefore := xdr.Int64(1000)
relBefore := xdr.Duration(1000)
cb1 := xdr.ClaimableBalanceEntry{
BalanceId: balanceIDOp1,
Amount: xdr.Int64(200000000),
Expand Down Expand Up @@ -2218,7 +2218,7 @@ func (s *CreateClaimableBalanceEffectsTestSuite) SetupTest() {
}
}
func (s *CreateClaimableBalanceEffectsTestSuite) TestEffects() {
relBefore := xdr.Int64(1000)
relBefore := xdr.Duration(1000)
testCases := []struct {
desc string
op xdr.Operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (p *SignersProcessor) Commit(ctx context.Context) error {
sponsor,
)
if err != nil {
return errors.Wrap(err, "Error inserting a signer")
return errors.Wrapf(err, "Error inserting a signer (%s)", signer)
}

if rowsAffected != 1 {
Expand Down
3 changes: 3 additions & 0 deletions services/horizon/internal/ingest/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func genAccount(tt *test.T, gen randxdr.Generator) xdr.LedgerEntryChange {
{randxdr.FieldEquals("created.data.account.ext.v1.ext.v2.numSponsoring"), randxdr.SetPositiveNum32},
{randxdr.FieldEquals("created.data.account.ext.v1.ext.v2.numSponsored"), randxdr.SetPositiveNum32},
{randxdr.FieldEquals("created.data.account.ext.v1.ext.v2.signerSponsoringIDs"), randxdr.SetVecLen(numSigners)},
{randxdr.FieldEquals("created.data.account.ext.v1.ext.v2.ext.v"), randxdr.SetU32(0)},
// {randxdr.FieldEquals("created.data.account.ext.v1.ext.v2.ext.v3.seqLedger"), randxdr.SetPositiveNum32},
// {randxdr.FieldEquals("created.data.account.ext.v1.ext.v2.ext.v3.seqTime"), randxdr.SetPositiveNum64},
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func TestPopulateClaimableBalance(t *testing.T) {
unconditional := &xdr.ClaimPredicate{
Type: xdr.ClaimPredicateTypeClaimPredicateUnconditional,
}
relBefore := xdr.Int64(12)
absBefore := xdr.Int64(1598440539)
relBefore := xdr.Duration(12)
absBefore := xdr.TimePoint(1598440539)

id, err := xdr.MarshalHex(&balanceID)
tt.NoError(err)
Expand Down
19 changes: 19 additions & 0 deletions strkey/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ func TestDecode(t *testing.T) {
0xb7, 0xd3, 0x73, 0x8d, 0x18, 0x55, 0xf3, 0x63,
},
},
{
Name: "SignedPayload",
Address: "PA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAAAAAEQAAAAAAAAAAAAAAAAAABBXA",
ExpectedVersionByte: VersionByteSignedPayload,
ExpectedPayload: []byte{
// ed25519 (same as test case 1)
0x36, 0x3e, 0xaa, 0x38, 0x67, 0x84, 0x1f, 0xba,
0xd0, 0xf4, 0xed, 0x88, 0xc7, 0x79, 0xe4, 0xfe,
0x66, 0xe5, 0x6a, 0x24, 0x70, 0xdc, 0x98, 0xc0,
0xec, 0x9c, 0x07, 0x3d, 0x05, 0xc7, 0xb1, 0x03,
// payload length
0x00, 0x00, 0x00, 0x09,
// payload
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
// padding
0x00, 0x00, 0x00,
},
},
}

for _, kase := range cases {
Expand Down
31 changes: 23 additions & 8 deletions strkey/encode_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package strkey

import (
"crypto/rand"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -69,6 +70,25 @@ func TestEncode(t *testing.T) {
},
Expected: "XBU2RRGLXH3E5CQHTD3ODLDF2BWDCYUSSBLLZ5GNW7JXHDIYKXZWGTOG",
},
{
Name: "SignedPayload",
VersionByte: VersionByteSignedPayload,
Payload: []byte{
// ed25519 (same as test case 1)
0x36, 0x3e, 0xaa, 0x38, 0x67, 0x84, 0x1f, 0xba,
0xd0, 0xf4, 0xed, 0x88, 0xc7, 0x79, 0xe4, 0xfe,
0x66, 0xe5, 0x6a, 0x24, 0x70, 0xdc, 0x98, 0xc0,
0xec, 0x9c, 0x07, 0x3d, 0x05, 0xc7, 0xb1, 0x03,
// payload length
0x00, 0x00, 0x00, 0x09,
// payload
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
// padding
0x00, 0x00, 0x00,
},
Expected: "PA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAAAAAEQAAAAAAAAAAAAAAAAAABBXA",
},
}

for _, kase := range cases {
Expand All @@ -83,13 +103,8 @@ func TestEncode(t *testing.T) {
assert.Error(t, err)

// test too long payload
_, err = Encode(VersionByteAccountID, []byte{
0x69, 0xa8, 0xc4, 0xcb, 0xb9, 0xf6, 0x4e, 0x8a,
0x07, 0x98, 0xf6, 0xe1, 0xac, 0x65, 0xd0, 0x6c,
0x31, 0x62, 0x92, 0x90, 0x56, 0xbc, 0xf4, 0xcd,
0xb7, 0xd3, 0x73, 0x8d, 0x18, 0x55, 0xf3, 0x63,
0x69, 0xa8, 0xc4, 0xcb, 0xb9, 0xf6, 0x4e, 0x8a,
0x01,
})
longPayload := make([]byte, maxPayloadSize+1)
rand.Read(longPayload)
_, err = Encode(VersionByteAccountID, longPayload)
assert.EqualError(t, err, "data exceeds maximum payload size for strkey")
}
16 changes: 11 additions & 5 deletions strkey/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ const (
//VersionByteHashX is the version byte used for encoded stellar hashX
//signer keys.
VersionByteHashX = 23 << 3 // Base32-encodes to 'X...'

//VersionByteSignedPayload is the version byte used for encoding "signed
//payload" (CAP-40) signer keys.
VersionByteSignedPayload = 15 << 3 // Base-32 encodes to 'P'
)

// maxPayloadSize is the maximum length of the payload for all versions.
const maxPayloadSize = 40
// maxPayloadSize is the maximum length of the payload for all versions. The
// largest strkey is a signed payload: 32-byte public key + 4-byte payload
// length + 64-byte payload
const maxPayloadSize = 100

// maxRawSize is the maximum length of a strkey in its raw form not encoded.
const maxRawSize = 1 + maxPayloadSize + 2
const maxRawSize = 1 + /* version byte */ maxPayloadSize + 2 /* checksum */

// maxEncodedSize is the maximum length of a strkey when base32 encoded.
const maxEncodedSize = (maxRawSize*8 + 4) / 5 // (8n+4)/5 is the EncodedLen for no padding
Expand Down Expand Up @@ -110,7 +116,7 @@ func Decode(expected VersionByte, src string) ([]byte, error) {
return nil, err
}

// if we made it through the gaunlet, return the decoded value
// if we made it through the gauntlet, return the decoded value
return payload, nil
}

Expand Down Expand Up @@ -182,7 +188,7 @@ func Version(src string) (VersionByte, error) {
// is not one of the defined valid version byte constants.
func checkValidVersionByte(version VersionByte) error {
switch version {
case VersionByteAccountID, VersionByteMuxedAccount, VersionByteSeed, VersionByteHashTx, VersionByteHashX:
case VersionByteAccountID, VersionByteMuxedAccount, VersionByteSeed, VersionByteHashTx, VersionByteHashX, VersionByteSignedPayload:
return nil
default:
return ErrInvalidVersionByte
Expand Down
5 changes: 5 additions & 0 deletions strkey/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func TestVersion(t *testing.T) {
Address: "MBU2RRGLXH3E5CQHTD3ODLDF2BWDCYUSSBLLZ5GNW7JXHDIYKXZWGTOG",
ExpectedVersionByte: VersionByte(0x60),
},
{
Name: "Signed Payload",
Address: "PDPYP7E6NEYZSVOTV6M23OFM2XRIMPDUJABHGHHH2Y67X7JL25GW6AAAAAAAAAAAAAAJEVA",
ExpectedVersionByte: VersionByteSignedPayload,
},
}

for _, kase := range cases {
Expand Down
81 changes: 81 additions & 0 deletions strkey/signed_payload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package strkey

import (
"bytes"

xdr "github.com/stellar/go-xdr/xdr3"
"github.com/stellar/go/support/errors"
)

type SignedPayload struct {
signer string
payload []byte
}

const maxPayloadLen = 64

// NewSignedPayload creates a signed payload from an account ID (G... address)
// and a payload. The payload buffer is copied directly into the structure, so
// it should not be modified after construction.
func NewSignedPayload(signerPublicKey string, payload []byte) (*SignedPayload, error) {
if len(payload) > maxPayloadLen {
return nil, errors.Errorf("payload length %d exceeds max %d",
len(payload), maxPayloadLen)
}

return &SignedPayload{signer: signerPublicKey, payload: payload}, nil
}

// Encode turns a signed payload structure into its StrKey equivalent.
func (sp *SignedPayload) Encode() (string, error) {
signerBytes, err := Decode(VersionByteAccountID, sp.Signer())
if err != nil {
return "", errors.Wrap(err, "failed to decode signed payload signer")
}

b := new(bytes.Buffer)
b.Write(signerBytes)
xdr.Marshal(b, sp.Payload())

strkey, err := Encode(VersionByteSignedPayload, b.Bytes())
if err != nil {
return "", errors.Wrap(err, "failed to encode signed payload")
}
return strkey, nil
}

func (sp *SignedPayload) Signer() string {
return sp.signer
}

func (sp *SignedPayload) Payload() []byte {
return sp.payload
}

// DecodeSignedPayload transforms a P... signer into a `SignedPayload` instance.
func DecodeSignedPayload(address string) (*SignedPayload, error) {
raw, err := Decode(VersionByteSignedPayload, address)
if err != nil {
return nil, errors.New("invalid signed payload")
}

const signerLen = 32
rawSigner, raw := raw[:signerLen], raw[signerLen:]
signer, err := Encode(VersionByteAccountID, rawSigner)
if err != nil {
return nil, errors.Wrap(err, "invalid signed payload signer")
}

payload := []byte{}
reader := bytes.NewBuffer(raw)
readBytes, err := xdr.Unmarshal(reader, &payload)
if err != nil {
return nil, errors.Wrap(err, "invalid signed payload")
}

if len(raw) != readBytes || reader.Len() > 0 {
return nil, errors.New("invalid signed payload padding")
}

return NewSignedPayload(signer, payload)
}
Loading