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: Implement CAP 27 and SEP 23 #2491

Merged
merged 1 commit into from
Apr 17, 2020
Merged
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
4 changes: 2 additions & 2 deletions build/account_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type AccountMergeMutator interface {
// Deprecated use txnbuild.AccountMerge instead
type AccountMergeBuilder struct {
O xdr.Operation
Destination xdr.AccountId
Destination xdr.MuxedAccount
Err error
}

Expand All @@ -49,5 +49,5 @@ func (b *AccountMergeBuilder) Mutate(muts ...interface{}) {

// MutateAccountMerge for Destination sets the AccountMergeBuilder's Destination field
func (m Destination) MutateAccountMerge(o *AccountMergeBuilder) error {
return setAccountId(m.AddressOrSeed, &o.Destination)
return setMuxedAccount(m.AddressOrSeed, &o.Destination)
}
14 changes: 7 additions & 7 deletions build/account_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ var _ = Describe("AccountMergeBuilder Mutators", func() {
Expect(subject.Err).NotTo(HaveOccurred())
})

It("sets the destination to the correct xdr.AccountId", func() {
var aid xdr.AccountId
aid.SetAddress(address)
Expect(subject.Destination.Equals(aid)).To(BeTrue())
It("sets the destination to the correct xdr.MuxedAccount", func() {
var muxed xdr.MuxedAccount
muxed.SetAddress(address)
Expect(subject.Destination.Equals(muxed)).To(BeTrue())
})
})

Expand All @@ -51,9 +51,9 @@ var _ = Describe("AccountMergeBuilder Mutators", func() {
})

It("sets the destination to the correct xdr.AccountId", func() {
var aid xdr.AccountId
aid.SetAddress(address)
Expect(subject.O.SourceAccount.Equals(aid)).To(BeTrue())
var muxed xdr.MuxedAccount
muxed.SetAddress(address)
Expect(subject.O.SourceAccount.Equals(muxed)).To(BeTrue())
})
})

Expand Down
2 changes: 1 addition & 1 deletion build/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestAsset_ToXDR(t *testing.T) {
{
Name: "bad issuer",
Asset: CreditAsset("USD", "FUNK"),
ExpectedErr: "base32 decode failed: illegal base32 data at input byte 0",
ExpectedErr: "strkey is 4 bytes long; minimum valid length is 5",
},
{
Name: "bad code",
Expand Down
4 changes: 2 additions & 2 deletions build/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ type OperationMutator interface {
// MutateOperation for SourceAccount sets the operation's SourceAccount
// to the pubilic key for the address provided
func (m SourceAccount) MutateOperation(o *xdr.Operation) error {
o.SourceAccount = &xdr.AccountId{}
return setAccountId(m.AddressOrSeed, o.SourceAccount)
o.SourceAccount = &xdr.MuxedAccount{}
return setMuxedAccount(m.AddressOrSeed, o.SourceAccount)
}
4 changes: 2 additions & 2 deletions build/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ func (m Destination) MutatePayment(o interface{}) error {
default:
return errors.New("Unexpected operation type")
case *xdr.PaymentOp:
return setAccountId(m.AddressOrSeed, &o.Destination)
return setMuxedAccount(m.AddressOrSeed, &o.Destination)
case *xdr.PathPaymentStrictReceiveOp:
return setAccountId(m.AddressOrSeed, &o.Destination)
return setMuxedAccount(m.AddressOrSeed, &o.Destination)
}
}

Expand Down
2 changes: 1 addition & 1 deletion build/set_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestSetOptions_Signer(t *testing.T) {
Name: "Bad",
Address: "foo",
Weight: 1,
Error: "base32 decode failed",
Error: "minimum valid length is 5",
},
}

Expand Down
4 changes: 2 additions & 2 deletions build/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (m AllowTrustBuilder) MutateTransaction(o *TransactionBuilder) error {
func (m AutoSequence) MutateTransaction(o *TransactionBuilder) error {
source := o.TX.SourceAccount

if source == (xdr.AccountId{}) {
if source == (xdr.MuxedAccount{}) {
return errors.New("auto sequence used prior to setting source account")
}

Expand Down Expand Up @@ -334,7 +334,7 @@ func (m Sequence) MutateTransaction(o *TransactionBuilder) error {
// MutateTransaction for SourceAccount sets the transaction's SourceAccount
// to the pubilic key for the address provided
func (m SourceAccount) MutateTransaction(o *TransactionBuilder) error {
return setAccountId(m.AddressOrSeed, &o.TX.SourceAccount)
return setMuxedAccount(m.AddressOrSeed, &o.TX.SourceAccount)
}

// MutateTransaction for BaseFee sets the base fee
Expand Down
13 changes: 13 additions & 0 deletions build/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ func setAccountId(addressOrSeed string, aid *xdr.AccountId) error {
return aid.SetAddress(kp.Address())
}

func setMuxedAccount(addressOrSeed string, m *xdr.MuxedAccount) error {
kp, err := keypair.Parse(addressOrSeed)
if err != nil {
return err
}

if m == nil {
return errors.New("m is nil in setMuxedAccount")
}

return m.SetAddress(kp.Address())
}

func createAlphaNumAsset(code, issuerAccountId string) (xdr.Asset, error) {
var issuer xdr.AccountId
err := setAccountId(issuerAccountId, &issuer)
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func ExampleLowLevelTransaction() {
panic(err)
}

var destination xdr.AccountId
var destination xdr.MuxedAccount
err = destination.SetAddress(dkp.Address())
if err != nil {
panic(err)
Expand All @@ -92,7 +92,7 @@ func ExampleLowLevelTransaction() {

memo, err := xdr.NewMemo(xdr.MemoTypeMemoNone, nil)

var source xdr.AccountId
var source xdr.MuxedAccount
err = source.SetAddress(skp.Address())
if err != nil {
panic(err)
Expand Down
19 changes: 10 additions & 9 deletions network/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ func HashFeeBumpTransaction(tx *xdr.FeeBumpTransaction, passphrase string) ([32]
// resulting hash is the value that can be signed by stellar secret key to
// authorize the transaction identified by the hash to stellar validators.
func HashTransactionV0(tx *xdr.TransactionV0, passphrase string) ([32]byte, error) {
sa, err := xdr.NewMuxedAccount(xdr.CryptoKeyTypeKeyTypeEd25519, tx.SourceAccountEd25519)
if err != nil {
return [32]byte{}, err
}
v1Tx := &xdr.Transaction{
SourceAccount: xdr.AccountId{
Type: xdr.PublicKeyTypePublicKeyTypeEd25519,
Ed25519: &tx.SourceAccountEd25519,
},
Fee: tx.Fee,
Memo: tx.Memo,
Operations: tx.Operations,
SeqNum: tx.SeqNum,
TimeBounds: tx.TimeBounds,
SourceAccount: sa,
Fee: tx.Fee,
Memo: tx.Memo,
Operations: tx.Operations,
SeqNum: tx.SeqNum,
TimeBounds: tx.TimeBounds,
}
return HashTransaction(v1Tx, passphrase)
}
Expand Down
74 changes: 72 additions & 2 deletions services/horizon/internal/actions_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"

"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/expingest"
Expand Down Expand Up @@ -240,7 +242,37 @@ func TestTransactionActions_Post(t *testing.T) {
ht := StartHTTPTest(t, "base")
defer ht.Finish()

form := url.Values{"tx": []string{"AAAAAGL8HQvQkbK2HA3WVjRrKmjX00fG8sLI7m0ERwJW/AX3AAAAZAAAAAAAAAABAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAArqN6LeOagjxMaUP96Bzfs9e0corNZXzBWJkFoK7kvkwAAAAAO5rKAAAAAAAAAAABVvwF9wAAAECDzqvkQBQoNAJifPRXDoLhvtycT3lFPCQ51gkdsFHaBNWw05S/VhW0Xgkr0CBPE4NaFV2Kmcs3ZwLmib4TRrML"}}
tx := xdr.TransactionEnvelope{
Type: xdr.EnvelopeTypeEnvelopeTypeTxV0,
V0: &xdr.TransactionV0Envelope{
Tx: xdr.TransactionV0{
SourceAccountEd25519: *xdr.MustAddress("GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H").Ed25519,
Fee: 100,
SeqNum: 1,
Operations: []xdr.Operation{
{
Body: xdr.OperationBody{
Type: xdr.OperationTypeCreateAccount,
CreateAccountOp: &xdr.CreateAccountOp{
Destination: xdr.MustAddress("GCXKG6RN4ONIEPCMNFB732A436Z5PNDSRLGWK7GBLCMQLIFO4S7EYWVU"),
StartingBalance: 1000000000,
},
},
},
},
},
Signatures: []xdr.DecoratedSignature{
{
Hint: xdr.SignatureHint{86, 252, 5, 247},
Signature: xdr.Signature{131, 206, 171, 228, 64, 20, 40, 52, 2, 98, 124, 244, 87, 14, 130, 225, 190, 220, 156, 79, 121, 69, 60, 36, 57, 214, 9, 29, 176, 81, 218, 4, 213, 176, 211, 148, 191, 86, 21, 180, 94, 9, 43, 208, 32, 79, 19, 131, 90, 21, 93, 138, 153, 203, 55, 103, 2, 230, 137, 190, 19, 70, 179, 11},
},
},
},
}

txStr, err := xdr.MarshalBase64(tx)
assert.NoError(t, err)
form := url.Values{"tx": []string{txStr}}

// existing transaction
w := ht.Post("/transactions", form)
Expand All @@ -260,8 +292,46 @@ func TestTransactionActions_PostSuccessful(t *testing.T) {
ht := StartHTTPTest(t, "failed_transactions")
defer ht.Finish()

tx2 := xdr.TransactionEnvelope{
Type: xdr.EnvelopeTypeEnvelopeTypeTxV0,
V0: &xdr.TransactionV0Envelope{
Tx: xdr.TransactionV0{
SourceAccountEd25519: *xdr.MustAddress("GCXKG6RN4ONIEPCMNFB732A436Z5PNDSRLGWK7GBLCMQLIFO4S7EYWVU").Ed25519,
Fee: 100,
SeqNum: 8589934593,
Operations: []xdr.Operation{
{
Body: xdr.OperationBody{
Type: xdr.OperationTypePayment,
PaymentOp: &xdr.PaymentOp{
Destination: xdr.MustMuxedAccountAddress("GBXGQJWVLWOYHFLVTKWV5FGHA3LNYY2JQKM7OAJAUEQFU6LPCSEFVXON"),
Asset: xdr.Asset{
Type: xdr.AssetTypeAssetTypeCreditAlphanum4,
AlphaNum4: &xdr.AssetAlphaNum4{
AssetCode: xdr.AssetCode4{85, 83, 68, 0},
Issuer: xdr.MustAddress("GCXKG6RN4ONIEPCMNFB732A436Z5PNDSRLGWK7GBLCMQLIFO4S7EYWVU"),
},
},
Amount: 1000000000,
},
},
},
},
},
Signatures: []xdr.DecoratedSignature{
{
Hint: xdr.SignatureHint{174, 228, 190, 76},
Signature: xdr.Signature{73, 202, 13, 176, 216, 188, 169, 9, 141, 130, 180, 106, 187, 225, 22, 89, 254, 24, 173, 62, 236, 12, 186, 131, 70, 190, 214, 24, 209, 69, 233, 68, 1, 238, 48, 154, 55, 170, 53, 196, 96, 218, 110, 2, 159, 187, 120, 2, 50, 115, 2, 192, 208, 35, 72, 151, 106, 17, 155, 160, 147, 200, 52, 12},
},
},
},
}

txStr, err := xdr.MarshalBase64(tx2)
assert.NoError(t, err)

// 56e3216045d579bea40f2d35a09406de3a894ecb5be70dbda5ec9c0427a0d5a1
form := url.Values{"tx": []string{"AAAAAK6jei3jmoI8TGlD/egc37PXtHKKzWV8wViZBaCu5L5MAAAAZAAAAAIAAAABAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAAbmgm1V2dg5V1mq1elMcG1txjSYKZ9wEgoSBaeW8UiFoAAAABVVNEAAAAAACuo3ot45qCPExpQ/3oHN+z17Ryis1lfMFYmQWgruS+TAAAAAA7msoAAAAAAAAAAAGu5L5MAAAAQEnKDbDYvKkJjYK0arvhFln+GK0+7Ay6g0a+1hjRRelEAe4wmjeqNcRg2m4Cn7t4AjJzAsDQI0iXahGboJPINAw="}}
form := url.Values{"tx": []string{txStr}}

w := ht.Post("/transactions", form)
ht.Assert.Equal(200, w.Code)
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (tx *Transaction) Sequence() int64 {
// SourceAddress returns the strkey-encoded account id that paid the fee for
// `tx`.
func (tx *Transaction) SourceAddress() string {
sa := tx.Envelope.SourceAccount()
sa := tx.Envelope.SourceAccount().ToAccountId()
return sa.Address()
}

Expand Down
30 changes: 30 additions & 0 deletions services/horizon/internal/db2/core/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package core
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/stellar/go/services/horizon/internal/test"
"github.com/stellar/go/xdr"
)
Expand Down Expand Up @@ -61,3 +63,31 @@ func TestSignatures(t *testing.T) {
tt.Assert.Equal("8qkkeKaKfsbgInyIkzXJhqJE5/Ufxri2LdxmyKkgkT6I3sPmvrs5cPWQSzEQyhV750IW2ds97xTHqTpOfuZCAg==", signatures[0])
tt.Assert.Equal("", signatures[1])
}

func TestTransaction_SourceAddress_MuxedAccount(t *testing.T) {
muxed := xdr.MustMuxedAccountAddress("MCAAAAAAAAAAAAB7BQ2L7E5NBWMXDUCMZSIPOBKRDSBYVLMXGSSKF6YNPIB7Y77ITKNOG")
var tx Transaction
tx.Envelope = xdr.TransactionEnvelope{
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
SourceAccount: muxed,
Operations: []xdr.Operation{
{
SourceAccount: &muxed,
Body: xdr.OperationBody{
Type: xdr.OperationTypePayment,
PaymentOp: &xdr.PaymentOp{
Destination: muxed,
Asset: xdr.Asset{Type: xdr.AssetTypeAssetTypeNative},
Amount: 100,
},
},
},
},
},
},
}

assert.Equal(t, "GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGZ", tx.SourceAddress())
}
12 changes: 6 additions & 6 deletions services/horizon/internal/db2/history/fee_bump_scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func FeeBumpScenario(tt *test.T, q *Q, successful bool) FeeBumpFixture {
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
SourceAccount: xdr.AccountId{
Type: xdr.PublicKeyTypePublicKeyTypeEd25519,
SourceAccount: xdr.MuxedAccount{
Type: xdr.CryptoKeyTypeKeyTypeEd25519,
Ed25519: &xdr.Uint256{
3, 3, 3,
},
Expand All @@ -144,7 +144,7 @@ func FeeBumpScenario(tt *test.T, q *Q, successful bool) FeeBumpFixture {
MaxTime: 4,
},
Operations: []xdr.Operation{
xdr.Operation{
{
Body: xdr.OperationBody{
Type: xdr.OperationTypeBumpSequence,
BumpSequenceOp: &xdr.BumpSequenceOp{
Expand All @@ -155,7 +155,7 @@ func FeeBumpScenario(tt *test.T, q *Q, successful bool) FeeBumpFixture {
},
},
Signatures: []xdr.DecoratedSignature{
xdr.DecoratedSignature{
{
Hint: xdr.SignatureHint{2, 2, 2, 2},
Signature: xdr.Signature{20, 20, 20},
},
Expand All @@ -164,7 +164,7 @@ func FeeBumpScenario(tt *test.T, q *Q, successful bool) FeeBumpFixture {
},
},
Signatures: []xdr.DecoratedSignature{
xdr.DecoratedSignature{
{
Hint: xdr.SignatureHint{3, 3, 3, 3},
Signature: xdr.Signature{30, 30, 30},
},
Expand Down Expand Up @@ -202,7 +202,7 @@ func FeeBumpScenario(tt *test.T, q *Q, successful bool) FeeBumpFixture {
Result: xdr.InnerTransactionResultResult{
Code: xdr.TransactionResultCodeTxSuccess,
Results: &[]xdr.OperationResult{
xdr.OperationResult{
{
Tr: &xdr.OperationResultTr{
Type: xdr.OperationTypeBumpSequence,
BumpSeqResult: &xdr.BumpSequenceResult{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func transactionToMap(transaction io.LedgerTransaction, sequence uint32) (map[st
return nil, err
}

sourceAccount := transaction.Envelope.SourceAccount()
sourceAccount := transaction.Envelope.SourceAccount().ToAccountId()
m := map[string]interface{}{
"id": toid.New(int32(sequence), int32(transaction.Index), 0).ToInt64(),
"transaction_hash": hex.EncodeToString(transaction.Result.TransactionHash[:]),
Expand Down
Loading