修改wallet的key生成方式
This commit is contained in:
parent
0ff052e527
commit
9b7d48b068
@ -5,6 +5,7 @@ import { router } from 'decorators/router'
|
|||||||
import { Wallet } from 'modules/Wallet'
|
import { Wallet } from 'modules/Wallet'
|
||||||
import { WalletExt } from 'modules/WalletExt'
|
import { WalletExt } from 'modules/WalletExt'
|
||||||
import { customAlphabet } from 'nanoid'
|
import { customAlphabet } from 'nanoid'
|
||||||
|
import { genRandomString, sha3_256, sha512 } from 'utils/security.util'
|
||||||
|
|
||||||
const nanoid = customAlphabet('1234567890abcdef', 10)
|
const nanoid = customAlphabet('1234567890abcdef', 10)
|
||||||
|
|
||||||
@ -15,8 +16,9 @@ class WalletController extends BaseController {
|
|||||||
let record = await Wallet.insertOrUpdate({ account: user.id }, {})
|
let record = await Wallet.insertOrUpdate({ account: user.id }, {})
|
||||||
let data: any = { oid: user.id }
|
let data: any = { oid: user.id }
|
||||||
if (record.nweRecord) {
|
if (record.nweRecord) {
|
||||||
record.salt = nanoid()
|
record.salt = nanoid(16)
|
||||||
record.is = nanoid(12)
|
const key = sha3_256(sha512(genRandomString(16), genRandomString(12)).passwordHash)
|
||||||
|
record.key = key
|
||||||
record.nweRecord = false
|
record.nweRecord = false
|
||||||
await record.save()
|
await record.save()
|
||||||
}
|
}
|
||||||
@ -48,18 +50,15 @@ class WalletController extends BaseController {
|
|||||||
@router('post /wallet/info')
|
@router('post /wallet/info')
|
||||||
async uploadWalletInfo(req, res) {
|
async uploadWalletInfo(req, res) {
|
||||||
let user = req.user
|
let user = req.user
|
||||||
let { key, address } = req.params
|
let { address } = req.params
|
||||||
if (!key && !address) {
|
if (!address) {
|
||||||
throw new ZError(10, 'no data to save')
|
throw new ZError(10, 'no data to save')
|
||||||
}
|
}
|
||||||
let record = await Wallet.insertOrUpdate({ account: user.id }, {})
|
let record = await Wallet.findOne({ account: user.id })
|
||||||
if (key) {
|
if (!record) {
|
||||||
record.key = key
|
throw new ZError(11, 'no record found')
|
||||||
}
|
|
||||||
|
|
||||||
if (address) {
|
|
||||||
record.address = address
|
|
||||||
}
|
}
|
||||||
|
record.address = address
|
||||||
await record.save()
|
await record.save()
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
@ -19,12 +19,6 @@ class WalletClass extends BaseModule {
|
|||||||
|
|
||||||
@prop()
|
@prop()
|
||||||
public address: string
|
public address: string
|
||||||
|
|
||||||
/**
|
|
||||||
* 钱包客户端存储的密码
|
|
||||||
*/
|
|
||||||
@prop()
|
|
||||||
public is: string
|
|
||||||
/**
|
/**
|
||||||
* 用于客户端生成密钥时的加盐
|
* 用于客户端生成密钥时的加盐
|
||||||
*/
|
*/
|
||||||
@ -37,7 +31,6 @@ class WalletClass extends BaseModule {
|
|||||||
public toJson() {
|
public toJson() {
|
||||||
return {
|
return {
|
||||||
key: this.key,
|
key: this.key,
|
||||||
is: this.is,
|
|
||||||
salt: this.salt,
|
salt: this.salt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,12 @@ export function sha512(password: string, salt: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function sha3_256(str: string) {
|
||||||
|
let hash = crypto.createHash('sha3-256')
|
||||||
|
hash.update(str)
|
||||||
|
return hash.digest('hex')
|
||||||
|
}
|
||||||
|
|
||||||
export function genRandomString(length: number) {
|
export function genRandomString(length: number) {
|
||||||
return crypto
|
return crypto
|
||||||
.randomBytes(Math.ceil(length / 2))
|
.randomBytes(Math.ceil(length / 2))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user