2018-06-06 19:37:03 +00:00
|
|
|
import React, { Fragment, PureComponent } from 'react';
|
2018-06-11 17:18:59 +00:00
|
|
|
import ENSSubdomainRegistry from 'Embark/contracts/ENSSubdomainRegistry';
|
2018-06-06 19:37:03 +00:00
|
|
|
import { Button, Field, TextInput, Card, Info, Text } from '../../ui/components'
|
2018-06-08 13:11:51 +00:00
|
|
|
import { IconCheck } from '../../ui/icons'
|
2018-06-06 19:37:03 +00:00
|
|
|
import theme from '../../ui/theme'
|
2018-06-05 20:48:30 +00:00
|
|
|
import { withFormik } from 'formik';
|
|
|
|
import PublicResolver from 'Embark/contracts/PublicResolver';
|
|
|
|
import { hash } from 'eth-ens-namehash';
|
2018-06-06 19:37:03 +00:00
|
|
|
import { CopyToClipboard } from 'react-copy-to-clipboard';
|
2018-06-11 17:18:59 +00:00
|
|
|
import RegisterSubDomain from '../ens/registerSubDomain';
|
|
|
|
const { getPrice } = ENSSubdomainRegistry.methods;
|
2018-06-05 14:43:05 +00:00
|
|
|
|
2018-06-06 19:37:03 +00:00
|
|
|
const nullAddress = '0x0000000000000000000000000000000000000000'
|
2018-06-11 17:18:59 +00:00
|
|
|
const validAddress = address => address != nullAddress;
|
2018-06-05 20:48:30 +00:00
|
|
|
const formatName = domainName => domainName.includes('.') ? domainName : `${domainName}.stateofus.eth`;
|
2018-06-11 17:18:59 +00:00
|
|
|
const getDomain = fullDomain => formatName(fullDomain).split('.').slice(1).join('.');
|
|
|
|
const hashedDomain = domainName => hash(getDomain(domainName));
|
2018-06-06 19:37:03 +00:00
|
|
|
|
2018-06-05 14:43:05 +00:00
|
|
|
const cardStyle = {
|
|
|
|
width: '75%',
|
|
|
|
marginLeft: '15%',
|
|
|
|
padding: '30px'
|
|
|
|
}
|
2018-06-05 00:34:56 +00:00
|
|
|
|
2018-06-06 19:37:03 +00:00
|
|
|
const addressStyle = {
|
|
|
|
fontSize: '18px',
|
|
|
|
fontWeight: 400,
|
2018-06-07 21:55:50 +00:00
|
|
|
cursor: 'copy',
|
2018-06-08 13:11:51 +00:00
|
|
|
wordWrap: 'break-word',
|
2018-06-06 19:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const backButton = {
|
|
|
|
fontSize: '40px',
|
2018-06-07 21:55:50 +00:00
|
|
|
color: theme.accent,
|
|
|
|
cursor: 'pointer'
|
2018-06-06 19:37:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 17:18:59 +00:00
|
|
|
class Register extends PureComponent {
|
|
|
|
state = { domainPrice: null };
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { domainName } = this.props;
|
|
|
|
getPrice(hashedDomain(domainName))
|
|
|
|
.call()
|
|
|
|
.then((res) => { this.setState({ domainPrice: res })});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { domainName, setStatus } = this.props;
|
|
|
|
const { domainPrice } = this.state;
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<Info.Action title="No address is associated with this domain">
|
|
|
|
{formatName(domainName.toUpperCase())} can be registered for {domainPrice} SNT
|
|
|
|
</Info.Action>
|
|
|
|
<RegisterSubDomain />
|
|
|
|
<div style={backButton} onClick={() => setStatus(null)}>←</div>
|
|
|
|
</Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 19:37:03 +00:00
|
|
|
class DisplayAddress extends PureComponent {
|
|
|
|
state = { copied: false }
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { copied } = this.state
|
2018-06-07 21:55:50 +00:00
|
|
|
const { domainName, address, statusAccount, setStatus } = this.props
|
2018-06-08 13:11:51 +00:00
|
|
|
const markCopied = (v) => { this.setState({ copied: v }) }
|
|
|
|
const isCopied = address => address == copied;
|
|
|
|
const renderCopied = address => isCopied(address) && <span style={{ color: theme.positive }}><IconCheck/>Copied!</span>;
|
2018-06-06 19:37:03 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2018-06-11 17:18:59 +00:00
|
|
|
{validAddress(address) ?
|
|
|
|
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
|
|
|
<Info.Action title="Click to copy"><b>{domainName.toUpperCase()}</b> Resolves To:</Info.Action>
|
|
|
|
{address && <Text style={{ marginTop: '1em' }}>Ethereum Address {renderCopied(address)}</Text>}
|
|
|
|
<CopyToClipboard text={address} onCopy={markCopied}>
|
|
|
|
<div style={addressStyle}>{address}</div>
|
|
|
|
</CopyToClipboard>
|
|
|
|
{statusAccount && <Text style={{ marginTop: '1em' }}>Status Address {renderCopied(statusAccount)}</Text>}
|
|
|
|
<CopyToClipboard text={statusAccount} onCopy={markCopied}>
|
|
|
|
<div style={{ ...addressStyle, color: isCopied ? theme.primary : null }}>{statusAccount}</div>
|
|
|
|
</CopyToClipboard>
|
|
|
|
</div>
|
|
|
|
:
|
|
|
|
<Info.Action title="No address is associated with this domain">
|
|
|
|
{domainName.toUpperCase()}
|
|
|
|
</Info.Action>}
|
|
|
|
<div style={backButton} onClick={() => setStatus(null)}>←</div>
|
2018-06-06 19:37:03 +00:00
|
|
|
</Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:48:30 +00:00
|
|
|
const InnerForm = ({
|
|
|
|
values,
|
|
|
|
errors,
|
|
|
|
touched,
|
|
|
|
handleChange,
|
|
|
|
handleBlur,
|
|
|
|
handleSubmit,
|
|
|
|
isSubmitting,
|
2018-06-06 19:37:03 +00:00
|
|
|
status,
|
|
|
|
setStatus
|
2018-06-05 20:48:30 +00:00
|
|
|
}) => (
|
2018-06-05 14:43:05 +00:00
|
|
|
<Card style={cardStyle}>
|
2018-06-11 17:18:59 +00:00
|
|
|
{!status
|
|
|
|
? <form onSubmit={handleSubmit} style={{ marginTop: '3em' }}>
|
|
|
|
<Field label="Enter Domain or Status Name" wide>
|
|
|
|
<TextInput
|
|
|
|
value={values.domainName}
|
|
|
|
name="domainName"
|
|
|
|
onChange={handleChange}
|
|
|
|
wide
|
|
|
|
required />
|
|
|
|
</Field>
|
|
|
|
<Button mode="strong" type="submit" wide>
|
|
|
|
Get Address
|
|
|
|
</Button>
|
|
|
|
</form>
|
|
|
|
: validAddress(status.address) ?
|
|
|
|
<DisplayAddress
|
|
|
|
domainName={values.domainName}
|
|
|
|
address={status.address}
|
|
|
|
statusAccount={status.statusAccount}
|
|
|
|
setStatus={setStatus} /> :
|
|
|
|
<Register
|
|
|
|
setStatus={setStatus}
|
|
|
|
domainName={values.domainName} />
|
|
|
|
}
|
2018-06-05 14:43:05 +00:00
|
|
|
</Card>
|
2018-06-05 00:34:56 +00:00
|
|
|
)
|
|
|
|
|
2018-06-05 20:48:30 +00:00
|
|
|
const NameLookup = withFormik({
|
|
|
|
mapPropsToValues: props => ({ domainName: '' }),
|
2018-06-07 21:55:50 +00:00
|
|
|
async handleSubmit(values, { status, setSubmitting, setStatus }) {
|
2018-06-05 20:48:30 +00:00
|
|
|
const { domainName } = values;
|
2018-06-07 21:55:50 +00:00
|
|
|
const { addr, text } = PublicResolver.methods;
|
|
|
|
const lookupHash = hash(formatName(domainName));
|
|
|
|
const address = await addr(lookupHash).call();
|
|
|
|
const statusAccount = await text(lookupHash, 'statusAccount').call();
|
|
|
|
setStatus({ address, statusAccount });
|
2018-06-05 20:48:30 +00:00
|
|
|
}
|
|
|
|
})(InnerForm)
|
|
|
|
|
2018-06-05 00:34:56 +00:00
|
|
|
export default NameLookup;
|