Skip to content

Commit 65324d2

Browse files
committed
Address book
1 parent c851740 commit 65324d2

File tree

6 files changed

+148
-21
lines changed

6 files changed

+148
-21
lines changed

docs/bundle.js

Lines changed: 31 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AccountIdBond.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Identicon = require('polkadot-identicon').default;
55
const {Label, Input} = require('semantic-ui-react');
66
const {InputBond} = require('./InputBond');
77
const nacl = require('tweetnacl');
8-
const {stringToSeed, hexToBytes, bytesToHex, runtime, secretStore, ss58Decode, AccountId} = require('oo7-substrate');
8+
const {stringToSeed, hexToBytes, bytesToHex, runtime, secretStore, addressBook, ss58Decode, AccountId} = require('oo7-substrate');
99

1010
class AccountIdBond extends InputBond {
1111
constructor () { super() }
@@ -33,7 +33,13 @@ AccountIdBond.defaultProps = {
3333
validator: a => {
3434
let y = secretStore().find(a);
3535
if (y) {
36-
return { external: new AccountId(ss58Decode(y.address)), internal: a, ok: true, extra: { knowSecret: true } };
36+
console.log('ss found', a, y)
37+
return { external: y.account, internal: a, ok: true, extra: { knowSecret: true } };
38+
}
39+
let z = addressBook().find(a);
40+
if (z) {
41+
console.log('ab found', a, z)
42+
return { external: z.account, internal: a, ok: true, extra: { knowSecret: false } };
3743
}
3844
return runtime.balances.ss58Decode(a).map(
3945
x => x

src/AddressBookList.jsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import {List, Button} from 'semantic-ui-react';
3+
import {ReactiveComponent} from 'oo7-react';
4+
import {runtime, addressBook} from 'oo7-substrate';
5+
import Identicon from 'polkadot-identicon';
6+
7+
export class AddressBookList extends ReactiveComponent {
8+
constructor () {
9+
super([], {
10+
addressBook: addressBook(),
11+
shortForm: addressBook().map(ss => {
12+
let r = {}
13+
ss.accounts.forEach(account => r[account.name] = runtime.balances.ss58Encode(runtime.balances.tryIndex(account.account)))
14+
return r
15+
})
16+
})
17+
}
18+
19+
readyRender () {
20+
return <List divided verticalAlign='bottom' style={{padding: '0 0 4px 4px', overflow: 'auto', maxHeight: '20em'}}>{
21+
this.state.addressBook.accounts.map(account =>
22+
<List.Item key={account.name}>
23+
<List.Content floated='right'>
24+
<Button size='small' onClick={() => addressBook().forget(key)}>Delete</Button>
25+
</List.Content>
26+
<span className='ui avatar image' style={{minWidth: '36px'}}>
27+
<Identicon account={account.account} />
28+
</span>
29+
<List.Content>
30+
<List.Header>{account.name}</List.Header>
31+
<List.Description>
32+
{this.state.shortForm[account.name]}
33+
</List.Description>
34+
</List.Content>
35+
</List.Item>
36+
)
37+
}</List>
38+
}
39+
}

src/TransformBondButton.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export class TransformBondButton extends ReactiveComponent {
77
constructor () {
88
super (['content', 'disabled', 'icon'])
99

10-
this.state = { bond: null, result: null }
10+
this.state = { bond: null, result: undefined }
1111
}
1212

1313
clicked () {
1414
if (this.state.result) {
15-
this.setState({ result: null })
15+
this.setState({ result: undefined })
1616
return
1717
}
1818

src/WalletList.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import {List, Button} from 'semantic-ui-react';
33
import {ReactiveComponent} from 'oo7-react';
4-
import {runtime} from 'oo7-substrate';
4+
import {runtime, secretStore} from 'oo7-substrate';
55
import Identicon from 'polkadot-identicon';
66

77
export class WalletList extends ReactiveComponent {

src/app.jsx

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import React from 'react';
22
require('semantic-ui-css/semantic.min.css');
3-
const { generateMnemonic, mnemonicToSeed } = require('bip39')
3+
const { generateMnemonic } = require('bip39')
44
import {Icon, List, Label, Header, Segment, Divider, Button} from 'semantic-ui-react';
55
import {Bond, TransformBond} from 'oo7';
66
import {ReactiveComponent, If, Rspan} from 'oo7-react';
7-
import {calls, runtime, chain, system, runtimeUp, ss58Encode} from 'oo7-substrate';
7+
import {calls, runtime, chain, system, runtimeUp, ss58Encode, addressBook, secretStore} from 'oo7-substrate';
88
import Identicon from 'polkadot-identicon';
99
import {AccountIdBond, SignerBond} from './AccountIdBond.jsx';
1010
import {BalanceBond} from './BalanceBond.jsx';
1111
import {InputBond} from './InputBond.jsx';
1212
import {TransactButton} from './TransactButton.jsx';
1313
import {StakingStatusLabel} from './StakingStatusLabel';
1414
import {WalletList} from './WalletList';
15+
import {AddressBookList} from './AddressBookList';
1516
import {TransformBondButton} from './TransformBondButton';
1617
import {Pretty} from './Pretty';
1718

@@ -21,6 +22,8 @@ export class App extends ReactiveComponent {
2122

2223
// For debug only.
2324
window.runtime = runtime;
25+
window.secretStore = secretStore;
26+
window.addressBook = addressBook;
2427
window.chain = chain;
2528
window.calls = calls;
2629
window.that = this;
@@ -30,10 +33,11 @@ export class App extends ReactiveComponent {
3033
this.destination = new Bond;
3134
this.staker = new Bond;
3235
this.nomination = new Bond;
36+
this.nick = new Bond;
37+
this.lookup = new Bond;
3338
this.name = new Bond;
3439
this.seed = new Bond;
35-
this.seed.trigger(generateMnemonic())
36-
this.seedAccount = this.seed.map(secretStore().accountFromSeed)
40+
this.seedAccount = this.seed.map(s => s ? secretStore().accountFromSeed(s) : undefined)
3741
this.seedAccount.use()
3842
}
3943

@@ -72,6 +76,7 @@ export class App extends ReactiveComponent {
7276
bond={this.seed}
7377
reversible
7478
placeholder='Some seed for this key'
79+
validator={n => n || null}
7580
action={<Button content="Another" onClick={() => this.seed.trigger(generateMnemonic())} />}
7681
iconPosition='left'
7782
icon={<i style={{opacity: 1}} className='icon'><Identicon account={this.seedAccount} size={28} style={{marginTop: '5px'}}/></i>}
@@ -95,6 +100,61 @@ export class App extends ReactiveComponent {
95100
</div>
96101
</Segment>
97102
<Divider hidden />
103+
<Segment style={{margin: '1em'}} padded>
104+
<Header as='h2'>
105+
<Icon name='search' />
106+
<Header.Content>
107+
Inspect
108+
<Header.Subheader>Inspect the status of any account on the network</Header.Subheader>
109+
</Header.Content>
110+
</Header>
111+
<div style={{paddingBottom: '1em'}}>
112+
<div style={{fontSize: 'small'}}>lookup account</div>
113+
<AccountIdBond bond={this.lookup}/>
114+
<If condition={this.lookup.ready()} then={<div>
115+
<Label>Balance
116+
<Label.Detail>
117+
<Pretty value={runtime.balances.balance(this.lookup)}/>
118+
</Label.Detail>
119+
</Label>
120+
<Label>Nonce
121+
<Label.Detail>
122+
<Pretty value={runtime.system.accountNonce(this.lookup)}/>
123+
</Label.Detail>
124+
</Label>
125+
<StakingStatusLabel id={this.lookup}/>
126+
<If condition={runtime.balances.tryIndex(this.lookup, null).map(x => x !== null)} then={
127+
<Label>Short-form
128+
<Label.Detail>
129+
<Rspan>{runtime.balances.tryIndex(this.lookup).map(ss58Encode)}</Rspan>
130+
</Label.Detail>
131+
</Label>
132+
}/>
133+
<Label>Address
134+
<Label.Detail>
135+
<Pretty value={this.lookup}/>
136+
</Label.Detail>
137+
</Label>
138+
</div>}/>
139+
</div>
140+
<div style={{paddingBottom: '1em'}}>
141+
<div style={{fontSize: 'small'}}>name</div>
142+
<InputBond
143+
bond={this.nick}
144+
placeholder='A name for this address'
145+
validator={n => { console.log(n); return n ? addressBook().map(ss => ss.byName[n] ? null : n) : null}}
146+
action={<TransformBondButton
147+
content='Add'
148+
transform={(name, account) => { addressBook().submit(account, name); return true }}
149+
args={[this.nick, this.lookup]}
150+
/>}
151+
/>
152+
</div>
153+
<div style={{paddingBottom: '1em'}}>
154+
<AddressBookList/>
155+
</div>
156+
</Segment>
157+
<Divider hidden />
98158
<Segment style={{margin: '1em'}} padded>
99159
<Header as='h2'>
100160
<Icon name='send' />
@@ -138,7 +198,6 @@ export class App extends ReactiveComponent {
138198
content="Send"
139199
icon='send'
140200
tx={{
141-
longevity: true,
142201
sender: runtime.balances.tryIndex(this.source),
143202
call: calls.balances.transfer(runtime.balances.tryIndex(this.destination), this.amount)
144203
}}
@@ -173,7 +232,7 @@ export class App extends ReactiveComponent {
173232

174233
<div style={{paddingBottom: '1em'}}>
175234
<div style={{fontSize: 'small'}}>nominated account</div>
176-
<SignerBond bond={this.nomination}/>
235+
<AccountIdBond bond={this.nomination}/>
177236
<If condition={this.nomination.ready()} then={<span>
178237
<Label>Balance
179238
<Label.Detail>
@@ -225,10 +284,10 @@ export class App extends ReactiveComponent {
225284
icon="thumbs down"
226285
tx={{
227286
sender: runtime.balances.tryIndex(this.staker),
228-
call: calls.staking.unnominate(runtime.staking.nominationIndex(this.nomination, this.staker))
287+
call: calls.staking.unnominate(runtime.staking.nominationIndex(this.staker))
229288
}}
230289
negative
231-
enabled={runtime.staking.nominationIndex(this.nomination, this.staker).map(i => i !== -1).default(false)}
290+
enabled={runtime.staking.nominationIndex(this.staker).map(i => i !== -1).default(false)}
232291
/>
233292
</div>
234293
</Segment>

0 commit comments

Comments
 (0)