From edf59f261ef3cce9daf36d3748262dd107cfce5d Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Fri, 25 May 2018 18:50:16 -0400 Subject: [PATCH] add check on form to ensure registry owns domain --- app/components/ens/addDomain.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/app/components/ens/addDomain.js b/app/components/ens/addDomain.js index da62e80..3cbc487 100644 --- a/app/components/ens/addDomain.js +++ b/app/components/ens/addDomain.js @@ -1,15 +1,22 @@ import ENSSubdomainRegistry from 'Embark/contracts/ENSSubdomainRegistry'; +import ENSRegistry from 'Embark/contracts/ENSRegistry'; import React, { Fragment } from 'react'; import { Form, FormGroup, FormControl, HelpBlock, Button, ControlLabel } from 'react-bootstrap'; import { withFormik } from 'formik'; import { hash } from 'eth-ens-namehash' import { debounce } from 'lodash/fp' +const { methods: { owner } } = ENSRegistry; const delay = debounce(1000); const getDomain = (hashedDomain, domains) => domains(hashedDomain).call(); +const registryIsOwner = address => address == ENSSubdomainRegistry._address; +const getAndIsOwner = async domainName => { + const address = await owner(hash(domainName)).call(); + return registryIsOwner(address); +} const fetchDomain = delay(getDomain); -const setPrice = (domainFn, hashedDomain, price) => domainFn(hashedDomain, price || 0).send() +const setPrice = (domainFn, hashedDomain, price) => domainFn(hashedDomain, price || 0).send(); const FieldGroup = ({ id, label, error, ...props }) => ( @@ -49,22 +56,24 @@ const InnerForm = ({ onBlur={handleBlur} value={values.domainPrice} /> - + ) const AddDomain = withFormik({ mapPropsToValues: props => ({ domainName: '', domainPrice: '' }), - validate(values, props) { + async validate(values, props) { + const { domainName } = values const errors = {}; if (!domainName) errors.domainName = 'Required'; - return errors; + if (domainName && !await getAndIsOwner(domainName)) errors.domainName = 'This domain is not owned by registry' + if (Object.keys(errors).length) throw errors; }, async handleSubmit(values, { setSubmitting }) { const { domainName, domainPrice } = values const { methods: { domains, addDomain, setDomainPrice } } = ENSSubdomainRegistry const { sha3 } = window.web3.utils - const hashedDomain = sha3(domainName); + const hashedDomain = hash(domainName) const debugTable = await ['eth', 'stateofus', 'stateofus.eth', 'freedomain', 'freedomain.eth', domainName] .map(async str => { const result = {}; @@ -82,7 +91,7 @@ const AddDomain = withFormik({ Promise.all(debugTable).then(v => { console.table(v) }); const { state } = await getDomain(hashedDomain, domains); setPrice( - !!state ? setDomainPrice : addDomain, + !!Number(state) ? setDomainPrice : addDomain, hashedDomain, domainPrice )