diff options
Diffstat (limited to 'packages/order-utils/src')
-rw-r--r-- | packages/order-utils/src/artifacts.ts | 6 | ||||
-rw-r--r-- | packages/order-utils/src/signature_utils.ts | 42 |
2 files changed, 43 insertions, 5 deletions
diff --git a/packages/order-utils/src/artifacts.ts b/packages/order-utils/src/artifacts.ts index 6eb5ad0c0..f6fd00472 100644 --- a/packages/order-utils/src/artifacts.ts +++ b/packages/order-utils/src/artifacts.ts @@ -1,8 +1,10 @@ import { Artifact } from '@0xproject/types'; import * as Exchange from './artifacts/Exchange.json'; -import * as ISigner from './artifacts/ISigner.json'; +import * as IValidator from './artifacts/IValidator.json'; +import * as IWallet from './artifacts/IWallet.json'; export const artifacts = { Exchange: (Exchange as any) as Artifact, - ISigner: (ISigner as any) as Artifact, + IWallet: (IWallet as any) as Artifact, + IValidator: (IValidator as any) as Artifact, }; diff --git a/packages/order-utils/src/signature_utils.ts b/packages/order-utils/src/signature_utils.ts index 8e387a6ae..427448260 100644 --- a/packages/order-utils/src/signature_utils.ts +++ b/packages/order-utils/src/signature_utils.ts @@ -7,7 +7,8 @@ import * as _ from 'lodash'; import { artifacts } from './artifacts'; import { assert } from './assert'; import { ExchangeContract } from './generated_contract_wrappers/exchange'; -import { ISignerContract } from './generated_contract_wrappers/i_signer'; +import { IWalletContract } from './generated_contract_wrappers/i_signer'; +import { IValidatorContract } from './generated_contract_wrappers/i_validator'; import { MessagePrefixOpts, MessagePrefixType, OrderError } from './types'; /** @@ -51,12 +52,27 @@ export async function isValidSignatureAsync( throw new Error('Caller signature type cannot be validated off-chain'); case SignatureType.Wallet: { - const signerContract = new ISignerContract(artifacts.ISigner.abi, signerAddress, provider); + const signerContract = new IWalletContract(artifacts.IWallet.abi, signerAddress, provider); const isValid = await signerContract.isValidSignature.callAsync(data, signature); return isValid; } - // TODO: Add SignatureType.Validator + case SignatureType.Validator: { + const validatorAddress = getValidatorAddressFromSignature(signature); + const exchangeContract = new ExchangeContract(artifacts.Exchange.abi, signerAddress, provider); + const isValidatorApproved = await exchangeContract.allowedValidators.callAsync( + signerAddress, + validatorAddress, + ); + if (!isValidatorApproved) { + throw new Error(`Validator ${validatorAddress} was not pre-approved by ${signerAddress}.`); + } + + const validatorSignature = getValidatorSignatureFromSignature(signature); + const validatorContract = new IValidatorContract(artifacts.IValidator.abi, signerAddress, provider); + const isValid = await validatorContract.isValidSignature.callAsync(data, signerAddress, validatorSignature); + return isValid; + } case SignatureType.PreSigned: { return isValidPresignedSignatureAsync(provider, data, signature, signerAddress); @@ -219,6 +235,26 @@ function parseECSignature(signature: string): ECSignature { return ecSignature; } +function getValidatorSignatureFromSignature(signature: string): string { + const signatureTypeIndex = getSignatureTypeIndexIfExists(signature); + if (signatureTypeIndex !== SignatureType.Validator) { + throw new Error('Cannot get validator address from non-validator signature'); + } + // tslint:disable-next-line:custom-no-magic-numbers + const validatorSignature = signature.slice(0, -22); + return validatorSignature; +} + +function getValidatorAddressFromSignature(signature: string): string { + const signatureTypeIndex = getSignatureTypeIndexIfExists(signature); + if (signatureTypeIndex !== SignatureType.Validator) { + throw new Error('Cannot get validator address from non-validator signature'); + } + // tslint:disable-next-line:custom-no-magic-numbers + const validatorAddress = signature.slice(-22, -2); + return `0x${validatorAddress}`; +} + function getSignatureTypeIndexIfExists(signature: string): number { const unprefixedSignature = ethUtil.stripHexPrefix(signature); // tslint:disable-next-line:custom-no-magic-numbers |