react-native-idcard-lib
Version:
IDCard lib for React Native
896 lines (767 loc) • 30.2 kB
JavaScript
import _ from 'lodash'
const Web3 = require('web3')
var IDCard = function (paras) {
var idc = this,
config = require('./config/config-1.json');
const contractAddress = config.idcard.mainnet;
const web3 = new Web3(
new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/b232b95452a643aea7291edf71e67759')
),
idcardContract = new web3.eth.Contract(require('./config/abi-idcard-kovan-1.json'), contractAddress, {});
idc.web3ResultAlias = function (result) {
// Del Int Key
for (var key in result) {
if (/^\d+(\.\d+)?$/.test(key))
result = _.omit(result, key)
}
return result
}
// ******************************************* WALLET *******************************************
idc.Wallet = function (pwd) {
var idcWallet = this
idcWallet.list = () => {
return web3.eth.accounts.wallet
}
idcWallet.setDefault = (address) => {
web3.eth.defaultAccount = address
}
idcWallet.getDefault = () => {
return web3.eth.defaultAccount
}
idcWallet.import = (walletKeystore) => {
function importPromise(resolve, reject) {
try {
const walletObj = web3.eth.accounts.wallet.decrypt(walletKeystore, pwd)
resolve(walletObj)
} catch (err) {
reject(err)
}
}
return new Promise(importPromise)
}
idcWallet.create = () => {
let preLength = web3.eth.accounts.wallet.length
let newAddress = web3.eth.accounts.wallet.create(1)[preLength].address
return {
newAddress: newAddress,
walletKeystore: web3.eth.accounts.wallet.encrypt(pwd)
}
}
idcWallet.add = (account) => {
const newWallet = web3.eth.accounts.wallet.add(account)
return {
newAddress: newWallet.address,
walletKeystore: web3.eth.accounts.wallet.encrypt(pwd)
}
}
idcWallet.remove = (account) => {
web3.eth.accounts.wallet.remove(account)
return web3.eth.accounts.wallet.encrypt(pwd)
}
// wallet info
idcWallet.getBalance = (address = idcWallet.getDefault()) => {
return web3.eth.getBalance(address)
}
// Wallet Command
idcWallet.transfer = {
to: null,
value: null,
data: null,
autoSetObj: async function (to, value, data) {
this.to = to
this.value = value
this.data = web3.utils.utf8ToHex(data)
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.to = null
this.value = null
this.data = null
}
}
}
idc.wallet = new idc.Wallet(paras.pwd)
// ******************************************* Command *******************************************
idc.Command = function () {
var cmd = this;
cmd.fromWei = (value, unit = 'ether') => {
return web3.utils.fromWei(value, unit)
}
cmd.toWei = (value, unit = 'ether') => {
return web3.utils.toWei(value, unit)
}
cmd.hexToUtf8 = (value) => {
return web3.utils.hexToUtf8(value)
}
cmd.utf8ToHex = (value) => {
return web3.utils.utf8ToHex(value)
}
cmd.getGasPrice = () => {
return web3.eth.getGasPrice()
}
cmd.isAddress = (address) => {
return web3.utils.isAddress(address)
}
// cmd.ipfsCat = (hash) => {
// return ipfs.files.cat(hash)
// }
}
idc.command = new idc.Command()
// *************************************** Contract Caller ***************************************
idc.Caller = function () {
var cer = this
cer.getIDCard = async (address) => {
return idcardContract.methods.IDs(address).call()
}
cer.getIDCBalance = async (address = idc.wallet.getDefault()) => {
return idcardContract.methods.wallet(address).call()
}
cer.getInfo = async (name) => {
return idcardContract.methods.info(web3.utils.utf8ToHex(name)).call()
}
cer.getUserByAddress = async (address) => {
const name = await cer.getIDCard(address)
if (parseInt(name) === 0) return {
used: false
}
const info = await cer.getInfo(web3.utils.hexToUtf8(name))
return info
}
cer.getMediaCount = async (name) => {
return idcardContract.methods.article_count(web3.utils.utf8ToHex(name)).call()
}
cer.getMCommentCount = async (name, i) => {
return idcardContract.methods.m_comments_count(web3.utils.utf8ToHex(name), i).call()
}
cer.getCommentCount = async (name) => {
return idcardContract.methods.comments_count(web3.utils.utf8ToHex(name)).call()
}
cer.getFollowCount = async (name) => {
return idcardContract.methods.follow_count(web3.utils.utf8ToHex(name)).call()
}
cer.getFollowCountTotal = async (name) => {
return idcardContract.methods.follow_count_total(web3.utils.utf8ToHex(name)).call()
}
cer.getFollowedCount = async (name) => {
return idcardContract.methods.followed_count(web3.utils.utf8ToHex(name)).call()
}
cer.getFollowedCountTotal = async (name) => {
return idcardContract.methods.followed_count_total(web3.utils.utf8ToHex(name)).call()
}
cer.getSellableInfo = async (name) => {
return idcardContract.methods.IDmarket(web3.utils.utf8ToHex(name)).call()
}
cer.getFollowStatus = async (name, other) => {
return idcardContract.methods.rStatus(web3.utils.utf8ToHex(name), web3.utils.utf8ToHex(other)).call()
}
cer.getFollowList = async (name) => {
const count = parseInt(await cer.getFollowCountTotal(name))
const followPromises = _.times(count, Number).map(async (i) => {
const followName = await idcardContract.methods.follow(web3.utils.utf8ToHex(name), i).call()
const rStatus = await idcardContract.methods.rStatus(web3.utils.utf8ToHex(name), followName).call()
if (rStatus.active)
return web3.utils.hexToUtf8(followName)
})
const followList = await Promise.all(followPromises)
return _.compact(followList)
}
cer.getMineFollowList = async () => {
const info = await cer.getUserByAddress(web3.eth.defaultAccount)
if (info.used === true) {
const followList = await cer.getFollowList(web3.utils.hexToUtf8(info.name))
return followList
} else {
return false
}
}
cer.getFollowedList = async (name) => {
const count = parseInt(await cer.getFollowedCountTotal(name))
const followedPromises = _.times(count, Number).map(async (i) => {
const followedName = await idcardContract.methods.followed(web3.utils.utf8ToHex(name), i).call()
const rStatus = await idcardContract.methods.rStatus(followedName, web3.utils.utf8ToHex(name)).call()
if (rStatus.active)
return web3.utils.hexToUtf8(followedName)
})
const followedList = await Promise.all(followedPromises)
return _.compact(followedList)
}
cer.listAlphabet = (list) => {
let resultList = {}
const Letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
// 遍历名单
list.forEach((name) => {
const Initials = _.upperFirst(name).substr(0, 1)
const alphabetIndex = _.indexOf(Letters, Initials)
if (alphabetIndex !== -1) {
const Letter = Letters[alphabetIndex]
if (resultList[Letter] === undefined) resultList[Letter] = []
else resultList[Letter].push(name)
} else {
if (resultList['#'] === undefined) resultList['#'] = []
else resultList['#'].push(name)
}
})
return resultList
}
cer.getGoldInfo = async (index) => {
return idcardContract.methods.goldenList(index).call()
}
cer.getGoldList = async () => {
const goldCount = parseInt(await idcardContract.methods.gold_count().call())
let goldList = []
const goldPromises = _.times(goldCount, Number).map(async (i) => {
let info = await idcardContract.methods.goldenList(i + 1).call()
info = idc.web3ResultAlias(info)
info.userName = web3.utils.hexToUtf8(info.name)
info.index = (i + 1)
goldList.splice(_.sortedIndexBy(goldList, info, 'index'), 0, info)
})
await Promise.all(goldPromises)
return goldList
}
cer.getGoldStatus = async () => {
let sT = await idcardContract.methods.startTime().call()
let bT = await idcardContract.methods.biddingTime().call()
sT = parseInt(sT)
bT = parseInt(bT)
const nT = Math.round(new Date().getTime() / 1000)
let result
if (nT === 0) {
result = {
status: 0,
}
} else if (((nT - sT) >= bT) && (nT > sT)) {
result = {
status: 0,
}
} else {
result = {
status: 1,
startTime: sT,
leftTime: (sT + bT) - nT
}
}
result.cycle = bT
return result
}
cer.getGoldNum = async () => {
let goldNum = await idcardContract.methods.gold_count().call()
return parseInt(goldNum)
}
cer.getBiddingPrice = async () => {
return idcardContract.methods.biddingPrice().call()
}
cer.getHighestBidder = async () => {
const name = await idcardContract.methods.highestBidder().call()
return web3.utils.hexToUtf8(name)
}
cer.getGoldStore = async () => {
return idcardContract.methods.goldStore().call()
}
cer.getSellableID = async () => {
let IDArray = []
let sellableIDArray = []
let resultArray = []
const preEventsArray = await idcardContract.getPastEvents('sell_log', {
fromBlock: 0
})
preEventsArray.forEach(function (event) {
IDArray.push(event.returnValues.name)
})
// 去重
IDArray = _.uniq(IDArray)
const idPromises = IDArray.map(async (id) => {
let info = await cer.getSellableInfo(web3.utils.hexToUtf8(id))
if (info.sellable) sellableIDArray.push(id)
})
await Promise.all(idPromises)
// 获取详细信息
sellableIDArray.forEach(function (id) {
const index = _.findLastIndex(preEventsArray, function (o) {
return o.returnValues.name == id
})
let resultObj = idc.web3ResultAlias(preEventsArray[index].returnValues)
// 防止无锁产生的数据冲突问题
if (!resultObj.sellable) return
resultObj.userName = web3.utils.hexToUtf8(resultObj.name)
resultArray.push(resultObj)
})
return resultArray
}
cer.getFee = function (level) {
return idcardContract.methods.FEE(level).call()
}
cer.getUserMedia = async (name, callback, isGold = 0) => {
// let address = await idcardContract.methods.owner().call()
let hexName = web3.utils.utf8ToHex(name)
let mCount = await cer.getMediaCount(name)
if (mCount == 0) {
callback({
key: 'empty'
})
return
}
for (let i = 0; i < mCount; i++) {
idcardContract.methods.articles(hexName, i).call()
.then(async (data) => {
let info = await idcardContract.methods.info(hexName).call()
callback({
key: `${name}-${i}`,
name: name,
index: i,
address: info.addr,
title: data.title ? data.title : null,
text: data.content,
goldMark: isGold ? 1 : 0,
time: data.time
})
})
}
}
cer.getMineMedia = async (callback) => {
const info = await cer.getUserByAddress(web3.eth.defaultAccount)
if (info.used === true) {
cer.getUserMedia(web3.utils.hexToUtf8(info.name), callback)
}
}
cer.getMComments = async (name, index, callback) => {
let hexName = web3.utils.utf8ToHex(name)
let mcCount = await idcardContract.methods.m_comments_count(hexName, index).call()
if (mcCount == 0) {
callback({
key: 'empty'
})
return
}
for (let i = 0; i < mcCount; i++) {
idcardContract.methods.m_comments(hexName, index, i).call()
.then(async (data) => {
let info = await idcardContract.methods.info(data.from).call()
callback({
key: `${name}-${index}-${i}`,
from: web3.utils.hexToUtf8(data.from),
fromAddress: info.addr,
name: name,
text: data.content,
time: data.time
})
})
}
}
cer.getUserComment = async (name, callback) => {
let hexName = web3.utils.utf8ToHex(name)
let cCount = await cer.getCommentCount(name)
if (cCount == 0) {
callback({
key: 'empty'
})
return
}
for (let i = 0; i < cCount; i++) {
idcardContract.methods.comments(hexName, i).call()
.then(async (data) => {
let info = await idcardContract.methods.info(data.from).call()
callback({
key: `${name}-${i}`,
name: web3.utils.hexToUtf8(data.from),
address: info.addr,
title: null,
text: data.content,
goldMark: 0,
time: data.time
})
})
}
}
}
idc.caller = new idc.Caller()
// *************************************** Contract Sender ***************************************
idc.Sender = function () {
var ser = this
ser.register = {
to: contractAddress,
value: null,
data: null,
autoSetObj: async function (name, about) {
this.value = web3.utils.fromWei(await idc.caller.getFee(2))
this.data = idcardContract.methods.Register(web3.utils.utf8ToHex(name), about).encodeABI()
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.value = null
this.gas = null
this.gasPrice = null
this.data = null
}
}
ser.editAbout = {
to: contractAddress,
value: 0,
data: null,
autoSetObj: function (about) {
this.data = idcardContract.methods.changeAbout(about).encodeABI()
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.gas = null
this.gasPrice = null
this.data = null
}
}
ser.sendMoment = {
to: contractAddress,
value: 0,
data: null,
autoSetObj: function (content) {
this.data = idcardContract.methods.aaarticle('', content).encodeABI()
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.gas = null
this.gasPrice = null
this.data = null
}
}
ser.follow = {
to: contractAddress,
value: 0,
data: null,
autoSetObj: function (aim) {
this.data = idcardContract.methods.fooollow(web3.utils.utf8ToHex(aim)).encodeABI()
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.gas = null
this.gasPrice = null
this.data = null
}
}
ser.unfollow = {
to: contractAddress,
value: 0,
data: null,
autoSetObj: function (aim) {
this.data = idcardContract.methods.unfooollow(web3.utils.utf8ToHex(aim)).encodeABI()
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.gas = null
this.gasPrice = null
this.data = null
}
}
ser.comment = {
to: contractAddress,
value: 0,
data: null,
autoSetObj: function (aim, content) {
this.data = idcardContract.methods.cooomment(web3.utils.utf8ToHex(aim), content).encodeABI()
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.gas = null
this.gasPrice = null
this.data = null
}
}
ser.mComment = {
to: contractAddress,
value: 0,
data: null,
autoSetObj: function (aim, index, content) {
this.data = idcardContract.methods.comment_article(web3.utils.utf8ToHex(aim), index, content).encodeABI()
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.gas = null
this.gasPrice = null
this.data = null
}
}
ser.sellCard = {
to: contractAddress,
value: 0,
data: null,
autoSetObj: function (price, isSell) {
this.data = idcardContract.methods.sell_panel(web3.utils.toWei(price), isSell).encodeABI()
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.gas = null
this.gasPrice = null
this.data = null
}
}
ser.buyCard = {
to: contractAddress,
value: 0,
data: null,
autoSetObj: function (aim, value) {
this.value = web3.utils.fromWei(value)
this.data = idcardContract.methods.buyID(web3.utils.utf8ToHex(aim)).encodeABI()
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.gas = null
this.gasPrice = null
this.data = null
}
}
ser.bid = {
to: contractAddress,
value: 0,
data: null,
autoSetObj: function (value) {
this.value = web3.utils.fromWei(value)
this.data = idcardContract.methods.bidGold().encodeABI()
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.gas = null
this.gasPrice = null
this.data = null
}
}
}
idc.sender = new idc.Sender()
// *************************************** Contract Events ***************************************
idc.Events = function () {
var eve = this
}
idc.events = new idc.Events()
idc.Token = function () {
var tok = this,
tokenList = []
tok.getList = function () {
return tokenList
}
tok.getIndex = function (symbol) {
return _.findIndex(tokenList, ['symbol', symbol])
}
tok.getInfo = function (symbol) {
const index = tok.getIndex(symbol)
return tokenList[index]
}
tok.setContract = function () {
tokenList.forEach((item, index) => {
tokenList[index].contract = new web3.eth.Contract(require('./config/abi-erc20.json'), item.address, {})
})
}
tok.setList = function (list) {
tokenList = list
tok.setContract()
}
tok.getBalance = function (tokenIndex, address = idc.wallet.getDefault()) {
return tokenList[tokenIndex].contract.methods.balanceOf(address).call()
}
tok.transfer = {
to: null,
value: 0,
data: null,
autoSetObj: async function (to, value, tokenIndex) {
this.to = tokenList[tokenIndex].address
this.data = tokenList[tokenIndex].contract.methods.transfer(to, web3.utils.toWei(value.toString())).encodeABI()
},
getObj: function () {
return {
from: web3.eth.defaultAccount,
to: this.to,
value: web3.utils.toWei(this.value.toString()),
data: this.data
}
},
getGas: function () {
return web3.eth.estimateGas(this.getObj())
},
execute: function (gas, gasPrice) {
let obj = this.getObj()
obj.gas = parseInt(gas)
obj.gasPrice = web3.utils.toWei(gasPrice.toString(), 'gwei')
return web3.eth.sendTransaction(obj)
},
clear: function () {
this.to = null
this.value = null
this.data = null
}
}
}
idc.token = new idc.Token()
}
export default IDCard