This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Claim.tsx
228 lines (204 loc) · 7.94 KB
/
Claim.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2018-2019 @paritytech/substrate-light-ui authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import QrSigner from '@parity/qr-signer';
import accounts from '@polkadot/ui-keyring/observable/accounts';
import { stringToHex } from '@polkadot/util';
import { Option, H160 } from '@polkadot/types';
import { BalanceOf, EthereumAddress } from '@polkadot/types/interfaces';
import { AppContext } from '@substrate/ui-common';
import { BoldText, CopyButton, ErrorText, FadedText, FlexSegment, Header, Loading, Margin, Message, Modal, SubHeader, StyledNavButton, StyledLinkButton, Stacked, StackedHorizontal, TextArea } from '@substrate/ui-components';
import { Either, left, right } from 'fp-ts/lib/Either';
import { fromNullable } from 'fp-ts/lib/Option';
import React, { useContext, useEffect, useState } from 'react';
import { RouteComponentProps } from 'react-router';
import { map, take } from 'rxjs/operators';
import Card from 'semantic-ui-react/dist/commonjs/views/Card';
type Props = RouteComponentProps;
interface EthereumAddressPayload {
address: string;
chainId: number;
}
const validateEthereumAddress = (addr: string): Either<string, EthereumAddress> => {
if (!addr || addr.length !== 42) {
return left('Invalid Ethereum address.');
} else {
const eAddress = new H160(addr);
return right(eAddress);
}
};
export function Claim (props: Props): React.ReactElement {
const { history } = props;
const { api } = useContext(AppContext);
const [claim, setClaim] = useState();
const [claimError, setClaimError] = useState();
const [ethereumAddress, setEthereumAddress] = useState<EthereumAddress>();
const [loading, setLoading] = useState<boolean>(false);
const [messageToSign, setMessageToSign] = useState<string>('');
const [renderQr, setRenderQr] = useState<boolean>(true);
const [scanSignature, setScanSignature] = useState<boolean>(false);
const [signature, setSignature] = useState();
useEffect(() => {
// eslint-disable-next-line @typescript-eslint/unbound-method
const accountsSub = accounts.subject.pipe(map(Object.values)).subscribe(values => {
const stash = values.filter(value =>
fromNullable(value.json.meta.tags)
.map(tags => tags.includes('stash'))
.getOrElse(undefined)
)[0];
const _messageToSign = fromNullable(stash)
.map(stash => `Pay KSMs to the Kusama account: ${stash.json.address}`)
.getOrElse('');
setMessageToSign(_messageToSign);
});
return (): void => accountsSub.unsubscribe();
}, []);
const handleSubmitClaim = (): void => {
if (!ethereumAddress) {
setClaimError('Please set your Ethereum address to claim.');
return;
}
setLoading(true);
const claimsSub = api.query.claims.claims<Option<BalanceOf>>(ethereumAddress.toHex())
.pipe(take(1)).subscribe((claim) => {
console.log('claim => ', claim);
setLoading(false);
claim.isSome
? setClaim(claim.unwrap())
: setClaimError('There is no claim associated with the provided Ethereum address. Please make sure you signed the above message with the same account as when you purchased your KSM and try again. If this problem persists, please raise an issue at https://riot.im/app/#/room/#KSMAClaims:polkadot.builders');
claimsSub.unsubscribe();
});
};
const handleScanSignature = (signature: any): void => {
setSignature(signature);
};
const handleSetEthereumAddress = (data: EthereumAddressPayload): void => {
if (!data) {
setClaimError('invalid payload.');
return;
} else if (data.chainId !== 1) {
setClaimError('You can only sign a claims transaction on the Ethereum Mainnet.');
return;
}
const validate = validateEthereumAddress(data.address);
validate.fold(
(err: string) => setClaimError(err),
(validAddress: EthereumAddress) => {
setEthereumAddress(validAddress);
setClaimError(undefined);
}
);
};
const renderCheckingClaim = (): React.ReactElement => {
return (
<Stacked>
{
loading
? <Loading active inline />
: (
<Stacked>
<Header>Signature valid!</Header>
<FadedText>Press the button below to claim your KSM!</FadedText>
<StyledNavButton negative onClick={handleSubmitClaim}>Check Claim</StyledNavButton>
<ErrorText>{claimError}</ErrorText>
</Stacked>
)
}
</Stacked>
);
};
const renderWithRaw = (): React.ReactElement => {
return (
<Card.Group itemsPerRow={2} stackable>
<Card raised>
<Card.Header><Header>Sign Eth Transaction</Header></Card.Header>
<Card.Content>
<SubHeader>Copy the below string and sign an Ethereum transaction with the account you used during the pre-sale.</SubHeader>
<Margin top />
<FlexSegment width={'100%'}>
{
messageToSign && (
<React.Fragment>
<BoldText>{messageToSign}</BoldText>
<CopyButton value={messageToSign} />
</React.Fragment>
)
}
</FlexSegment>
</Card.Content>
</Card>
<Card>
<Card.Header><Header>Transaction Signature</Header></Card.Header>
<Card.Content>
<SubHeader>Paste the resulting signature object from the above transaction below:</SubHeader>
<TextArea />
</Card.Content>
</Card>
</Card.Group>
);
};
const renderWithQr = (): React.ReactElement => {
return (
<Card.Content centered>
<Stacked>
{
!ethereumAddress
? <Stacked>
<FadedText> Scan your Ethereum account address below to unlock it. </FadedText>
<QrSigner scan={true} onScan={handleSetEthereumAddress} />
</Stacked>
: <React.Fragment>
{
messageToSign &&
<React.Fragment>
<Message floating>
<FadedText>Scan the QR Code below to sign the following message.</FadedText>
<BoldText>{messageToSign}</BoldText>
</Message>
{
scanSignature
? (<QrSigner onScan={handleScanSignature} scan={true} size={300} />)
: (<QrSigner account={ethereumAddress.toHex()} data={stringToHex(messageToSign)} scan={false} size={300} />)
}
</React.Fragment>
}
</React.Fragment>
}
<StyledLinkButton onClick={(): void => setScanSignature(!scanSignature)}>{scanSignature ? 'Show QR' : 'Scan Signature'}</StyledLinkButton>
</Stacked>
</Card.Content>
);
};
const renderStartClaim = (): React.ReactElement => {
return (
<React.Fragment>
<StyledLinkButton onClick={(): void => setRenderQr(!renderQr)}>{renderQr ? 'Raw' : 'QR'} </StyledLinkButton>
{
renderQr
? renderWithQr()
: renderWithRaw()
}
<ErrorText>{claimError}</ErrorText>
</React.Fragment>
);
};
return (
<React.Fragment>
<Modal.Content>
{
claim
? claim.toString()
: signature
? renderCheckingClaim()
: renderStartClaim()
}
</Modal.Content>
<Modal.Actions>
<StackedHorizontal justifyContent='space-around'>
<StyledLinkButton onClick={(): void => history.push('/onboarding/bond')}>Skip</StyledLinkButton>
<StyledNavButton disabled={!claim} onClick={(): void => history.push('/onboarding/bond')}>Next</StyledNavButton>
</StackedHorizontal>
</Modal.Actions>
</React.Fragment>
);
}