-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Sign.tsx
238 lines (219 loc) · 7.21 KB
/
Sign.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
229
230
231
232
233
234
235
236
237
238
// Copyright 2017-2019 @polkadot/app-toolbox 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 { Signer } from '@polkadot/api/types';
import { I18nProps } from '@polkadot/react-components/types';
import { KeyringPair } from '@polkadot/keyring/types';
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import { web3FromSource } from '@polkadot/extension-dapp';
import { Button, Input, InputAddress, Output, Static } from '@polkadot/react-components';
import { useToggle } from '@polkadot/react-hooks';
import keyring from '@polkadot/ui-keyring';
import { hexToU8a, isFunction, isHex, stringToHex, stringToU8a, u8aToHex } from '@polkadot/util';
import translate from './translate';
import Unlock from './Unlock';
interface Props extends I18nProps {
className?: string;
}
interface AccountState {
isExternal: boolean;
isHardware: boolean;
isInjected: boolean;
}
function Sign ({ className, t }: Props): React.ReactElement<Props> {
const [currentPair, setCurrentPair] = useState<KeyringPair | null>(keyring.getPairs()[0] || null);
const [{ data, isHexData }, setData] = useState<{ data: string; isHexData: boolean }>({ data: '', isHexData: false });
const [{ isInjected }, setAccountState] = useState<AccountState>({
isExternal: false,
isHardware: false,
isInjected: false
});
const [isLocked, setIsLocked] = useState(false);
const [{ isUsable, signer }, setSigner] = useState<{ isUsable: boolean; signer: Signer | null }>({ isUsable: true, signer: null });
const [signature, setSignature] = useState('');
const [isUnlockVisible, toggleUnlock] = useToggle();
useEffect((): void => {
const isExternal = currentPair?.meta.isExternal || false;
const isHardware = currentPair?.meta.isHardware || false;
const isInjected = currentPair?.meta.isInjected || false;
const isUsable = !(isExternal || isHardware || isInjected);
setAccountState({ isExternal, isHardware, isInjected });
setIsLocked(
isInjected
? false
: currentPair?.isLocked || false
);
setSignature('');
setSigner({ isUsable, signer: null });
// for injected, retrieve the signer
if (currentPair && isInjected) {
const { meta: { source } } = currentPair;
web3FromSource(source)
.catch((): null => null)
.then((injected): void => setSigner({
isUsable: isFunction(injected?.signer?.signRaw),
signer: injected?.signer || null
}));
}
}, [currentPair]);
const _onChangeAccount = (accountId: string | null): void => setCurrentPair(keyring.getPair(accountId || ''));
const _onChangeData = (data: string): void => setData({ data, isHexData: isHex(data) });
const _onSign = (): void => {
if (isLocked || !isUsable || !currentPair) {
return;
}
if (signer?.signRaw) {
setSignature('');
signer
.signRaw({
address: currentPair.address,
data: isHexData
? data
: stringToHex(data),
type: 'bytes'
})
.then(({ signature }): void => setSignature(signature));
} else {
setSignature(u8aToHex(
currentPair.sign(
isHexData
? hexToU8a(data)
: stringToU8a(data)
)
));
}
};
const _onUnlock = (): void => {
setIsLocked(false);
toggleUnlock();
};
return (
<div className={`toolbox--Sign ${className}`}>
<div className='ui--row'>
<InputAddress
className='full'
help={t('select the account you wish to sign data with')}
isInput={false}
label={t('account')}
onChange={_onChangeAccount}
type='account'
/>
</div>
<div className='toolbox--Sign-input'>
<div className='ui--row'>
<Input
autoFocus
className='full'
help={t('The input data to sign. This can be either specified as a hex value (0x-prefix) or as a string.')}
label={t('sign the following data')}
onChange={_onChangeData}
value={data}
/>
</div>
<div className='ui--row'>
<Static
className='medium'
help={t('Detection on the input string to determine if it is hex or non-hex.')}
label={t('hex input data')}
value={
isHexData
? t('Yes')
: t('No')
}
/>
</div>
<div className='ui--row'>
<Output
className='full'
help={t('The resulting signature of the input data, as done with the crypto algorithm from the account. (This could be non-deterministic for some types such as sr25519).')}
isHidden={signature.length === 0}
isMonospace
label={t('signature of supplied data')}
value={signature}
withCopy
/>
</div>
<div
className='unlock-overlay'
hidden={!isUsable || !isLocked || isInjected}
>
{isLocked && (
<div className='unlock-overlay-warning'>
<div className='unlock-overlay-content'>
{t('You need to unlock this account to be able to sign data.')}<br/>
<Button.Group>
<Button
isPrimary
onClick={toggleUnlock}
label={t('Unlock account')}
icon='unlock'
/>
</Button.Group>
</div>
</div>
)}
</div>
<div
className='unlock-overlay'
hidden={isUsable}
>
<div className='unlock-overlay-warning'>
<div className='unlock-overlay-content'>
{isInjected
? t('This injected account cannot be used to sign data since the extension does not support raw signing.')
: t('This external account cannot be used to sign data. Only Limited support is currently available for signing from any non-internal accounts.')}
</div>
</div>
</div>
{isUnlockVisible && (
<Unlock
onClose={toggleUnlock}
onUnlock={_onUnlock}
pair={currentPair}
/>
)}
</div>
<Button.Group>
<Button
icon='privacy'
isDisabled={!(isUsable && !isLocked)}
isPrimary
label={t('Sign message')}
onClick={_onSign}
/>
</Button.Group>
</div>
);
}
export default translate(
styled(Sign)`
.toolbox--Sign-input {
position: relative;
width: 100%;
height: 100%;
.unlock-overlay {
position: absolute;
width: 100%;
height: 100%;
top:0;
left:0;
background-color: #0f0e0e7a;
}
.unlock-overlay-warning {
display: flex;
align-items: center;
justify-content: center;
height:100%;
}
.unlock-overlay-content {
color:#fff;
padding: 0 2.5rem;
text-align:center;
.ui--Button-Group {
text-align: center;
}
}
}
`
);