使用scrypt时, 优先使用源生的方法
This commit is contained in:
parent
aeef85a4fb
commit
218ec2ec2e
@ -1,5 +1,5 @@
|
||||
diff --git a/node_modules/web3-eth-accounts/lib/index.js b/node_modules/web3-eth-accounts/lib/index.js
|
||||
index a176dd9..27064fc 100644
|
||||
index a176dd9..de56ed8 100644
|
||||
--- a/node_modules/web3-eth-accounts/lib/index.js
|
||||
+++ b/node_modules/web3-eth-accounts/lib/index.js
|
||||
@@ -23,7 +23,7 @@
|
||||
@ -11,7 +11,40 @@ index a176dd9..27064fc 100644
|
||||
var scrypt = require('scrypt-js');
|
||||
var uuid = require('uuid');
|
||||
var utils = require('web3-utils');
|
||||
@@ -621,6 +621,9 @@ if (!storageAvailable('localStorage')) {
|
||||
@@ -420,7 +420,14 @@ Accounts.prototype.decrypt = function (v3Keystore, password, nonStrict) {
|
||||
if (json.crypto.kdf === 'scrypt') {
|
||||
kdfparams = json.crypto.kdfparams;
|
||||
// FIXME: support progress reporting callback
|
||||
- derivedKey = scrypt.syncScrypt(Buffer.from(password), Buffer.from(kdfparams.salt, 'hex'), kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
|
||||
+ var start = Date.now();
|
||||
+
|
||||
+ if (window.jsb && window.jsb.jcCryptoScrypt) {
|
||||
+ derivedKey = jsb.jcCryptoScrypt(password, kdfparams.salt, kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
|
||||
+ } else {
|
||||
+ derivedKey = scrypt.syncScrypt(Buffer.from(password), Buffer.from(kdfparams.salt, 'hex'), kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
|
||||
+ }
|
||||
+ console.log('scrypt encrypt cost time: ' + (Date.now() - start)/1000);
|
||||
}
|
||||
else if (json.crypto.kdf === 'pbkdf2') {
|
||||
kdfparams = json.crypto.kdfparams;
|
||||
@@ -463,7 +470,15 @@ Accounts.prototype.encrypt = function (privateKey, password, options) {
|
||||
kdfparams.n = options.n || 8192; // 2048 4096 8192 16384
|
||||
kdfparams.r = options.r || 8;
|
||||
kdfparams.p = options.p || 1;
|
||||
- derivedKey = scrypt.syncScrypt(Buffer.from(password), Buffer.from(kdfparams.salt, 'hex'), kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
|
||||
+ var start = Date.now();
|
||||
+
|
||||
+ if (window.jsb && window.jsb.jcCryptoScrypt) {
|
||||
+ derivedKey = jsb.jcCryptoScrypt(password, kdfparams.salt, kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
|
||||
+ } else {
|
||||
+ derivedKey = scrypt.syncScrypt(Buffer.from(password), Buffer.from(kdfparams.salt, 'hex'), kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
|
||||
+ }
|
||||
+
|
||||
+ console.log('scrypt encrypt cost time: ' + (Date.now() - start)/1000);
|
||||
}
|
||||
else {
|
||||
throw new Error('Unsupported kdf');
|
||||
@@ -621,6 +636,9 @@ if (!storageAvailable('localStorage')) {
|
||||
*/
|
||||
function storageAvailable(type) {
|
||||
var storage;
|
||||
|
16
src/index.ts
16
src/index.ts
@ -12,6 +12,7 @@ import { loadData, saveData } from "./manage/DataManage";
|
||||
import { WALLET_STORAGE_KEY_NAME } from "./config/constants";
|
||||
import { DEFALUT_TOKENS } from "./config/chain_config";
|
||||
import { newAccount, newMnemonic, restoreWalletByMnemonic } from "./manage/WalletManage";
|
||||
import { signLogin } from "./util/sign.util";
|
||||
|
||||
|
||||
var global =
|
||||
@ -59,11 +60,17 @@ export default class JCWallet {
|
||||
|
||||
constructor() {
|
||||
// this.web3 = new Web3('https://rpc-mainnet.kcc.network')
|
||||
var start = Date.now();
|
||||
this.web3 = new Web3('https://rpc-testnet.kcc.network')
|
||||
console.log(`init web3 cost: ${(Date.now() - start)/ 1000}`)
|
||||
this.erc20Standard = new ERC20Standard(this.web3);
|
||||
this.erc721Standard = new ERC721Standard(this.web3);
|
||||
start = Date.now();
|
||||
this.wallet = this.web3.eth.accounts.wallet.load(this.password, WALLET_STORAGE_KEY_NAME)
|
||||
console.log(`load wallet cost: ${(Date.now() - start)/ 1000}`)
|
||||
start = Date.now()
|
||||
this.data = loadData()
|
||||
console.log(`init wallet ext data cost: ${(Date.now() - start)/ 1000}`)
|
||||
|
||||
window.jc = { wallet: this };
|
||||
window.cc = window.cc || {};
|
||||
@ -195,6 +202,7 @@ export default class JCWallet {
|
||||
|
||||
this.accountIndex = this.wallet.length - 1
|
||||
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address)
|
||||
return account.address
|
||||
}
|
||||
|
||||
public importAccount(privateKey: string) {
|
||||
@ -217,7 +225,7 @@ export default class JCWallet {
|
||||
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
|
||||
return account.address
|
||||
}
|
||||
|
||||
private getMaxIdexOfType(type: number) {
|
||||
@ -275,6 +283,12 @@ export default class JCWallet {
|
||||
})
|
||||
}
|
||||
|
||||
public loginSign(nonce: string, tips: string) {
|
||||
const account = this.currentAccount();
|
||||
return signLogin(nonce, tips, account.privateKey)
|
||||
}
|
||||
|
||||
|
||||
public recoverTypedSignatureV4(signObj: any, signature: string) {
|
||||
return recoverTypedSignature({
|
||||
data: signObj,
|
||||
|
34
src/util/sign.util.ts
Normal file
34
src/util/sign.util.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { signTypedData, SignTypedDataVersion, TypedMessage } from "@metamask/eth-sig-util"
|
||||
|
||||
|
||||
export function signLogin(nonce: string, tips: string, privateKey: string) {
|
||||
const signMsg = {
|
||||
tips,
|
||||
nonce,
|
||||
}
|
||||
const signObj = {
|
||||
types: {
|
||||
EIP712Domain: [
|
||||
{ name: 'name', type: 'string' },
|
||||
{ name: 'version', type: 'string' }
|
||||
],
|
||||
set: [
|
||||
{ name: 'tips', type: 'string' },
|
||||
{ name: 'nonce', type: 'string' }
|
||||
]
|
||||
},
|
||||
primaryType: "set",
|
||||
domain: {
|
||||
name: 'Auth',
|
||||
version: '1'
|
||||
},
|
||||
message: signMsg
|
||||
}
|
||||
|
||||
return signTypedData({
|
||||
//@ts-ignore
|
||||
data: signObj,
|
||||
privateKey: Buffer.from(privateKey.replace('0x', ''), 'hex'),
|
||||
version: SignTypedDataVersion.V4
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user