This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
933 additions
and
616 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,26 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/0xProject/0x-mesh/constants" | ||
"github.com/0xProject/0x-mesh/p2p" | ||
"github.com/xeipuuv/gojsonschema" | ||
"github.com/0xProject/0x-mesh/zeroex" | ||
) | ||
|
||
// JSON-schema schemas | ||
var ( | ||
addressSchemaLoader = gojsonschema.NewStringLoader(`{"id":"/addressSchema","type":"string","pattern":"^0x[0-9a-fA-F]{40}$"}`) | ||
wholeNumberSchemaLoader = gojsonschema.NewStringLoader(`{"id":"/wholeNumberSchema","anyOf":[{"type":"string","pattern":"^\\d+$"},{"type":"integer"}]}`) | ||
hexSchemaLoader = gojsonschema.NewStringLoader(`{"id":"/hexSchema","type":"string","pattern":"^0x(([0-9a-fA-F])*)?$"}`) | ||
orderSchemaLoader = gojsonschema.NewStringLoader(`{"id":"/orderSchema","properties":{"makerAddress":{"$ref":"/addressSchema"},"takerAddress":{"$ref":"/addressSchema"},"makerFee":{"$ref":"/wholeNumberSchema"},"takerFee":{"$ref":"/wholeNumberSchema"},"senderAddress":{"$ref":"/addressSchema"},"makerAssetAmount":{"$ref":"/wholeNumberSchema"},"takerAssetAmount":{"$ref":"/wholeNumberSchema"},"makerAssetData":{"$ref":"/hexSchema"},"takerAssetData":{"$ref":"/hexSchema"},"makerFeeAssetData":{"$ref":"/hexSchema"},"takerFeeAssetData":{"$ref":"/hexSchema"},"salt":{"$ref":"/wholeNumberSchema"},"feeRecipientAddress":{"$ref":"/addressSchema"},"expirationTimeSeconds":{"$ref":"/wholeNumberSchema"},"exchangeAddress":{"$ref":"/addressSchema"},"chainId": {"type": "number"}},"required":["makerAddress","takerAddress","makerFee","takerFee","senderAddress","makerAssetAmount","takerAssetAmount","makerAssetData","takerAssetData","makerFeeAssetData","takerFeeAssetData","salt","feeRecipientAddress","expirationTimeSeconds","exchangeAddress","chainId"],"type":"object"}`) | ||
signedOrderSchemaLoader = gojsonschema.NewStringLoader(`{"id":"/signedOrderSchema","allOf":[{"$ref":"/orderSchema"},{"properties":{"signature":{"$ref":"/hexSchema"}},"required":["signature"]}]}`) | ||
meshMessageSchemaLoader = gojsonschema.NewStringLoader(`{"id":"/meshMessageSchema","properties":{"MessageType":{"type":"string"},"Order":{"$ref":"/signedOrderSchema"}},"required":["MessageType","Order"]}`) | ||
) | ||
|
||
func setupMeshMessageSchemaValidator() (*gojsonschema.Schema, error) { | ||
sl := gojsonschema.NewSchemaLoader() | ||
if err := sl.AddSchemas(addressSchemaLoader); err != nil { | ||
return nil, err | ||
} | ||
if err := sl.AddSchemas(wholeNumberSchemaLoader); err != nil { | ||
return nil, err | ||
} | ||
if err := sl.AddSchemas(hexSchemaLoader); err != nil { | ||
return nil, err | ||
} | ||
if err := sl.AddSchemas(orderSchemaLoader); err != nil { | ||
return nil, err | ||
} | ||
if err := sl.AddSchemas(signedOrderSchemaLoader); err != nil { | ||
return nil, err | ||
} | ||
schema, err := sl.Compile(meshMessageSchemaLoader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return schema, nil | ||
} | ||
|
||
func setupOrderSchemaValidator() (*gojsonschema.Schema, error) { | ||
sl := gojsonschema.NewSchemaLoader() | ||
if err := sl.AddSchemas(addressSchemaLoader); err != nil { | ||
return nil, err | ||
} | ||
if err := sl.AddSchemas(wholeNumberSchemaLoader); err != nil { | ||
return nil, err | ||
} | ||
if err := sl.AddSchemas(hexSchemaLoader); err != nil { | ||
return nil, err | ||
} | ||
if err := sl.AddSchemas(orderSchemaLoader); err != nil { | ||
return nil, err | ||
} | ||
schema, err := sl.Compile(signedOrderSchemaLoader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return schema, nil | ||
} | ||
|
||
func (app *App) schemaValidateOrder(o []byte) (*gojsonschema.Result, error) { | ||
orderLoader := gojsonschema.NewBytesLoader(o) | ||
|
||
result, err := app.orderJSONSchema.Validate(orderLoader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (app *App) schemaValidateMeshMessage(o []byte) (*gojsonschema.Result, error) { | ||
messageLoader := gojsonschema.NewBytesLoader(o) | ||
|
||
result, err := app.meshMessageJSONSchema.Validate(messageLoader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
func validateMessageSize(message *p2p.Message) error { | ||
// TODO(albrow): split up max order size and max message size. | ||
// if len(message.Data) > constants.MaxOrderSizeInBytes { | ||
// return errMaxSize | ||
// } | ||
return nil | ||
} | ||
|
||
func validateMessageSize(message *p2p.Message) error { | ||
if len(message.Data) > constants.MaxOrderSizeInBytes { | ||
return constants.ErrMaxMessageSize | ||
} | ||
func validateOrderSize(order *zeroex.SignedOrder) error { | ||
// TODO(albrow): split up max order size and max message size. | ||
// encoded, err := encodeOrder(order) | ||
// if err != nil { | ||
// return err | ||
// } | ||
// if len(encoded) > constants.MaxOrderSizeInBytes { | ||
// return errMaxSize | ||
// } | ||
return nil | ||
} |
Oops, something went wrong.