商城下单购买,品类接口,钱包依赖
This commit is contained in:
126
wallet/Wallet.js
Normal file
126
wallet/Wallet.js
Normal file
@@ -0,0 +1,126 @@
|
||||
import Bitcore from "bitcore-lib"
|
||||
import Mnemonic from "bitcore-mnemonic"
|
||||
import secp256k1 from 'secp256k1'
|
||||
import {
|
||||
Address,
|
||||
pubToAddress,
|
||||
toBuffer,
|
||||
toChecksumAddress,
|
||||
intToBuffer
|
||||
} from 'ethereumjs-util'
|
||||
import coinType from './networks.js'
|
||||
import basex from 'base-x'
|
||||
|
||||
export default class Wallet {
|
||||
|
||||
static coinType = coinType
|
||||
|
||||
/**
|
||||
* 生成助记词
|
||||
* @param {Object} lang
|
||||
*/
|
||||
static generateMnemonic(lang) {
|
||||
if (lang) {
|
||||
return (new Mnemonic(this.getLanguage(lang))).toString();
|
||||
} else {
|
||||
return (new Mnemonic()).toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证助记词
|
||||
* @param {Object} code
|
||||
* @param {Object} lang
|
||||
*/
|
||||
static validMnemonic(code, lang) {
|
||||
if (lang) {
|
||||
return Mnemonic.isValid(code, this.getLanguage(lang));
|
||||
} else {
|
||||
return Mnemonic.isValid(code);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取助记词字典
|
||||
* @param {Object} lang
|
||||
*/
|
||||
static getLanguage(lang) {
|
||||
return Mnemonic.Words[lang]
|
||||
}
|
||||
|
||||
/**
|
||||
* 转成硬钱包私钥
|
||||
* @param {Object} code
|
||||
*/
|
||||
static toHDPrivateKey(code) {
|
||||
return (new Mnemonic(code)).toHDPrivateKey()
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证地址是否合法
|
||||
* @param {Object} addr
|
||||
*/
|
||||
static isValidAddress(addr) {
|
||||
return Bitcore.Address.isValid(addr)
|
||||
}
|
||||
|
||||
/**
|
||||
* 硬钱包私钥转成对应网络的 地址 和 私钥
|
||||
* @param {Object} hdPrivateKey
|
||||
* @param {Object} type
|
||||
*/
|
||||
static HDPrivateKeyToAddress(hdPrivateKey, type) {
|
||||
const derived = hdPrivateKey.derive("m/44'/" + type.type + "'/0'/0/0");
|
||||
if (type.type === 195) {
|
||||
const ethAddr = this.getEthereumAddress(derived)
|
||||
const addressBuffer = Buffer.concat([intToBuffer(0x41), ethAddr.buf], 21)
|
||||
return {
|
||||
address: Bitcore.encoding.Base58Check.encode(addressBuffer),
|
||||
public_key: derived.privateKey.publicKey.toString(),
|
||||
private_key: derived.privateKey.toString()
|
||||
}
|
||||
}
|
||||
if (type.type === 144) {
|
||||
let addr = derived.privateKey.toAddress(type.network).toString()
|
||||
let deco = Bitcore.encoding.Base58.decode(addr)
|
||||
|
||||
return {
|
||||
address: basex('rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz').encode(deco),
|
||||
public_key: derived.privateKey.publicKey.toString(),
|
||||
private_key: derived.privateKey.toString()
|
||||
}
|
||||
}
|
||||
if (this.networkIsEthereum(type)) {
|
||||
return {
|
||||
address: toChecksumAddress(this.getEthereumAddress(derived).toString()),
|
||||
public_key: derived.privateKey.publicKey.toString(),
|
||||
private_key: derived.privateKey.toString()
|
||||
}
|
||||
}
|
||||
return {
|
||||
address: derived.privateKey.toAddress(type.network).toString(),
|
||||
public_key: derived.privateKey.publicKey.toString(),
|
||||
private_key: derived.privateKey.toString()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 以太坊地址格式转换
|
||||
* @param {Object} derived
|
||||
*/
|
||||
static getEthereumAddress(derived) {
|
||||
const publicKey = derived.hdPublicKey.publicKey.toBuffer()
|
||||
const ethPublicKey = secp256k1.publicKeyConvert(publicKey, false)
|
||||
.slice(1)
|
||||
return Address.fromPublicKey(toBuffer(ethPublicKey))
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是以太坊网络
|
||||
* @param {Object} type
|
||||
*/
|
||||
static networkIsEthereum(type) {
|
||||
return type.isEthereum
|
||||
}
|
||||
|
||||
}
|
||||
29
wallet/_test.js
Normal file
29
wallet/_test.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import {
|
||||
Wallet
|
||||
} from "./Wallet.js"
|
||||
|
||||
const code = Wallet.generateMnemonic(this.defaultLanguage)
|
||||
this.mnemonicCode = code
|
||||
const hdPrivateKey = Wallet.toHDPrivateKey(this.mnemonicCode)
|
||||
// const derived = hdPrivateKey.derive("m/44'/61'/0'/0/0");
|
||||
// const publicKey = derived.hdPublicKey.publicKey.toBuffer()
|
||||
// const ethPublicKey = secp256k1.publicKeyConvert(publicKey, false)
|
||||
// .slice(1)
|
||||
|
||||
// const ethAddr = Address.fromPublicKey(toBuffer(ethPublicKey)).toString();
|
||||
|
||||
// console.log(toChecksumAddress(ethAddr));
|
||||
|
||||
// console.log(Address.fromPrivateKey(hdPrivateKey.hdPublicKey.publicKey.toBuffer()));
|
||||
|
||||
var addr = []
|
||||
for (var i in this.coinType) {
|
||||
let whk = Wallet.HDPrivateKeyToAddress(hdPrivateKey, i)
|
||||
let parmas = {
|
||||
name: this.coinType[i],
|
||||
address: whk.address,
|
||||
private_key: whk.private_key,
|
||||
}
|
||||
addr.push(parmas)
|
||||
}
|
||||
this.address = addr
|
||||
24
wallet/index.js
Normal file
24
wallet/index.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import store from "@/store/index.js"
|
||||
|
||||
const USE_SOTER_AUTH_KEY = 'USE_SOTER_AUTH_KEY'
|
||||
|
||||
/**
|
||||
* 初始化配置
|
||||
*/
|
||||
const initWalletConfigs = () => {
|
||||
// 生物识别
|
||||
const USE_SOTER = Boolean(uni.getStorageSync(USE_SOTER_AUTH_KEY))
|
||||
store.dispatch('wallet/setSoterAuth', USE_SOTER)
|
||||
// 获取默认钱包
|
||||
}
|
||||
|
||||
const setSoterAuthStatus = (opt) => {
|
||||
uni.setStorageSync(USE_SOTER_AUTH_KEY, opt)
|
||||
store.dispatch('wallet/setSoterAuth', opt)
|
||||
}
|
||||
|
||||
export default {
|
||||
USE_SOTER_AUTH_KEY,
|
||||
setSoterAuthStatus,
|
||||
initWalletConfigs
|
||||
}
|
||||
209
wallet/networks.js
Normal file
209
wallet/networks.js
Normal file
@@ -0,0 +1,209 @@
|
||||
import Bitcore from "bitcore-lib"
|
||||
|
||||
export default [{
|
||||
type: 0,
|
||||
name: '比特币',
|
||||
symbol: 'BTC',
|
||||
code: 'btc',
|
||||
isEthereum: false,
|
||||
network: Bitcore.Networks.mainnet
|
||||
},
|
||||
{
|
||||
type: 60,
|
||||
name: '以太坊',
|
||||
symbol: 'ETH',
|
||||
code: 'eth',
|
||||
isEthereum: true
|
||||
},
|
||||
{
|
||||
type: 61,
|
||||
name: '以太经典',
|
||||
symbol: 'ETC',
|
||||
code: 'etc',
|
||||
isEthereum: true
|
||||
},
|
||||
{
|
||||
type: 60,
|
||||
name: '赤子心',
|
||||
symbol: 'CZX',
|
||||
code: 'eth_0x3a2a239b1bdaae768ffa06314d523e88e98d4d1f',
|
||||
isEthereum: true
|
||||
},
|
||||
// {
|
||||
// type: 2,
|
||||
// name: '莱特币',
|
||||
// symbol: 'LTC',
|
||||
// isEthereum: false,
|
||||
// network: Bitcore.Networks.add({
|
||||
// name: 'LTC',
|
||||
// alias: 'LTC',
|
||||
// pubkeyhash: 0x30,
|
||||
// privatekey: 0x32,
|
||||
// scripthash: 0xb0,
|
||||
// bech32prefix: 'ltc',
|
||||
// xpubkey: 0x019da462,
|
||||
// xprivkey: 0x019d9cfe,
|
||||
// networkMagic: 0xdbb6c0fb
|
||||
// })
|
||||
// },
|
||||
{
|
||||
type: 3,
|
||||
name: '狗狗币',
|
||||
symbol: 'DOGE',
|
||||
code: 'doge',
|
||||
isEthereum: false,
|
||||
network: Bitcore.Networks.add({
|
||||
name: 'DOGE',
|
||||
alias: 'DOGE',
|
||||
pubkeyhash: 0x1e,
|
||||
privatekey: 0x16,
|
||||
scripthash: 0x9e,
|
||||
bech32prefix: 'doge',
|
||||
xpubkey: 0x02facafd,
|
||||
xprivkey: 0x02fac398,
|
||||
networkMagic: 0xc0c0c0c0
|
||||
})
|
||||
},
|
||||
|
||||
// {
|
||||
// type: 133,
|
||||
// name: '零币',
|
||||
// symbol: 'ZEC',
|
||||
// isEthereum: false,
|
||||
// network: Bitcore.Networks.add({
|
||||
// name: 'ZEC',
|
||||
// alias: 'ZEC',
|
||||
// pubkeyhash: 0x1e,
|
||||
// privatekey: 0x16,
|
||||
// scripthash: 0x9e,
|
||||
// bech32prefix: 'doge',
|
||||
// xpubkey: 0x02facafd,
|
||||
// xprivkey: 0x02fac398,
|
||||
// networkMagic: 0xc0c0c0c0
|
||||
// })
|
||||
// },
|
||||
// {
|
||||
// type: 144,
|
||||
// name: 'XPR - 瑞波币',
|
||||
// symbol: 'XPR',
|
||||
// isEthereum: false,
|
||||
// network: Bitcore.Networks.add({
|
||||
// name: 'XPR',
|
||||
// alias: 'XPR',
|
||||
// pubkeyhash: 0x1e,
|
||||
// privatekey: 0x16,
|
||||
// scripthash: 0x9e,
|
||||
// bech32prefix: 'doge',
|
||||
// xpubkey: 0x02facafd,
|
||||
// xprivkey: 0x02fac398,
|
||||
// networkMagic: 0xc0c0c0c0
|
||||
// })
|
||||
// },
|
||||
// {
|
||||
// type: 145,
|
||||
// name: '比特现金',
|
||||
// symbol: 'BCH',
|
||||
// isEthereum: false,
|
||||
// network: Bitcore.Networks.add({
|
||||
// name: 'BCH',
|
||||
// alias: 'BCH',
|
||||
// pubkeyhash: 0x00,
|
||||
// privatekey: 0x05,
|
||||
// scripthash: 0x80,
|
||||
// bech32prefix: 'bitcoincash',
|
||||
// xpubkey: 0x0488b21e,
|
||||
// xprivkey: 0x0488ade4,
|
||||
// networkMagic: 0xd9b4bef9
|
||||
// })
|
||||
// },
|
||||
// {
|
||||
// type: 195,
|
||||
// name: '波场',
|
||||
// symbol: 'TRX',
|
||||
// isEthereum: false,
|
||||
// network: Bitcore.Networks.add({
|
||||
// name: 'TRX',
|
||||
// alias: 'TRX',
|
||||
// pubkeyhash: 0x41,
|
||||
// privatekey: 0x05,
|
||||
// scripthash: 0x80,
|
||||
// bech32prefix: '',
|
||||
// xpubkey: 0x0488b21e,
|
||||
// xprivkey: 0x0488ade4
|
||||
// })
|
||||
// },
|
||||
{
|
||||
type: 195,
|
||||
name: 'USDT(TRC20)',
|
||||
symbol: 'USDT',
|
||||
code: 'trx_TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
|
||||
isEthereum: false,
|
||||
network: Bitcore.Networks.add({
|
||||
name: 'USDT',
|
||||
alias: 'USDT',
|
||||
pubkeyhash: 0x41,
|
||||
privatekey: 0x05,
|
||||
scripthash: 0x80,
|
||||
bech32prefix: '',
|
||||
xpubkey: 0x0488b21e,
|
||||
xprivkey: 0x0488ade4
|
||||
})
|
||||
},
|
||||
{
|
||||
type: 195,
|
||||
name: 'USDT(ERC20)',
|
||||
symbol: 'USDT',
|
||||
code: 'eth_0xdac17f958d2ee523a2206206994597c13d831ec7',
|
||||
isEthereum: true
|
||||
},
|
||||
{
|
||||
type: 0,
|
||||
name: 'USDT(OMNI)',
|
||||
symbol: 'USDT',
|
||||
code: 'usdt',
|
||||
isEthereum: false,
|
||||
network: Bitcore.Networks.mainnet
|
||||
},
|
||||
{
|
||||
type: 13107,
|
||||
name: '比特元',
|
||||
symbol: 'BTY',
|
||||
code: 'bty',
|
||||
isEthereum: false,
|
||||
network: Bitcore.Networks.add({
|
||||
name: 'BTY',
|
||||
alias: 'BTY',
|
||||
pubkeyhash: 0x00,
|
||||
privatekey: 0x05,
|
||||
scripthash: 0x80,
|
||||
bech32prefix: 'bityuan',
|
||||
xpubkey: 0x0488b21e,
|
||||
xprivkey: 0x0488ade4,
|
||||
networkMagic: 0xd9b4bef9
|
||||
})
|
||||
},
|
||||
// {
|
||||
// type: 60,
|
||||
// name: '元链',
|
||||
// symbol: 'YCC',
|
||||
// isEthereum: true
|
||||
// },
|
||||
{
|
||||
type: 13107,
|
||||
name: 'JZC',
|
||||
symbol: 'JZC',
|
||||
code: 'bty',
|
||||
isEthereum: false,
|
||||
network: Bitcore.Networks.add({
|
||||
name: 'JZC',
|
||||
alias: 'JZC',
|
||||
pubkeyhash: 0x00,
|
||||
privatekey: 0x05,
|
||||
scripthash: 0x80,
|
||||
bech32prefix: 'bityuan',
|
||||
xpubkey: 0x0488b21e,
|
||||
xprivkey: 0x0488ade4,
|
||||
networkMagic: 0xd9b4bef9
|
||||
})
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user