This repository was archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Public node with accounts and signing in Frontend #5304
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2015-2017 Parity Technologies (UK) Ltd. | ||
// This file is part of Parity. | ||
|
||
// Parity is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Parity is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Parity. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import { keythereum } from '../ethkey'; | ||
|
||
export default class Account { | ||
constructor (persist, data) { | ||
const { | ||
keyObject, | ||
meta = {}, | ||
name = '' | ||
} = data; | ||
|
||
this._persist = persist; | ||
this._keyObject = keyObject; | ||
this._name = name; | ||
this._meta = meta; | ||
} | ||
|
||
isValidPassword (password) { | ||
try { | ||
keythereum.recover(Buffer.from(password), this._keyObject); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
get address () { | ||
return `0x${this._keyObject.address.toLowerCase()}`; | ||
} | ||
|
||
get name () { | ||
return this._name; | ||
} | ||
|
||
set name (name) { | ||
this._name = name; | ||
|
||
this._persist(); | ||
} | ||
|
||
get meta () { | ||
return JSON.stringify(this._meta); | ||
} | ||
|
||
set meta (meta) { | ||
this._meta = JSON.parse(meta); | ||
|
||
this._persist(); | ||
} | ||
|
||
get uuid () { | ||
return this._keyObject.id; | ||
} | ||
|
||
decryptPrivateKey (password) { | ||
return keythereum.recover(Buffer.from(password), this._keyObject); | ||
} | ||
|
||
static fromPrivateKey (persist, key, password) { | ||
const iv = keythereum.crypto.randomBytes(16); | ||
const salt = keythereum.crypto.randomBytes(32); | ||
|
||
// Keythereum will fail if `password` is an empty string | ||
password = Buffer.from(password); | ||
|
||
const keyObject = keythereum.dump(password, key, salt, iv); | ||
|
||
const account = new Account(persist, { keyObject }); | ||
|
||
return account; | ||
} | ||
|
||
toJSON () { | ||
return { | ||
keyObject: this._keyObject, | ||
name: this._name, | ||
meta: this._meta | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2015-2017 Parity Technologies (UK) Ltd. | ||
// This file is part of Parity. | ||
|
||
// Parity is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Parity is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Parity. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import Account from './account'; | ||
import localStore from 'store'; | ||
import { debounce } from 'lodash'; | ||
|
||
const NULL_ADDRESS = '0x0000000000000000000000000000000000000000'; | ||
const LS_STORE_KEY = '_parity::localAccounts'; | ||
|
||
export default class Accounts { | ||
constructor (data = localStore.get(LS_STORE_KEY) || {}) { | ||
const { | ||
last = NULL_ADDRESS, | ||
store = [] | ||
} = data; | ||
|
||
this.persist = debounce(() => { | ||
localStore.set(LS_STORE_KEY, this); | ||
}, 100); | ||
|
||
this._last = last; | ||
this._store = store.map((data) => new Account(this.persist, data)); | ||
} | ||
|
||
create (secret, password) { | ||
const privateKey = Buffer.from(secret.slice(2), 'hex'); | ||
const account = Account.fromPrivateKey(this.persist, privateKey, password); | ||
|
||
this._store.push(account); | ||
this.lastAddress = account.address; | ||
|
||
this.persist(); | ||
|
||
return account.address; | ||
} | ||
|
||
set lastAddress (value) { | ||
this._last = value.toLowerCase(); | ||
} | ||
|
||
get lastAddress () { | ||
return this._last; | ||
} | ||
|
||
get (address) { | ||
address = address.toLowerCase(); | ||
|
||
this.lastAddress = address; | ||
|
||
const account = this._store.find((account) => account.address === address); | ||
|
||
if (!account) { | ||
throw new Error(`Account not found: ${address}`); | ||
} | ||
|
||
return account; | ||
} | ||
|
||
remove (address, password) { | ||
address = address.toLowerCase(); | ||
|
||
const index = this._store.findIndex((account) => account.address === address); | ||
|
||
if (index === -1) { | ||
return false; | ||
} | ||
|
||
const account = this._store[index]; | ||
|
||
if (!account.isValidPassword(password)) { | ||
console.log('invalid password'); | ||
return false; | ||
} | ||
|
||
if (address === this.lastAddress) { | ||
this.lastAddress = NULL_ADDRESS; | ||
} | ||
|
||
this._store.splice(index, 1); | ||
|
||
this.persist(); | ||
|
||
return true; | ||
} | ||
|
||
mapArray (mapper) { | ||
return this._store.map(mapper); | ||
} | ||
|
||
mapObject (mapper) { | ||
const result = {}; | ||
|
||
this._store.forEach((account) => { | ||
result[account.address] = mapper(account); | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
toJSON () { | ||
return { | ||
last: this._last, | ||
store: this._store | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2015-2017 Parity Technologies (UK) Ltd. | ||
// This file is part of Parity. | ||
|
||
// Parity is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Parity is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Parity. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import Accounts from './accounts'; | ||
|
||
const accounts = new Accounts(); | ||
|
||
export default accounts; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if
nodeKind
is changed during lifetime of the UI (e.g. Parity being restarted without closing tab window)?