将jcwallet挂载到全局对象, 修正一些方法引用错误
This commit is contained in:
parent
2b4235449e
commit
aeef85a4fb
15
src/index.ts
15
src/index.ts
@ -8,10 +8,10 @@ import { createWalletEvents, WALLET_ACCOUNT_CHANGE, WALLET_CHAIN_CHANGE, WALLET_
|
|||||||
import { ERC20Standard } from "./standards/ERC20Standard";
|
import { ERC20Standard } from "./standards/ERC20Standard";
|
||||||
import { ERC721Standard } from "./standards/ERC721Standard";
|
import { ERC721Standard } from "./standards/ERC721Standard";
|
||||||
import { IAccount, initAccount } from "./data/DataModel";
|
import { IAccount, initAccount } from "./data/DataModel";
|
||||||
import { DataManage } from "./manage/DataManage";
|
import { loadData, saveData } from "./manage/DataManage";
|
||||||
import { WALLET_STORAGE_KEY_NAME } from "./config/constants";
|
import { WALLET_STORAGE_KEY_NAME } from "./config/constants";
|
||||||
import { DEFALUT_TOKENS } from "./config/chain_config";
|
import { DEFALUT_TOKENS } from "./config/chain_config";
|
||||||
import { newMnemonic, restoreWalletByMnemonic } from "./manage/WalletManage";
|
import { newAccount, newMnemonic, restoreWalletByMnemonic } from "./manage/WalletManage";
|
||||||
|
|
||||||
|
|
||||||
var global =
|
var global =
|
||||||
@ -53,7 +53,6 @@ export default class JCWallet {
|
|||||||
public erc20Standard: ERC20Standard
|
public erc20Standard: ERC20Standard
|
||||||
public erc721Standard: ERC721Standard
|
public erc721Standard: ERC721Standard
|
||||||
public mainHandlers = createWalletEvents()
|
public mainHandlers = createWalletEvents()
|
||||||
private dataManage = new DataManage()
|
|
||||||
public data: IAccount[] = []
|
public data: IAccount[] = []
|
||||||
public iconType = 'jazz'
|
public iconType = 'jazz'
|
||||||
private accountIndex = 0
|
private accountIndex = 0
|
||||||
@ -64,7 +63,7 @@ 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)
|
||||||
this.data = this.dataManage.loadData()
|
this.data = loadData()
|
||||||
|
|
||||||
window.jc = { wallet: this };
|
window.jc = { wallet: this };
|
||||||
window.cc = window.cc || {};
|
window.cc = window.cc || {};
|
||||||
@ -165,7 +164,7 @@ export default class JCWallet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.dataManage.saveData(this.data)
|
saveData(this.data)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,7 +175,7 @@ export default class JCWallet {
|
|||||||
public createAccount() {
|
public createAccount() {
|
||||||
// let account = this.web3.eth.accounts.create()
|
// let account = this.web3.eth.accounts.create()
|
||||||
const index = this.getMaxIdexOfType(0);
|
const index = this.getMaxIdexOfType(0);
|
||||||
const accountNew = this.dataManage.newAccount(this.password, index)
|
const accountNew = newAccount(this.password, index)
|
||||||
const account = this.wallet.add(accountNew)
|
const account = this.wallet.add(accountNew)
|
||||||
this.wallet.save(this.password, WALLET_STORAGE_KEY_NAME)
|
this.wallet.save(this.password, WALLET_STORAGE_KEY_NAME)
|
||||||
const chain = this.currentChain.id
|
const chain = this.currentChain.id
|
||||||
@ -191,7 +190,7 @@ export default class JCWallet {
|
|||||||
index
|
index
|
||||||
})
|
})
|
||||||
this.data.push(data);
|
this.data.push(data);
|
||||||
this.dataManage.saveData(this.data)
|
saveData(this.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.accountIndex = this.wallet.length - 1
|
this.accountIndex = this.wallet.length - 1
|
||||||
@ -213,7 +212,7 @@ export default class JCWallet {
|
|||||||
index
|
index
|
||||||
})
|
})
|
||||||
this.data.push(data);
|
this.data.push(data);
|
||||||
this.dataManage.saveData(this.data)
|
saveData(this.data)
|
||||||
}
|
}
|
||||||
this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME);
|
this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME);
|
||||||
this.accountIndex = this.wallet.length - 1
|
this.accountIndex = this.wallet.length - 1
|
||||||
|
@ -1,43 +1,39 @@
|
|||||||
import { IAccount } from "../data/DataModel";
|
import { IAccount } from "../data/DataModel";
|
||||||
import { singleton } from "../decorator/singleton.decorator";
|
|
||||||
import { aesDecrypt, aesEncrypt } from "../util/crypto.util";
|
import { aesDecrypt, aesEncrypt } from "../util/crypto.util";
|
||||||
|
|
||||||
const LOCAL_ACCOUNT_DATAS = 'local_account_datas'
|
const LOCAL_ACCOUNT_DATAS = 'local_account_datas'
|
||||||
const LOCAL_WALLET_MNEMONIC = 'local_wallet_mnemonic'
|
const LOCAL_WALLET_MNEMONIC = 'local_wallet_mnemonic'
|
||||||
@singleton
|
|
||||||
export class DataManage{
|
|
||||||
public loadData(){
|
export function loadData(){
|
||||||
const dataStr = localStorage.getItem(LOCAL_ACCOUNT_DATAS)
|
const dataStr = localStorage.getItem(LOCAL_ACCOUNT_DATAS)
|
||||||
let result: IAccount[] = []
|
let result: IAccount[] = []
|
||||||
if (dataStr) {
|
if (dataStr) {
|
||||||
try {
|
try {
|
||||||
result = JSON.parse(dataStr)
|
result = JSON.parse(dataStr)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('load local data error')
|
console.log('load local data error')
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
return result
|
||||||
public saveData(datas: IAccount[]) {
|
}
|
||||||
const dataStr = JSON.stringify(datas)
|
|
||||||
localStorage.setItem(LOCAL_ACCOUNT_DATAS, dataStr)
|
export function saveData(datas: IAccount[]) {
|
||||||
}
|
const dataStr = JSON.stringify(datas)
|
||||||
|
localStorage.setItem(LOCAL_ACCOUNT_DATAS, dataStr)
|
||||||
public loadMnemonic(password: string) {
|
}
|
||||||
let dataStr: string = localStorage.getItem(LOCAL_WALLET_MNEMONIC)
|
|
||||||
if (dataStr) {
|
export function loadMnemonic(password: string) {
|
||||||
dataStr = aesDecrypt(dataStr, password)
|
let dataStr: string = localStorage.getItem(LOCAL_WALLET_MNEMONIC)
|
||||||
}
|
if (dataStr) {
|
||||||
return dataStr
|
dataStr = aesDecrypt(dataStr, password)
|
||||||
}
|
}
|
||||||
|
return dataStr
|
||||||
|
}
|
||||||
|
|
||||||
public saveMnemonic(mnemonic: string, password: string) {
|
|
||||||
const dataStr = aesEncrypt(mnemonic, password);
|
|
||||||
localStorage.setItem(LOCAL_WALLET_MNEMONIC, dataStr);
|
export function saveMnemonic(mnemonic: string, password: string) {
|
||||||
}
|
const dataStr = aesEncrypt(mnemonic, password);
|
||||||
|
localStorage.setItem(LOCAL_WALLET_MNEMONIC, dataStr);
|
||||||
|
|
||||||
}
|
}
|
@ -1,9 +1,10 @@
|
|||||||
import { hdkey } from 'ethereumjs-wallet'
|
import { hdkey } from 'ethereumjs-wallet'
|
||||||
import { generateMnemonic, mnemonicToSeedSync } from "bip39";
|
import { generateMnemonic, mnemonicToSeedSync } from "bip39";
|
||||||
|
import { loadMnemonic, saveMnemonic } from './DataManage';
|
||||||
|
|
||||||
|
|
||||||
export function newAccount(password: string, index: number) {
|
export function newAccount(password: string, index: number) {
|
||||||
const mnemonic = this.loadMnemonic(password)
|
const mnemonic = loadMnemonic(password)
|
||||||
const seed = mnemonicToSeedSync(mnemonic)
|
const seed = mnemonicToSeedSync(mnemonic)
|
||||||
const hdWallet = hdkey.fromMasterSeed(seed)
|
const hdWallet = hdkey.fromMasterSeed(seed)
|
||||||
const keyPair1 = hdWallet.derivePath(`m/44'/60'/0'/0/${index}`)
|
const keyPair1 = hdWallet.derivePath(`m/44'/60'/0'/0/${index}`)
|
||||||
@ -13,10 +14,10 @@ export function newAccount(password: string, index: number) {
|
|||||||
|
|
||||||
export function newMnemonic(password: string) {
|
export function newMnemonic(password: string) {
|
||||||
let mnemonic = generateMnemonic()
|
let mnemonic = generateMnemonic()
|
||||||
this.saveMnemonic(mnemonic, password)
|
saveMnemonic(mnemonic, password)
|
||||||
return mnemonic
|
return mnemonic
|
||||||
}
|
}
|
||||||
|
|
||||||
export function restoreWalletByMnemonic(mnemonic: string, password: string) {
|
export function restoreWalletByMnemonic(mnemonic: string, password: string) {
|
||||||
this.saveMnemonic(mnemonic, password)
|
saveMnemonic(mnemonic, password)
|
||||||
}
|
}
|
@ -45,7 +45,7 @@ module.exports = {
|
|||||||
path: path.resolve(__dirname, "dist"),
|
path: path.resolve(__dirname, "dist"),
|
||||||
filename: "jcwallet.js",
|
filename: "jcwallet.js",
|
||||||
library: "jcwallet",
|
library: "jcwallet",
|
||||||
libraryTarget: "commonjs2",
|
// libraryTarget: "commonjs2",
|
||||||
// libraryTarget: "window",
|
libraryTarget: "window",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user