Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local Dev Support #20

Merged
merged 8 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,22 @@ Golang
}
fmt.Println(sign.Verify(rawMessage))
```


## For Local Dapp Development

You can use the extension to communicate with local chains on your server.

Go to `Settings` -> `About IOST Wallet` -> `Developer Mode`, and enable Developer Mode.

With the Developer Mode enabled, you are able to import accounts on local chains.

If you are importing local accounts, enter the account name you want to import, then provide endpoint and chainID information as well.
Those information will be tied to the account info and used when the account tries to communicate with the chain.

This feature is still experimental. Please report any bugs or unexpected behavoiurs.





15 changes: 13 additions & 2 deletions public/app/iostProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,24 @@ const IWalletJS = {

window.postMessage({action: 'GET_ACCOUNT'}, '*')

const mapHostToNetwork = (url) => {
if (url.includes('//test.api.iost.io')) {
return "TESTNET"
}

if (url.includes('//api.iost.io')) {
return "MAINNET"
}

return "LOCALNET"
}

function signAndSend(tx){
const domain = document.domain
const actionId = uuidv4()
const cb = new Callback()
const action = tx.actions[0]
const network = this.currentRPC._provider._host.indexOf('//api.iost.io') > -1?'MAINNET':'TESTNET'
const network = mapHostToNetwork(this.currentRPC._provider._host)
const message = {
action: ACTION.TX_ASK,
actionId: actionId,
Expand Down Expand Up @@ -166,7 +177,7 @@ function signMessage(message) {
const domain = document.domain
const actionId = uuidv4()

const network = this.currentRPC._provider._host.indexOf('//api.iost.io') > -1 ? 'MAINNET' : 'TESTNET'
const network = mapHostToNetwork(this.currentRPC._provider._host)
const windowMessage = {
action: ACTION.TX_ASK,
actionId: actionId,
Expand Down
7 changes: 5 additions & 2 deletions public/app/scripts/controllers/TxController.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,15 @@ TxController.prototype.processTx = async function(txIdx, isAddWhitelist, iGASPri
const acc = accounts.find(item => item.name == account.name && item.network == network)
if(acc){
const encodedPrivateKey = aesDecrypt(acc.privateKey, this.state.password)
await iostController.changeNetwork(network == 'MAINNET'?IOST_NODE_URL: IOST_TEST_NODE_URL)
const url = network === 'LOCALNET' ? acc.endpoint : network === 'MAINNET'?IOST_NODE_URL: IOST_TEST_NODE_URL
await iostController.changeNetwork(url)
iostController.loginAccount(account.name, encodedPrivateKey)
const tx = new iostController.pack.Tx()
Object.keys(_tx).map(key => tx[key] = _tx[key])
if(network != 'MAINNET'){
if (network === 'TESTNET') {
tx.setChainID(1023)
} else if (network === 'LOCALNET') {
tx.setChainID(acc.chainID)
}

if (iGASPrice) {
Expand Down
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class App extends Component<Props> {
const { type, name, privateKey } = account
const password = await user.getLockPassword()
const encodedPrivateKey = utils.aesDecrypt(privateKey, password)
iost.changeNetwork(utils.getNetWork(account.network))
iost.changeNetwork(utils.getNetWork(account))
// this.changeLocation('/gasManage')

iost.changeAccount(account)
Expand Down
4 changes: 2 additions & 2 deletions src/components/Account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Account extends Component<Props> {
const { type, name, privateKey } = account
const password = await user.getLockPassword()
if(password){
iost.changeNetwork(utils.getNetWork(account.network))
iost.changeNetwork(utils.getNetWork(account))
// const encodedPrivateKey = utils.aesDecrypt(privateKey, password)
// iost.loginAccount(name, encodedPrivateKey)

Expand Down Expand Up @@ -95,7 +95,7 @@ class Account extends Component<Props> {
try {
const ipassword = await user.getLockPassword()
if(ipassword){
iost.changeNetwork(utils.getNetWork(account.network))
iost.changeNetwork(utils.getNetWork(account))
// const encodedPrivateKey = utils.aesDecrypt(privateKey, ipassword)
// iost.changeNetwork(utils.getNetWork(account.network))
// iost.loginAccount(name, encodedPrivateKey)
Expand Down
35 changes: 24 additions & 11 deletions src/components/AccountImport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import LoadingImage from 'components/LoadingImage'
import NetworkSelector from 'components/NetworkSelector'
import iost from 'iostJS/iost'
import { privateKeyToPublicKey, publickKeyToAccount, nameAndPublicKeyToAccount } from 'utils/key'
import { localnetDefault } from 'constants/localnet'
import utils from 'utils'
import ui from 'utils/ui';
import user from 'utils/user';
Expand Down Expand Up @@ -36,6 +37,8 @@ class AccountImport extends Component<Props> {
state = {
privateKey: '',
accountName: '',
endpoint: '',
chainID: '',
isLoading: false,
errorMessage: '',
developerMode: undefined,
Expand Down Expand Up @@ -66,9 +69,8 @@ class AccountImport extends Component<Props> {

getDeveloperStatus = async () => {
const developerMode = await utils.getStorage('developerMode');
this.setState({
developerMode
});

this.setState(developerMode ? { developerMode, ...localnetDefault } : { developerMode });
}

onSubmit = async () => {
Expand All @@ -83,9 +85,12 @@ class AccountImport extends Component<Props> {
try {
publicKey = privateKeyToPublicKey(privateKey)
if(accountName) {
let account = await nameAndPublicKeyToAccount(accountName, publicKey, 'LOCALNET')
let account = await nameAndPublicKeyToAccount(accountName, publicKey, this.state.endpoint)
const password = await user.getLockPassword()
accounts = [{ name: account.name,
accounts = [{
name: account.name,
endpoint: this.state.endpoint,
chainID: this.state.chainID,
network: 'LOCALNET',
privateKey: utils.aesEncrypt(privateKey, password),
publicKey: publicKey,
Expand Down Expand Up @@ -141,8 +146,7 @@ class AccountImport extends Component<Props> {
if(activeAccount){
changeLocation('/accountManage')
}else {
iost.changeNetwork(utils.getNetWork(accounts[0].network))

iost.changeNetwork(utils.getNetWork(account[0]))
iost.rpc.blockchain.getAccountInfo(accounts[0].name)
.then((accountInfo) => {
if (!iost.isValidAccount(accountInfo, accounts[0].publicKey)) {
Expand Down Expand Up @@ -198,11 +202,20 @@ class AccountImport extends Component<Props> {
<Header title={I18n.t('firstLogin_ImportAccount')} logo={!canBack} onBack={this.backTo} hasSetting={false} />
<div className="accountImport-box">
<textarea name="privateKey" id="" className="privateKey-content" onChange={this.handleChange} placeholder={I18n.t('ImportAccount_EnterPrivate')}/>
{this.state.developerMode === true ? (
<Fragment><p className="accountNameText">{I18n.t('ImportAccount_LocalNetMessage')}</p>
<input name="accountName" id="" className="input-accountName" onChange={this.handleChange} placeholder={I18n.t('ImportAccount_EnterName')}/>
{this.state.developerMode && (
<Fragment>
<p className="accountNameText">{I18n.t('ImportAccount_LocalNetMessage')}</p>
<input name="accountName" id="" className="input-accountName" onChange={this.handleChange} placeholder={I18n.t('ImportAccount_EnterName')}/>
{this.state.accountName &&
<Fragment>
<p className="localnetInfoText">{I18n.t('ImportAccount_LocalNetEndpointMessage')}</p>
<input name="endpoint" id="" value={this.state.endpoint} className="input-localnetInfo" onChange={this.handleChange} placeholder={I18n.t('ImportAccount_EnterEndpoint')}/>
<p className="localnetInfoText">{I18n.t('ImportAccount_LocalNetChainIDMessage')}</p>
<input name="chainID" id="" min="0" type="number" value={this.state.chainID} className="input-localnetInfo" onChange={this.handleChange} placeholder={I18n.t('ImportAccount_EnterChainID')}/>
</Fragment>
}
</Fragment>
) : (null)}
)}
<Button className="btn-submit" onClick={this.onSubmit}>{isLoading ? <LoadingImage /> : I18n.t('ImportAccount_Submit')}</Button>
</div>
</Fragment>
Expand Down
24 changes: 22 additions & 2 deletions src/components/AccountImport/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
height: 540px;
background-color: $gray;
text-align: center;

.privateKey-content {
margin-top: 30px;
width: 300px;
Expand All @@ -15,6 +16,7 @@
resize: none;
padding: 5px;
}

.input-accountName {
margin-top: 30px;
width: 300px;
Expand All @@ -33,13 +35,31 @@
margin:10px auto 0px auto;
color:#999;
}

.input-localnetInfo {
margin-top: 15px;
width: 300px;
height: 30px;
border: 1px solid $border;
border-radius: 4px;
resize: none;
padding: 5px;
}

.localnetInfoText {
width: 300px;
resize: none;
display: block;
padding: 5px 5px 0px 5px;
margin: 10px auto 0px auto;
color: #999;
}

.btn-submit {
position: absolute;
bottom: 30px;
left: 0;
right: 0;
margin: 0 auto;
}


}
2 changes: 1 addition & 1 deletion src/components/AccountManage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class AccountManage extends Component<Props> {
const account = accounts[0]
// reset current account

iost.changeNetwork(utils.getNetWork(account.network))
iost.changeNetwork(utils.getNetWork(account))
// iost.loginAccount(account.name, account.publicKey)
iost.changeAccount(account)
user.setActiveAccount(account)
Expand Down
2 changes: 1 addition & 1 deletion src/components/AssetManage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class AssetManage extends Component<Props> {
const account = await user.getActiveAccount()
const _user = `${account.name}-${account.network}`
// 未找到该币,会报错
const data = await getTokenInfo(token, account.network)
const data = await getTokenInfo(token, account)
const assets = await utils.getStorage('assets', {})
const asset = { symbol: token, fullName: data.full_name, onlyIssuerCanTransfer:data.only_issuer_can_transfer, issuer:data.issuer }
assets[_user] = [...(assets[_user] || []), asset]
Expand Down
6 changes: 4 additions & 2 deletions src/components/BuyRam.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component, Fragment } from 'react'
import { I18n } from 'react-redux-i18n'

import ui from 'utils/ui'
import user from 'utils/user'
import Input from 'components/Input'
import Button from 'components/Button'
import LoadingImage from 'components/LoadingImage'
Expand Down Expand Up @@ -36,11 +37,12 @@ class BuyRam extends Component<Props> {
})
}

buyRam = () => {
buyRam = async () => {
const { amount, forWho } = this.state
const ID = iost.account.getID()
const activeAccount = await user.getActiveAccount()

iost.sendTransaction('ram.iost', 'buy', [ID, forWho || ID, Number(amount)])
iost.sendTransaction('ram.iost', 'buy', [ID, forWho || ID, Number(amount)], activeAccount.network === 'LOCALNET' ? activeAccount.chainID : null)
.onPending(() => {
this.setState({
isLoading: true,
Expand Down
9 changes: 6 additions & 3 deletions src/components/GasManage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { privateKeyToPublicKey } from 'utils/key'
import utils from 'utils'

import ui from "utils/ui";
import user from "utils/user";
import './index.scss'
import classnames from "classnames";

Expand Down Expand Up @@ -103,11 +104,13 @@ class GasManage extends Component<Props> {
}


onSubmit = () => {
onSubmit = async () => {
const { isStake, buyAmount, resourceAddress, sellAmount } = this.state
const account = iost.account.getID()
const activeAccount = await user.getActiveAccount()

if(isStake){
iost.signAndSend('gas.iost', 'pledge', [account, resourceAddress || account, buyAmount])
iost.signAndSend('gas.iost', 'pledge', [account, resourceAddress || account, buyAmount], null, activeAccount.network === 'LOCALNET' ? activeAccount.chainID : null)
.on('pending', () => {
this.setState({
isLoading: true,
Expand All @@ -124,7 +127,7 @@ class GasManage extends Component<Props> {
this.moveTo('/tokenTransferFailed')()
})
}else{
iost.signAndSend('gas.iost', 'unpledge', [account, resourceAddress || account, sellAmount])
iost.signAndSend('gas.iost', 'unpledge', [account, resourceAddress || account, sellAmount], null, activeAccount.network === 'LOCALNET' ? activeAccount.chainID : null)
.on('pending', () => {
this.setState({
isLoading: true,
Expand Down
6 changes: 4 additions & 2 deletions src/components/LendRam.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import LoadingImage from 'components/LoadingImage'
import TransactionSuccess from 'components/TransactionSuccess'
import TransactionFailed from 'components/TransactionFailed'
import ui from 'utils/ui'
import user from 'utils/user'
import iost from 'iostJS/iost'

import './LendRam.scss'
Expand All @@ -28,11 +29,12 @@ class LendRam extends Component<Props> {
})
}

lendRam = () => {
lendRam = async () => {
const { amount, forWho } = this.state
const ID = iost.account.getID()
const activeAccount = await user.getActiveAccount()

iost.sendTransaction('ram.iost', 'lend', [ID, forWho, Number(amount)])
iost.sendTransaction('ram.iost', 'lend', [ID, forWho, Number(amount)], activeAccount.network === 'LOCALNET' ? activeAccount.chainID : null)
.onPending(() => {
this.setState({
isLoading: true,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Login extends Component<Props> {
return
}

iost.changeNetwork(utils.getNetWork(account.network))
iost.changeNetwork(utils.getNetWork(account))
iost.rpc.blockchain.getAccountInfo(account)
.then((accountInfo) => {
if (!iost.isValidAccount(accountInfo, publicKey)) {
Expand Down
6 changes: 4 additions & 2 deletions src/components/PledgeGas.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import LoadingImage from 'components/LoadingImage'
import TransactionSuccess from 'components/TransactionSuccess'
import TransactionFailed from 'components/TransactionFailed'
import ui from 'utils/ui'
import user from 'utils/user'
import iost from 'iostJS/iost'

import './PledgeGas.scss'
Expand Down Expand Up @@ -35,11 +36,12 @@ class PledgeGas extends Component<Props> {
})
}

pledgeToken = () => {
pledgeToken = async () => {
const { amount, forWho } = this.state
const ID = iost.account.getID()
const activeAccount = await user.getActiveAccount()

iost.sendTransaction('gas.iost', 'pledge', [ID, forWho || ID, amount])
iost.sendTransaction('gas.iost', 'pledge', [ID, forWho || ID, amount], activeAccount.network === 'LOCALNET' ? activeAccount.chainID : null )
.onPending(() => {
this.setState({
isLoading: true,
Expand Down
9 changes: 6 additions & 3 deletions src/components/RamManage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { privateKeyToPublicKey } from 'utils/key'
import utils from 'utils'

import ui from "utils/ui";
import user from "utils/user";
import './index.scss'

type Props = {
Expand Down Expand Up @@ -112,15 +113,17 @@ class RamManage extends Component<Props> {
})
}

onSubmit = () => {
onSubmit = async () => {
const { isBuy, buyAmount,sellAmount, resourceAddress, ramMarketInfo } = this.state
const account = iost.account.getID()
const activeAccount = await user.getActiveAccount()

if(isBuy){
const _buyAmount = parseInt(buyAmount * 1024)
const ramPrice = (ramMarketInfo.buy_price*1024).toFixed(4)
const amountLimit = ['iost', (+ramPrice+1) * (+buyAmount)]

iost.signAndSend('ram.iost', 'buy', [account, resourceAddress || account, _buyAmount], amountLimit)
iost.signAndSend('ram.iost', 'buy', [account, resourceAddress || account, _buyAmount], amountLimit, activeAccount.network === 'LOCALNET' ? activeAccount.chainID : null)
.on('pending', () => {
this.setState({
isLoading: true,
Expand All @@ -142,7 +145,7 @@ class RamManage extends Component<Props> {
const amountLimit = ['ram', _sellAmount]
// return console.log(amountLimit)

iost.signAndSend('ram.iost', 'sell', [account, account, _sellAmount], amountLimit)
iost.signAndSend('ram.iost', 'sell', [account, account, _sellAmount], amountLimit, activeAccount.network === 'LOCALNET' ? activeAccount.chainID : null)
.on('pending', () => {
this.setState({
isLoading: true,
Expand Down
Loading