diff --git a/src/github.com/stellar/gateway/bridge/handlers/request_handler_authorize_test.go b/src/github.com/stellar/gateway/bridge/handlers/request_handler_authorize_test.go index 521fbd4..cc4f4da 100644 --- a/src/github.com/stellar/gateway/bridge/handlers/request_handler_authorize_test.go +++ b/src/github.com/stellar/gateway/bridge/handlers/request_handler_authorize_test.go @@ -54,7 +54,7 @@ func TestRequestHandlerAuthorize(t *testing.T) { "name": "account_id" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) }) @@ -73,7 +73,7 @@ func TestRequestHandlerAuthorize(t *testing.T) { "name": "asset_code" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) }) diff --git a/src/github.com/stellar/gateway/bridge/handlers/request_handler_builder.go b/src/github.com/stellar/gateway/bridge/handlers/request_handler_builder.go index 3fe0836..146334e 100644 --- a/src/github.com/stellar/gateway/bridge/handlers/request_handler_builder.go +++ b/src/github.com/stellar/gateway/bridge/handlers/request_handler_builder.go @@ -42,7 +42,7 @@ func (rh *RequestHandler) Builder(w http.ResponseWriter, r *http.Request) { sequenceNumber, err := strconv.ParseUint(request.SequenceNumber, 10, 64) if err != nil { - errorResponse := protocols.NewInvalidParameterError("sequence_number", request.SequenceNumber) + errorResponse := protocols.NewInvalidParameterError("sequence_number", request.SequenceNumber, "Sequence number must be a number") log.WithFields(errorResponse.LogData).Error(errorResponse.Error()) server.Write(w, errorResponse) return diff --git a/src/github.com/stellar/gateway/bridge/handlers/request_handler_payment.go b/src/github.com/stellar/gateway/bridge/handlers/request_handler_payment.go index 62cde96..447962f 100644 --- a/src/github.com/stellar/gateway/bridge/handlers/request_handler_payment.go +++ b/src/github.com/stellar/gateway/bridge/handlers/request_handler_payment.go @@ -134,10 +134,9 @@ func (rh *RequestHandler) Payment(w http.ResponseWriter, r *http.Request) { } } - _, err = keypair.Parse(destinationObject.AccountID) - if err != nil { + if !protocols.IsValidAccountID(destinationObject.AccountID) { log.WithFields(log.Fields{"AccountId": destinationObject.AccountID}).Print("Invalid AccountId in destination") - server.Write(w, protocols.NewInvalidParameterError("destination", request.Destination)) + server.Write(w, protocols.NewInvalidParameterError("destination", request.Destination, "Destination public key must start with `G`.")) return } @@ -231,7 +230,7 @@ func (rh *RequestHandler) Payment(w http.ResponseWriter, r *http.Request) { id, err := strconv.ParseUint(memo, 10, 64) if err != nil { log.WithFields(log.Fields{"memo": memo}).Print("Cannot convert memo_id value to uint64") - server.Write(w, protocols.NewInvalidParameterError("memo", request.Memo)) + server.Write(w, protocols.NewInvalidParameterError("memo", request.Memo, "Memo.id must be a number")) return } memoMutator = b.MemoID{id} @@ -241,7 +240,7 @@ func (rh *RequestHandler) Payment(w http.ResponseWriter, r *http.Request) { memoBytes, err := hex.DecodeString(memo) if err != nil || len(memoBytes) != 32 { log.WithFields(log.Fields{"memo": memo}).Print("Cannot decode hash memo value") - server.Write(w, protocols.NewInvalidParameterError("memo", request.Memo)) + server.Write(w, protocols.NewInvalidParameterError("memo", request.Memo, "Memo.hash must be 32 bytes and hex encoded.")) return } var b32 [32]byte @@ -250,7 +249,7 @@ func (rh *RequestHandler) Payment(w http.ResponseWriter, r *http.Request) { memoMutator = &b.MemoHash{hash} default: log.Print("Not supported memo type: ", memoType) - server.Write(w, protocols.NewInvalidParameterError("memo", request.Memo)) + server.Write(w, protocols.NewInvalidParameterError("memo", request.Memo, "Memo type not supported")) return } @@ -289,12 +288,12 @@ func (rh *RequestHandler) Payment(w http.ResponseWriter, r *http.Request) { case tx.Err.Error() == "Asset code length is invalid": server.Write( w, - protocols.NewInvalidParameterError("asset_code", request.AssetCode), + protocols.NewInvalidParameterError("asset_code", request.AssetCode, "Asset code length is invalid"), ) case strings.Contains(tx.Err.Error(), "cannot parse amount"): server.Write( w, - protocols.NewInvalidParameterError("amount", request.Amount), + protocols.NewInvalidParameterError("amount", request.Amount, "Cannot parse amount"), ) default: log.WithFields(log.Fields{"err": tx.Err}).Print("Transaction builder error") diff --git a/src/github.com/stellar/gateway/bridge/handlers/request_handler_payment_test.go b/src/github.com/stellar/gateway/bridge/handlers/request_handler_payment_test.go index f04453a..e5815b8 100644 --- a/src/github.com/stellar/gateway/bridge/handlers/request_handler_payment_test.go +++ b/src/github.com/stellar/gateway/bridge/handlers/request_handler_payment_test.go @@ -81,7 +81,7 @@ func TestRequestHandlerPayment(t *testing.T) { "name": "source" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) }) @@ -111,7 +111,7 @@ func TestRequestHandlerPayment(t *testing.T) { "name": "destination" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) }) @@ -345,7 +345,7 @@ func TestRequestHandlerPayment(t *testing.T) { "name": "asset_issuer" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) }) @@ -395,7 +395,7 @@ func TestRequestHandlerPayment(t *testing.T) { "name": "asset_code" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) }) @@ -445,7 +445,7 @@ func TestRequestHandlerPayment(t *testing.T) { "name": "amount" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) }) @@ -565,7 +565,7 @@ func TestRequestHandlerPayment(t *testing.T) { "name": "memo" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) Convey("unsupported memo_type", func() { @@ -581,7 +581,7 @@ func TestRequestHandlerPayment(t *testing.T) { "name": "memo" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) Convey("memo is attached to the transaction", func() { diff --git a/src/github.com/stellar/gateway/compliance/handlers/request_handler_auth.go b/src/github.com/stellar/gateway/compliance/handlers/request_handler_auth.go index 9777434..1d24fa5 100644 --- a/src/github.com/stellar/gateway/compliance/handlers/request_handler_auth.go +++ b/src/github.com/stellar/gateway/compliance/handlers/request_handler_auth.go @@ -55,9 +55,9 @@ func (rh *RequestHandler) HandlerAuth(c web.C, w http.ResponseWriter, r *http.Re return } - if senderStellarToml.SigningKey == "" { - errorResponse := protocols.NewInvalidParameterError("data.sender", authData.Sender) - log.WithFields(errorResponse.LogData).Warn("No SIGNING_KEY in stellar.toml of sender") + if !protocols.IsValidAccountID(senderStellarToml.SigningKey) { + errorResponse := protocols.NewInvalidParameterError("data.sender", authData.Sender, "SIGNING_KEY in stellar.toml of sender is invalid") + log.WithFields(errorResponse.LogData).Warn("SIGNING_KEY in stellar.toml of sender is invalid") server.Write(w, errorResponse) return } @@ -65,7 +65,7 @@ func (rh *RequestHandler) HandlerAuth(c web.C, w http.ResponseWriter, r *http.Re // Verify signature signatureBytes, err := base64.StdEncoding.DecodeString(authreq.Signature) if err != nil { - errorResponse := protocols.NewInvalidParameterError("sig", authreq.Signature) + errorResponse := protocols.NewInvalidParameterError("sig", authreq.Signature, "Invalid base64 string.") log.WithFields(errorResponse.LogData).Warn("Error decoding signature") server.Write(w, errorResponse) return @@ -77,7 +77,7 @@ func (rh *RequestHandler) HandlerAuth(c web.C, w http.ResponseWriter, r *http.Re "data": authreq.Data, "sig": authreq.Signature, }).Warn("Invalid signature") - errorResponse := protocols.NewInvalidParameterError("sig", authreq.Signature) + errorResponse := protocols.NewInvalidParameterError("sig", authreq.Signature, "Invalid signature.") server.Write(w, errorResponse) return } @@ -86,7 +86,7 @@ func (rh *RequestHandler) HandlerAuth(c web.C, w http.ResponseWriter, r *http.Re var tx xdr.Transaction _, err = xdr.Unmarshal(b64r, &tx) if err != nil { - errorResponse := protocols.NewInvalidParameterError("data.tx", authData.Tx) + errorResponse := protocols.NewInvalidParameterError("data.tx", authData.Tx, "Error decoding Transaction XDR") log.WithFields(log.Fields{ "err": err, "tx": authData.Tx, @@ -96,7 +96,7 @@ func (rh *RequestHandler) HandlerAuth(c web.C, w http.ResponseWriter, r *http.Re } if tx.Memo.Hash == nil { - errorResponse := protocols.NewInvalidParameterError("data.tx", authData.Tx) + errorResponse := protocols.NewInvalidParameterError("data.tx", authData.Tx, "Transaction does not contain Memo.Hash") log.WithFields(log.Fields{"tx": authData.Tx}).Warn("Transaction does not contain Memo.Hash") server.Write(w, errorResponse) return @@ -107,7 +107,7 @@ func (rh *RequestHandler) HandlerAuth(c web.C, w http.ResponseWriter, r *http.Re memoBytes := [32]byte(*tx.Memo.Hash) if memoPreimageHashBytes != memoBytes { - errorResponse := protocols.NewInvalidParameterError("data.tx", authData.Tx) + errorResponse := protocols.NewInvalidParameterError("data.tx", authData.Tx, "Memo preimage hash does not equal tx Memo.Hash") h := xdr.Hash(memoPreimageHashBytes) tx.Memo.Hash = &h diff --git a/src/github.com/stellar/gateway/compliance/handlers/request_handler_auth_test.go b/src/github.com/stellar/gateway/compliance/handlers/request_handler_auth_test.go index 15e4e60..4eb2a91 100644 --- a/src/github.com/stellar/gateway/compliance/handlers/request_handler_auth_test.go +++ b/src/github.com/stellar/gateway/compliance/handlers/request_handler_auth_test.go @@ -84,7 +84,7 @@ func TestRequestHandlerAuth(t *testing.T) { "code": "invalid_parameter", "message": "Invalid parameter." }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) Convey("When data is invalid", func() { @@ -100,7 +100,7 @@ func TestRequestHandlerAuth(t *testing.T) { "code": "invalid_parameter", "message": "Invalid parameter." }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) Convey("When sender's stellar.toml does not contain signing key", func() { @@ -150,7 +150,7 @@ func TestRequestHandlerAuth(t *testing.T) { "name": "data.sender" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) Convey("When signature is invalid", func() { @@ -213,7 +213,7 @@ func TestRequestHandlerAuth(t *testing.T) { "name": "sig" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) Convey("When all params are valid", func() { diff --git a/src/github.com/stellar/gateway/compliance/handlers/request_handler_send_test.go b/src/github.com/stellar/gateway/compliance/handlers/request_handler_send_test.go index d4ff196..a82d3cc 100644 --- a/src/github.com/stellar/gateway/compliance/handlers/request_handler_send_test.go +++ b/src/github.com/stellar/gateway/compliance/handlers/request_handler_send_test.go @@ -108,7 +108,7 @@ func TestRequestHandlerSend(t *testing.T) { "name": "source" } }`) - assert.Equal(t, expected, test.StringToJSONMap(responseString)) + assert.Equal(t, expected, test.StringToJSONMap(responseString, "more_info")) }) Convey("When params are valid", func() { diff --git a/src/github.com/stellar/gateway/protocols/bridge/authorize.go b/src/github.com/stellar/gateway/protocols/bridge/authorize.go index cdc7783..403fc92 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/authorize.go +++ b/src/github.com/stellar/gateway/protocols/bridge/authorize.go @@ -6,7 +6,6 @@ import ( "github.com/stellar/gateway/bridge/config" "github.com/stellar/gateway/protocols" - "github.com/stellar/go/keypair" ) var ( @@ -45,9 +44,8 @@ func (request *AuthorizeRequest) Validate(allowedAssets []config.Asset, issuingA return err } - _, err = keypair.Parse(request.AccountID) - if err != nil { - return protocols.NewInvalidParameterError("account_id", request.AccountID) + if !protocols.IsValidAccountID(request.AccountID) { + return protocols.NewInvalidParameterError("account_id", request.AccountID, "Account ID must start with `G`.") } // Is asset allowed? @@ -60,7 +58,7 @@ func (request *AuthorizeRequest) Validate(allowedAssets []config.Asset, issuingA } if !allowed { - return protocols.NewInvalidParameterError("asset_code", request.AssetCode) + return protocols.NewInvalidParameterError("asset_code", request.AssetCode, "Asset code not allowed.") } return nil diff --git a/src/github.com/stellar/gateway/protocols/bridge/builder.go b/src/github.com/stellar/gateway/protocols/bridge/builder.go index ab076ad..7e8c805 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/builder.go +++ b/src/github.com/stellar/gateway/protocols/bridge/builder.go @@ -97,11 +97,11 @@ func (r BuilderRequest) Process() error { err = json.Unmarshal(operation.RawBody, &manageData) operationBody = manageData default: - return protocols.NewInvalidParameterError("operations["+strconv.Itoa(i)+"][type]", string(operation.Type)) + return protocols.NewInvalidParameterError("operations["+strconv.Itoa(i)+"][type]", string(operation.Type), "Invalid operation type.") } if err != nil { - return protocols.NewInvalidParameterError("operations["+strconv.Itoa(i)+"][body]", "", map[string]interface{}{"err": err}) + return protocols.NewInvalidParameterError("operations["+strconv.Itoa(i)+"][body]", "", "Operation is invalid.", map[string]interface{}{"err": err}) } r.Operations[i].Body = operationBody @@ -113,12 +113,12 @@ func (r BuilderRequest) Process() error { // Validate validates if the request is correct. func (r BuilderRequest) Validate() error { if !protocols.IsValidAccountID(r.Source) { - return protocols.NewInvalidParameterError("source", r.Source) + return protocols.NewInvalidParameterError("source", r.Source, "Source parameter must start with `G`.") } for i, signer := range r.Signers { - if !protocols.IsValidAccountID(signer) { - return protocols.NewInvalidParameterError("signers["+strconv.Itoa(i)+"]", signer) + if !protocols.IsValidSecret(signer) { + return protocols.NewInvalidParameterError("signers["+strconv.Itoa(i)+"]", signer, "Signer must start with `S`.") } } diff --git a/src/github.com/stellar/gateway/protocols/bridge/builder_account_merge.go b/src/github.com/stellar/gateway/protocols/bridge/builder_account_merge.go index 5132c11..1339107 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/builder_account_merge.go +++ b/src/github.com/stellar/gateway/protocols/bridge/builder_account_merge.go @@ -25,11 +25,11 @@ func (op AccountMergeOperationBody) ToTransactionMutator() b.TransactionMutator // Validate validates if operation body is valid. func (op AccountMergeOperationBody) Validate() error { if !protocols.IsValidAccountID(op.Destination) { - return protocols.NewInvalidParameterError("destination", op.Destination) + return protocols.NewInvalidParameterError("destination", op.Destination, "Destination must start with `G`.") } if op.Source != nil && !protocols.IsValidAccountID(*op.Source) { - return protocols.NewInvalidParameterError("source", *op.Source) + return protocols.NewInvalidParameterError("source", *op.Source, "Source must start with `G`.") } return nil diff --git a/src/github.com/stellar/gateway/protocols/bridge/builder_allow_trust.go b/src/github.com/stellar/gateway/protocols/bridge/builder_allow_trust.go index e9f8511..16e291a 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/builder_allow_trust.go +++ b/src/github.com/stellar/gateway/protocols/bridge/builder_allow_trust.go @@ -31,15 +31,15 @@ func (op AllowTrustOperationBody) ToTransactionMutator() b.TransactionMutator { // Validate validates if operation body is valid. func (op AllowTrustOperationBody) Validate() error { if !protocols.IsValidAssetCode(op.AssetCode) { - return protocols.NewInvalidParameterError("asset_code", op.AssetCode) + return protocols.NewInvalidParameterError("asset_code", op.AssetCode, "Asset code is invalid") } if !protocols.IsValidAccountID(op.Trustor) { - return protocols.NewInvalidParameterError("trustor", op.Trustor) + return protocols.NewInvalidParameterError("trustor", op.Trustor, "Trustor must be a public key (starting with `G`).") } if op.Source != nil && !protocols.IsValidAccountID(*op.Source) { - return protocols.NewInvalidParameterError("source", *op.Source) + return protocols.NewInvalidParameterError("source", *op.Source, "Source must be a public key (starting with `G`).") } return nil diff --git a/src/github.com/stellar/gateway/protocols/bridge/builder_change_trust.go b/src/github.com/stellar/gateway/protocols/bridge/builder_change_trust.go index 7f88b2b..f23a64a 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/builder_change_trust.go +++ b/src/github.com/stellar/gateway/protocols/bridge/builder_change_trust.go @@ -36,17 +36,17 @@ func (op ChangeTrustOperationBody) ToTransactionMutator() b.TransactionMutator { // Validate validates if operation body is valid. func (op ChangeTrustOperationBody) Validate() error { if !op.Asset.Validate() { - return protocols.NewInvalidParameterError("asset", op.Asset.String()) + return protocols.NewInvalidParameterError("asset", op.Asset.String(), "Asset is invalid.") } if op.Limit != nil { if !protocols.IsValidAmount(*op.Limit) { - return protocols.NewInvalidParameterError("limit", *op.Limit) + return protocols.NewInvalidParameterError("limit", *op.Limit, "Limit is not a valid amount.") } } if op.Source != nil && !protocols.IsValidAccountID(*op.Source) { - return protocols.NewInvalidParameterError("source", *op.Source) + return protocols.NewInvalidParameterError("source", *op.Source, "Source must be a public key (starting with `G`).") } return nil diff --git a/src/github.com/stellar/gateway/protocols/bridge/builder_create_account.go b/src/github.com/stellar/gateway/protocols/bridge/builder_create_account.go index 49c71ef..99b0fd1 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/builder_create_account.go +++ b/src/github.com/stellar/gateway/protocols/bridge/builder_create_account.go @@ -29,15 +29,15 @@ func (op CreateAccountOperationBody) ToTransactionMutator() b.TransactionMutator // Validate validates if operation body is valid. func (op CreateAccountOperationBody) Validate() error { if !protocols.IsValidAccountID(op.Destination) { - return protocols.NewInvalidParameterError("destination", op.Destination) + return protocols.NewInvalidParameterError("destination", op.Destination, "Destination must be a public key (starting with `G`)") } if !protocols.IsValidAmount(op.StartingBalance) { - return protocols.NewInvalidParameterError("starting_balance", op.StartingBalance) + return protocols.NewInvalidParameterError("starting_balance", op.StartingBalance, "Not a valid amount.") } if op.Source != nil && !protocols.IsValidAccountID(*op.Source) { - return protocols.NewInvalidParameterError("source", *op.Source) + return protocols.NewInvalidParameterError("source", *op.Source, "Source must be a public key (starting with `G`)") } return nil diff --git a/src/github.com/stellar/gateway/protocols/bridge/builder_inflation.go b/src/github.com/stellar/gateway/protocols/bridge/builder_inflation.go index 40f11d8..74c92de 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/builder_inflation.go +++ b/src/github.com/stellar/gateway/protocols/bridge/builder_inflation.go @@ -24,7 +24,7 @@ func (op InflationOperationBody) ToTransactionMutator() b.TransactionMutator { // Validate validates if operation body is valid. func (op InflationOperationBody) Validate() error { if op.Source != nil && !protocols.IsValidAccountID(*op.Source) { - return protocols.NewInvalidParameterError("source", *op.Source) + return protocols.NewInvalidParameterError("source", *op.Source, "Source must be a public key (starting with `G`).") } return nil diff --git a/src/github.com/stellar/gateway/protocols/bridge/builder_manage_data.go b/src/github.com/stellar/gateway/protocols/bridge/builder_manage_data.go index 3fcc8a0..e5bd9f7 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/builder_manage_data.go +++ b/src/github.com/stellar/gateway/protocols/bridge/builder_manage_data.go @@ -36,16 +36,16 @@ func (op ManageDataOperationBody) ToTransactionMutator() b.TransactionMutator { // Validate validates if operation body is valid. func (op ManageDataOperationBody) Validate() error { if len(op.Name) > 64 { - return protocols.NewInvalidParameterError("name", op.Name) + return protocols.NewInvalidParameterError("name", op.Name, "Name must be less than or equal 64 characters.") } data, err := base64.StdEncoding.DecodeString(op.Data) if err != nil || len(data) > 64 { - return protocols.NewInvalidParameterError("data", op.Data) + return protocols.NewInvalidParameterError("data", op.Data, "Not a valid base64 string.") } if op.Source != nil && !protocols.IsValidAccountID(*op.Source) { - return protocols.NewInvalidParameterError("source", *op.Source) + return protocols.NewInvalidParameterError("source", *op.Source, "Source must be a public key (starting with `G`).") } return nil diff --git a/src/github.com/stellar/gateway/protocols/bridge/builder_manage_offer.go b/src/github.com/stellar/gateway/protocols/bridge/builder_manage_offer.go index 95f97ee..ff2b75c 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/builder_manage_offer.go +++ b/src/github.com/stellar/gateway/protocols/bridge/builder_manage_offer.go @@ -49,12 +49,12 @@ func (op ManageOfferOperationBody) Validate() error { if op.OfferID != nil { _, err := strconv.ParseUint(*op.OfferID, 10, 64) if err != nil { - return protocols.NewInvalidParameterError("offer_id", *op.OfferID) + return protocols.NewInvalidParameterError("offer_id", *op.OfferID, "Not a number.") } } if op.Source != nil && !protocols.IsValidAccountID(*op.Source) { - return protocols.NewInvalidParameterError("source", *op.Source) + return protocols.NewInvalidParameterError("source", *op.Source, "Source must be a public key (starting with `G`).") } return nil diff --git a/src/github.com/stellar/gateway/protocols/bridge/builder_path_payment.go b/src/github.com/stellar/gateway/protocols/bridge/builder_path_payment.go index eddb622..2f35f8e 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/builder_path_payment.go +++ b/src/github.com/stellar/gateway/protocols/bridge/builder_path_payment.go @@ -63,32 +63,32 @@ func (op PathPaymentOperationBody) ToTransactionMutator() b.TransactionMutator { // Validate validates if operation body is valid. func (op PathPaymentOperationBody) Validate() error { if !protocols.IsValidAccountID(op.Destination) { - return protocols.NewInvalidParameterError("destination", op.Destination) + return protocols.NewInvalidParameterError("destination", op.Destination, "Destination must be a public key (starting with `G`).") } if !protocols.IsValidAmount(op.SendMax) { - return protocols.NewInvalidParameterError("send_max", op.SendMax) + return protocols.NewInvalidParameterError("send_max", op.SendMax, "Not a valid amount.") } if !protocols.IsValidAmount(op.DestinationAmount) { - return protocols.NewInvalidParameterError("destination_amount", op.DestinationAmount) + return protocols.NewInvalidParameterError("destination_amount", op.DestinationAmount, "Not a valid amount.") } if !op.SendAsset.Validate() { - return protocols.NewInvalidParameterError("send_asset", op.SendAsset.String()) + return protocols.NewInvalidParameterError("send_asset", op.SendAsset.String(), "Invalid asset.") } if !op.DestinationAsset.Validate() { - return protocols.NewInvalidParameterError("destination_asset", op.DestinationAsset.String()) + return protocols.NewInvalidParameterError("destination_asset", op.DestinationAsset.String(), "Invalid asset.") } if op.Source != nil && !protocols.IsValidAccountID(*op.Source) { - return protocols.NewInvalidParameterError("source", *op.Source) + return protocols.NewInvalidParameterError("source", *op.Source, "Source must be a public key (starting with `G`).") } for i, asset := range op.Path { if !asset.Validate() { - return protocols.NewInvalidParameterError("path["+strconv.Itoa(i)+"]", asset.String()) + return protocols.NewInvalidParameterError("path["+strconv.Itoa(i)+"]", asset.String(), "Invalid asset.") } } diff --git a/src/github.com/stellar/gateway/protocols/bridge/builder_payment.go b/src/github.com/stellar/gateway/protocols/bridge/builder_payment.go index 119b618..2cb2ad6 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/builder_payment.go +++ b/src/github.com/stellar/gateway/protocols/bridge/builder_payment.go @@ -41,19 +41,19 @@ func (op PaymentOperationBody) ToTransactionMutator() b.TransactionMutator { // Validate validates if operation body is valid. func (op PaymentOperationBody) Validate() error { if !protocols.IsValidAccountID(op.Destination) { - return protocols.NewInvalidParameterError("destination", op.Destination) + return protocols.NewInvalidParameterError("destination", op.Destination, "Destination must be a public key (starting with `G`).") } if !protocols.IsValidAmount(op.Amount) { - return protocols.NewInvalidParameterError("amount", op.Amount) + return protocols.NewInvalidParameterError("amount", op.Amount, "Invalid amount.") } if !op.Asset.Validate() { - return protocols.NewInvalidParameterError("asset", op.Asset.String()) + return protocols.NewInvalidParameterError("asset", op.Asset.String(), "Invalid asset.") } if op.Source != nil && !protocols.IsValidAccountID(*op.Source) { - return protocols.NewInvalidParameterError("source", *op.Source) + return protocols.NewInvalidParameterError("source", *op.Source, "Source must be a public key (starting with `G`).") } return nil diff --git a/src/github.com/stellar/gateway/protocols/bridge/builder_set_options.go b/src/github.com/stellar/gateway/protocols/bridge/builder_set_options.go index afdba6d..5527245 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/builder_set_options.go +++ b/src/github.com/stellar/gateway/protocols/bridge/builder_set_options.go @@ -78,17 +78,17 @@ func (op SetOptionsOperationBody) ToTransactionMutator() b.TransactionMutator { // Validate validates if operation body is valid. func (op SetOptionsOperationBody) Validate() error { if op.InflationDest != nil && !protocols.IsValidAccountID(*op.InflationDest) { - return protocols.NewInvalidParameterError("inflation_dest", *op.InflationDest) + return protocols.NewInvalidParameterError("inflation_dest", *op.InflationDest, "Inflation destination must be a public key (starting with `G`).") } if op.Signer != nil { if !protocols.IsValidAccountID(op.Signer.PublicKey) { - return protocols.NewInvalidParameterError("signer.public_key", op.Signer.PublicKey) + return protocols.NewInvalidParameterError("signer.public_key", op.Signer.PublicKey, "Public key must be starting with `G`.") } } if op.Source != nil && !protocols.IsValidAccountID(*op.Source) { - return protocols.NewInvalidParameterError("source", *op.Source) + return protocols.NewInvalidParameterError("source", *op.Source, "Source must be a public key (starting with `G`).") } return nil diff --git a/src/github.com/stellar/gateway/protocols/bridge/payment.go b/src/github.com/stellar/gateway/protocols/bridge/payment.go index a87df73..604d15c 100644 --- a/src/github.com/stellar/gateway/protocols/bridge/payment.go +++ b/src/github.com/stellar/gateway/protocols/bridge/payment.go @@ -130,7 +130,7 @@ func (request *PaymentRequest) Validate() error { if request.Source != "" { _, err = keypair.Parse(request.Source) if err != nil { - return protocols.NewInvalidParameterError("source", request.Source) + return protocols.NewInvalidParameterError("source", request.Source, "Source must be a public key (starting with `G`).") } } @@ -153,9 +153,8 @@ func (request *PaymentRequest) Validate() error { } if request.AssetIssuer != "" { - _, err := keypair.Parse(request.AssetIssuer) - if err != nil { - return protocols.NewInvalidParameterError("asset_issuer", request.AssetIssuer) + if !protocols.IsValidAccountID(request.AssetIssuer) { + return protocols.NewInvalidParameterError("asset_issuer", request.AssetIssuer, "Asset issuer must be a public key (starting with `G`).") } } @@ -169,9 +168,8 @@ func (request *PaymentRequest) Validate() error { } if request.SendAssetIssuer != "" { - _, err := keypair.Parse(request.SendAssetIssuer) - if err != nil { - return protocols.NewInvalidParameterError("send_asset_issuer", request.SendAssetIssuer) + if !protocols.IsValidAccountID(request.SendAssetIssuer) { + return protocols.NewInvalidParameterError("send_asset_issuer", request.SendAssetIssuer, "Send asset issuer must be a public key (starting with `G`).") } } diff --git a/src/github.com/stellar/gateway/protocols/common.go b/src/github.com/stellar/gateway/protocols/common.go index 83c2635..8e4af6c 100644 --- a/src/github.com/stellar/gateway/protocols/common.go +++ b/src/github.com/stellar/gateway/protocols/common.go @@ -28,7 +28,7 @@ func (a Asset) ToBaseAsset() build.Asset { // String returns string representation of this asset func (a Asset) String() string { - return fmt.Sprintf("%+v", a) + return fmt.Sprintf("Code: %s, Issuer: %s", a.Code, a.Issuer) } // Validate checks if asset params are correct. diff --git a/src/github.com/stellar/gateway/protocols/compliance/send.go b/src/github.com/stellar/gateway/protocols/compliance/send.go index 667df1b..5a98b70 100644 --- a/src/github.com/stellar/gateway/protocols/compliance/send.go +++ b/src/github.com/stellar/gateway/protocols/compliance/send.go @@ -56,22 +56,21 @@ func (request *SendRequest) Validate() error { return err } - _, err = keypair.Parse(request.Source) - if err != nil { - return protocols.NewInvalidParameterError("source", request.Source) + if !protocols.IsValidAccountID(request.Source) { + return protocols.NewInvalidParameterError("source", request.Source, "Source must be a public key (starting with `G`).") } if !validateStellarAddress(request.Sender) { - return protocols.NewInvalidParameterError("sender", request.Sender) + return protocols.NewInvalidParameterError("sender", request.Sender, "Not a valid stellar address.") } if !validateStellarAddress(request.Destination) { - return protocols.NewInvalidParameterError("destination", request.Destination) + return protocols.NewInvalidParameterError("destination", request.Destination, "Not a valid stellar address.") } _, err = keypair.Parse(request.AssetIssuer) - if err != nil { - return protocols.NewInvalidParameterError("asset_issuer", request.AssetIssuer) + if !protocols.IsValidAccountID(request.AssetIssuer) { + return protocols.NewInvalidParameterError("asset_issuer", request.AssetIssuer, "Asset issuer must be a public key (starting with `G`).") } return nil diff --git a/src/github.com/stellar/gateway/protocols/errors.go b/src/github.com/stellar/gateway/protocols/errors.go index 1854195..afba17c 100644 --- a/src/github.com/stellar/gateway/protocols/errors.go +++ b/src/github.com/stellar/gateway/protocols/errors.go @@ -26,7 +26,7 @@ func NewInternalServerError(logMessage string, logData map[string]interface{}) * } // NewInvalidParameterError creates and returns a new InvalidParameterError -func NewInvalidParameterError(name, value string, additionalLogData ...map[string]interface{}) *ErrorResponse { +func NewInvalidParameterError(name, value, moreInfo string, additionalLogData ...map[string]interface{}) *ErrorResponse { logData := map[string]interface{}{"name": name, "value": value} if len(additionalLogData) == 1 { for k, v := range additionalLogData[0] { @@ -35,11 +35,12 @@ func NewInvalidParameterError(name, value string, additionalLogData ...map[strin } return &ErrorResponse{ - Status: InvalidParameterError.Status, - Code: InvalidParameterError.Code, - Message: InvalidParameterError.Message, - Data: map[string]interface{}{"name": name}, - LogData: logData, + Status: InvalidParameterError.Status, + Code: InvalidParameterError.Code, + Message: InvalidParameterError.Message, + MoreInfo: moreInfo, + Data: map[string]interface{}{"name": name}, + LogData: logData, } } @@ -63,6 +64,8 @@ type ErrorResponse struct { Code string `json:"code"` // Error message that will be returned to API consumer Message string `json:"message"` + // Additional information returned to API consumer + MoreInfo string `json:"more_info,omitempty"` // Error data that will be returned to API consumer Data map[string]interface{} `json:"data,omitempty"` // Error message that will be logged. diff --git a/src/github.com/stellar/gateway/protocols/validation.go b/src/github.com/stellar/gateway/protocols/validation.go index 301615e..70109dd 100644 --- a/src/github.com/stellar/gateway/protocols/validation.go +++ b/src/github.com/stellar/gateway/protocols/validation.go @@ -11,6 +11,25 @@ func IsValidAccountID(accountID string) bool { if err != nil { return false } + + if accountID[0] != 'G' { + return false + } + + return true +} + +// IsValidSecret returns true if secret is valid +func IsValidSecret(secret string) bool { + _, err := keypair.Parse(secret) + if err != nil { + return false + } + + if secret[0] != 'S' { + return false + } + return true } diff --git a/src/github.com/stellar/gateway/test/helpers.go b/src/github.com/stellar/gateway/test/helpers.go index c323c52..37a912f 100644 --- a/src/github.com/stellar/gateway/test/helpers.go +++ b/src/github.com/stellar/gateway/test/helpers.go @@ -5,7 +5,13 @@ import ( ) // StringToJSONMap transforms -func StringToJSONMap(value string) (m map[string]interface{}) { - json.Unmarshal([]byte(value), &m) +func StringToJSONMap(value string, ignoredFields ...string) (m map[string]interface{}) { + err := json.Unmarshal([]byte(value), &m) + if err != nil { + panic(err) + } + for _, ignoredField := range ignoredFields { + delete(m, ignoredField) + } return }