generated from jimmychu0807/substrate-front-end-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transfer.js
117 lines (103 loc) · 3.69 KB
/
Transfer.js
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
/* eslint-disable no-unused-vars */
import React, {useState, useEffect} from 'react'
import {Form, Input, Grid, Label, Icon, Dropdown} from 'semantic-ui-react'
import {TxButton} from './substrate-lib/components'
// import { useSubstrateState } from './substrate-lib'
import {Keyring} from '@polkadot/keyring'
import {mnemonicGenerate, mnemonicToMiniSecret, mnemonicValidate, ed25519PairFromSeed} from '@polkadot/util-crypto'
import {useSubstrate, useSubstrateState} from './substrate-lib'
import {u8aToHex} from "@polkadot/util";
export default function Main(props) {
const {
setCurrentAccount,
state: {keyring, currentAccount, vaultAccount},
} = useSubstrate()
useEffect(() => {
//const storedAddress = sessionStorage.getItem('currentAccount');
//if (storedAddress) {
// setCurrentAccount(storedAddress);
//}
}, []);
const [mnemonic, setMnemonic] = useState('');
const [status, setStatus] = useState(null)
const [formState, setFormState] = useState({addressTo: '', amount: 0})
const onChange = (_, data) =>
setFormState(prev => ({...prev, [data.state]: data.value}))
const {addressTo, amount} = formState
const accounts = [currentAccount]
const availableAccounts = []
accounts.map(account => {
return availableAccounts.push({
key: account,
text: account,
value: account,
})
})
const createAccount = async () => {
const generatedMnemonic = mnemonicGenerate();
//setMnemonic(generatedMnemonic); // Storing the mnemonic in state (hypothetically)
const localKeyring = new Keyring({type: 'sr25519', ss58Format: 42});
// Add account from mnemonic
const account = localKeyring.addFromMnemonic(generatedMnemonic, {name: 'fresh'});
// Create valid Substrate-compatible seed from mnemonic
const seed = mnemonicToMiniSecret(generatedMnemonic);
// Convert the private key to a hexadecimal string
const privateKeyHex = u8aToHex(seed);
console.log(`Private Key in Hex: ${privateKeyHex}`);
keyring.addPair(account);
console.log(keyring.getPairs());
setCurrentAccount(account);
const currentUrl = new URL(window.location.href);
currentUrl.searchParams.set('seed', privateKeyHex);
window.history.pushState({path: currentUrl.href}, '', currentUrl.href);
console.log(`New account created: ${account.address}`)
};
console.log(`Account: ${currentAccount?.address}`)
return (
<Grid.Column width={8}>
<h1>Transfer PAS on L1</h1>
Vault Account: {vaultAccount}
<Form>
<Form.Field>
<Input
fluid
label="To"
type="text"
placeholder="address"
value={addressTo}
state="addressTo"
onChange={onChange}
/>
</Form.Field>
<Form.Field>
<Input
fluid
step='0.1'
label="Amount"
type="number"
state="amount"
placeholder='Enter PAS Amount'
pattern='^([1-9](?:\.[1-9])?|0?\.[1-9])$'
onChange={onChange}
/>
</Form.Field>
<Form.Field style={{textAlign: 'center'}}>
<TxButton
label="Submit"
type="SIGNED-TX"
setStatus={setStatus}
attrs={{
palletRpc: 'balances',
callable: 'transferKeepAlive',
inputParams: [addressTo, amount * Math.pow(10, 10)],
paramFields: [true, true],
}}
/>
<button onClick={createAccount}>Create Account</button>
{/* <TxButton accountAddress={accountAddress} /> */}
</Form.Field>
<div style={{overflowWrap: 'break-word'}}>{status}</div>
</Form>
</Grid.Column>
)
}