修改帐号创建和导入流程
This commit is contained in:
parent
4d92674c27
commit
6064f447e6
@ -1,62 +1,77 @@
|
|||||||
import { DEFALUT_TOKENS } from "../config/chain_config"
|
import { DEFALUT_TOKENS } from "../config/chain_config";
|
||||||
|
|
||||||
export interface IToken {
|
export interface IToken {
|
||||||
address: string
|
address: string;
|
||||||
type: 'eth'|'erc20'
|
type: "eth" | "erc20";
|
||||||
default: boolean
|
default: boolean;
|
||||||
symbol?: string
|
symbol?: string;
|
||||||
balance?: string
|
balance?: string;
|
||||||
decimal: number
|
decimal: number;
|
||||||
image?: string
|
image?: string;
|
||||||
last?: number
|
last?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface INFT {
|
export interface INFT {
|
||||||
address: string
|
address: string;
|
||||||
index: number
|
index: number;
|
||||||
tokenId?: string
|
tokenId?: string;
|
||||||
image?: string
|
image?: string;
|
||||||
name?: string
|
name?: string;
|
||||||
desc?: string
|
desc?: string;
|
||||||
last?: number
|
last?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function initNFT(address: string, index: number) {
|
export function initNFT(address: string, index: number) {
|
||||||
return {
|
return {
|
||||||
address,
|
address,
|
||||||
index
|
index,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ITokenData {
|
export interface ITokenData {
|
||||||
tokens: IToken[]
|
tokens: IToken[];
|
||||||
heros: INFT[]
|
heros: INFT[];
|
||||||
weapons: INFT[]
|
weapons: INFT[];
|
||||||
chips: INFT[]
|
chips: INFT[];
|
||||||
}
|
}
|
||||||
export interface IAccount {
|
export interface IAccount {
|
||||||
address: string
|
address: string;
|
||||||
nickname?: string
|
type: number;
|
||||||
avatar?: string
|
index: number;
|
||||||
|
nickname?: string;
|
||||||
|
avatar?: string;
|
||||||
tokenData: {
|
tokenData: {
|
||||||
[key: number]: ITokenData
|
[key: number]: ITokenData;
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function initAccount(address: string, chain: number, nickname: string): IAccount {
|
export function initAccount({
|
||||||
let chainData = {}
|
address,
|
||||||
|
chain,
|
||||||
|
nickname,
|
||||||
|
type,
|
||||||
|
index
|
||||||
|
}: {
|
||||||
|
address: string;
|
||||||
|
chain: number;
|
||||||
|
nickname: string;
|
||||||
|
type: number,
|
||||||
|
index: number
|
||||||
|
}): IAccount {
|
||||||
|
let chainData = {};
|
||||||
let data: IAccount = {
|
let data: IAccount = {
|
||||||
address,
|
address,
|
||||||
nickname,
|
nickname,
|
||||||
tokenData: chainData
|
type,
|
||||||
}
|
index,
|
||||||
let tokens = DEFALUT_TOKENS[chain]
|
tokenData: chainData,
|
||||||
|
};
|
||||||
|
let tokens = DEFALUT_TOKENS[chain];
|
||||||
chainData[chain] = {
|
chainData[chain] = {
|
||||||
tokens,
|
tokens,
|
||||||
heros: [],
|
heros: [],
|
||||||
weapons: [],
|
weapons: [],
|
||||||
chips: []
|
chips: [],
|
||||||
}
|
};
|
||||||
//TODO: add default tokens
|
return data;
|
||||||
return data
|
|
||||||
}
|
}
|
||||||
|
102
src/index.ts
102
src/index.ts
@ -63,17 +63,14 @@ export default class JCWallet {
|
|||||||
this.erc20Standard = new ERC20Standard(this.web3);
|
this.erc20Standard = new ERC20Standard(this.web3);
|
||||||
this.erc721Standard = new ERC721Standard(this.web3);
|
this.erc721Standard = new ERC721Standard(this.web3);
|
||||||
this.wallet = this.web3.eth.accounts.wallet.load(this.password, WALLET_STORAGE_KEY_NAME)
|
this.wallet = this.web3.eth.accounts.wallet.load(this.password, WALLET_STORAGE_KEY_NAME)
|
||||||
if (!this.wallet || this.wallet.length === 0) {
|
|
||||||
let key = '0xa6c4354fb93a55fb67117969a12465209395ec31089fea9e6e061f873b87a473'
|
|
||||||
this.wallet.add(key);
|
|
||||||
this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME);
|
|
||||||
}
|
|
||||||
this.data = this.dataManage.loadData()
|
this.data = this.dataManage.loadData()
|
||||||
|
|
||||||
window.jc = { wallet: this };
|
window.jc = { wallet: this };
|
||||||
window.cc = window.cc || {};
|
window.cc = window.cc || {};
|
||||||
window.cc.walletCallback = (dataStr: string) => {
|
window.cc.walletCallback = (dataStr: string) => {
|
||||||
console.log('[Native CB]::' + dataStr)
|
console.log('[Native CB]::' + dataStr)
|
||||||
}
|
}
|
||||||
|
this.init({chains: [322, 97]})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -90,6 +87,12 @@ export default class JCWallet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!this.wallet || this.wallet.length === 0) {
|
||||||
|
// let key = '0xa6c4354fb93a55fb67117969a12465209395ec31089fea9e6e061f873b87a473'
|
||||||
|
// this.wallet.add(key);
|
||||||
|
// this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME);
|
||||||
|
this.createAccount();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get currentChain() {
|
get currentChain() {
|
||||||
@ -139,20 +142,19 @@ export default class JCWallet {
|
|||||||
const chain = this.currentChain.id
|
const chain = this.currentChain.id
|
||||||
let data = this.data.find(o => o.address === address)
|
let data = this.data.find(o => o.address === address)
|
||||||
if (!data) {
|
if (!data) {
|
||||||
let accountName = `Account${this.wallet.length}`
|
throw new Error('account data not found');
|
||||||
data = initAccount(address, chain, accountName)
|
}
|
||||||
this.data.push(data)
|
|
||||||
} else {
|
if (!data.tokenData[chain]) {
|
||||||
if (!data.tokenData[chain]) {
|
let tokens = DEFALUT_TOKENS[chain]
|
||||||
let tokens = DEFALUT_TOKENS[chain]
|
data.tokenData[chain] = {
|
||||||
data.tokenData[chain] = {
|
tokens,
|
||||||
tokens,
|
heros: [],
|
||||||
heros: [],
|
weapons: [],
|
||||||
weapons: [],
|
chips: []
|
||||||
chips: []
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.dataManage.saveData(this.data)
|
this.dataManage.saveData(this.data)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
@ -165,10 +167,64 @@ export default class JCWallet {
|
|||||||
let account = this.web3.eth.accounts.create()
|
let account = this.web3.eth.accounts.create()
|
||||||
this.wallet.add(account)
|
this.wallet.add(account)
|
||||||
this.wallet.save(this.password, WALLET_STORAGE_KEY_NAME)
|
this.wallet.save(this.password, WALLET_STORAGE_KEY_NAME)
|
||||||
|
const chain = this.currentChain.id
|
||||||
|
let data = this.data.find(o => o.address === account.address)
|
||||||
|
if (!data) {
|
||||||
|
const index = this.getMaxIdexOfType(0);
|
||||||
|
const nickname = `Account ${ index + 1 }`
|
||||||
|
data = initAccount({
|
||||||
|
address: account.address,
|
||||||
|
chain,
|
||||||
|
nickname,
|
||||||
|
type: 0,
|
||||||
|
index
|
||||||
|
})
|
||||||
|
this.data.push(data);
|
||||||
|
this.dataManage.saveData(this.data)
|
||||||
|
}
|
||||||
|
|
||||||
this.accountIndex = this.wallet.length - 1
|
this.accountIndex = this.wallet.length - 1
|
||||||
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address)
|
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public importAccount(privateKey: string) {
|
||||||
|
let account = this.web3.eth.accounts.privateKeyToAccount(privateKey)
|
||||||
|
if (this.wallet[account.address]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
this.wallet.add(account);
|
||||||
|
const chain = this.currentChain.id
|
||||||
|
let data = this.data.find(o => o.address === account.address)
|
||||||
|
if (!data) {
|
||||||
|
const index = this.getMaxIdexOfType(1);
|
||||||
|
const nickname = `Imported ${ index + 1 }`
|
||||||
|
data = initAccount({
|
||||||
|
address: account.address,
|
||||||
|
chain,
|
||||||
|
nickname,
|
||||||
|
type: 1,
|
||||||
|
index
|
||||||
|
})
|
||||||
|
this.data.push(data);
|
||||||
|
this.dataManage.saveData(this.data)
|
||||||
|
}
|
||||||
|
this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME);
|
||||||
|
this.accountIndex = this.wallet.length - 1
|
||||||
|
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
private getMaxIdexOfType(type: number) {
|
||||||
|
let maxIdx = -1;
|
||||||
|
for (let i = 0, l = this.data.length; i < l; i++) {
|
||||||
|
if (this.data[i].type !== type) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
maxIdx = Math.max(this.data[i].index, maxIdx);
|
||||||
|
}
|
||||||
|
return maxIdx + 1
|
||||||
|
}
|
||||||
|
|
||||||
public selectAccount(address: string) {
|
public selectAccount(address: string) {
|
||||||
let index = 0
|
let index = 0
|
||||||
for (let i = 0, l = this.wallet.length; i < l ; i ++) {
|
for (let i = 0, l = this.wallet.length; i < l ; i ++) {
|
||||||
@ -183,18 +239,6 @@ export default class JCWallet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public importAccount(privateKey: string) {
|
|
||||||
let account = this.web3.eth.accounts.privateKeyToAccount(privateKey)
|
|
||||||
if (this.wallet[account.address]) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
this.wallet.add(account);
|
|
||||||
this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME);
|
|
||||||
this.accountIndex = this.wallet.length - 1
|
|
||||||
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
public async sendEth(to: string, amount: number | string) {
|
public async sendEth(to: string, amount: number | string) {
|
||||||
let from = this.currentAccount().address;
|
let from = this.currentAccount().address;
|
||||||
const amountToSend = this.web3.utils.toWei(amount+'', "ether");
|
const amountToSend = this.web3.utils.toWei(amount+'', "ether");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user