@33cn/chain33-transaction-parser
Advanced tools
Comparing version 1.0.1 to 1.0.2
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var txmap_1 = require("./txmap"); | ||
/** | ||
* 辅助解析交易的含义 | ||
* | ||
* 第一层的key 取自 execer | ||
* actions 取自 payload payload[action] | ||
* | ||
* coinAmount 为币的数量所在字段 payload[action][coinAmount] | ||
* 若为空 则为0 | ||
* coinSymbol 为币的代称所在字段 payload[action][coinSymbol] | ||
* 若为空 则取 coinStaticSymbol | ||
* | ||
* coinStaticSymbol 为默认币种名称 | ||
* | ||
*/ | ||
* @description 解析交易内容 | ||
* @param {string} execer 执行器 | ||
* @param {any} payload 交易内容 | ||
* @param {string} [lang='cn'] 解析后语言选择, 默认是中文 | ||
* @returns {object} | ||
*/ | ||
var txParser = function (execer, payload, lang) { | ||
if (lang === void 0) { lang = "cn"; } | ||
if (lang === void 0) { lang = 'cn'; } | ||
if (/^user\./.test(execer)) | ||
@@ -23,3 +17,3 @@ return { execerName: execer, action: {} }; | ||
return { execerName: '平行链', action: {} }; | ||
if (!txmap[execer]) { | ||
if (!txmap_1.default[execer]) { | ||
console.warn('execer name undefined in tx parser', execer); | ||
@@ -32,3 +26,3 @@ return { execerName: execer, action: {} }; | ||
} | ||
var execerName = txmap[execer]['name'][lang]; | ||
var execerName = txmap_1.default[execer]['name'][lang]; | ||
var action = {}; | ||
@@ -43,3 +37,3 @@ if (payload) { | ||
var actionKeyInMap = actionKey.toLowerCase(); | ||
var actionmap = txmap[execer]['actions'][actionKeyInMap]; | ||
var actionmap = txmap_1.default[execer]['actions'][actionKeyInMap]; | ||
if (!actionmap) { | ||
@@ -59,10 +53,8 @@ console.warn('undefined actionKey', actionKey); | ||
/** | ||
* | ||
* 输出交易信息为字符串 | ||
* @export | ||
* @param {*} execer | ||
* @param {*} payload | ||
* @param {string} [lang='cn'] | ||
* @returns | ||
*/ | ||
* @description 解析交易内容 | ||
* @param {string} execer 执行器 | ||
* @param {any} payload 交易内容 | ||
* @param {string} [lang='cn'] 解析后语言选择, 默认是中文 | ||
* @returns {string} 输出信息为字符串 | ||
*/ | ||
var txInWords = function (execer, payload, lang) { | ||
@@ -74,6 +66,5 @@ if (lang === void 0) { lang = 'cn'; } | ||
/** | ||
* 16进制字符串转unicode字符串 | ||
* | ||
* @param {string} [hex=''] | ||
* @returns | ||
* @description 16进制字符串转unicode字符串 | ||
* @param {string} [hex=''] 16进制字符串 | ||
* @returns {string} unicode字符串 | ||
*/ | ||
@@ -105,8 +96,6 @@ var hex2str = function (hex) { | ||
/** | ||
* unicode字符串转16进制字符串 | ||
* | ||
* @export | ||
* @param {*} str | ||
* @returns | ||
*/ | ||
* @description unicode字符串转16进制字符串 | ||
* @param {string} [hex=''] unicode字符串 | ||
* @returns {string} 16进制字符串 | ||
*/ | ||
var str2hex = function (str) { | ||
@@ -122,71 +111,2 @@ if (typeof str !== 'string') | ||
}; | ||
var txmap = { | ||
"ticket": { | ||
"name": { | ||
"cn": "购票", | ||
"en": "ticket" | ||
}, | ||
"actions": { | ||
"miner": { | ||
"name": { | ||
"cn": "挖矿", | ||
"en": "miner" | ||
}, | ||
"coinAmount": "reward", | ||
"coinSymbol": null, | ||
"coinStaticSymbol": "BTY", | ||
}, | ||
"topen": { | ||
"name": { | ||
"cn": "开启挖矿", | ||
"en": "topen" | ||
}, | ||
"coinAmount": "reward", | ||
"coinSymbol": null, | ||
"coinStaticSymbol": "BTY", | ||
}, | ||
"tclose": { | ||
"name": { | ||
"cn": "关闭挖矿", | ||
"en": "tclose" | ||
}, | ||
"coinAmount": "reward", | ||
"coinSymbol": null, | ||
"coinStaticSymbol": "BTY", | ||
}, | ||
} | ||
}, | ||
"coins": { | ||
"name": { | ||
"cn": "币", | ||
"en": "coins" | ||
}, | ||
"actions": { | ||
"withdraw": { | ||
"name": { | ||
"cn": "提币", | ||
"en": "withdraw" | ||
}, | ||
"coinAmount": "amount", | ||
"coinSymbol": "cointoken", | ||
"coinStaticSymbol": "BTY", | ||
}, | ||
"transfer": { | ||
"name": { | ||
"cn": "转账", | ||
"en": "transfer" | ||
}, | ||
"coinAmount": "amount", | ||
"coinSymbol": "cointoken", | ||
"coinStaticSymbol": "BTY", | ||
} | ||
} | ||
}, | ||
"none": { | ||
"name": { | ||
"cn": "空交易", | ||
"en": "none", | ||
}, | ||
} | ||
}; | ||
exports.default = { | ||
@@ -196,4 +116,3 @@ txParser: txParser, | ||
hex2str: hex2str, | ||
str2hex: str2hex, | ||
txmap: txmap | ||
str2hex: str2hex | ||
}; |
{ | ||
"name": "@33cn/chain33-transaction-parser", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "解析交易", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -5,5 +5,13 @@ # 解析交易的行为和含义 | ||
```javascript | ||
import txParser from '@33cn/chain33-transaction-parser' | ||
console.log('txParser', txParser) | ||
npm install @33cn/chain33-transaction-parser | ||
import Parser from '@33cn/chain33-transaction-parser' | ||
使用例子: | ||
// 解析交易内容 | ||
var txParser = Parser.txParser(execer, payload, 'cn') | ||
// 解析交易内容并以字符串形式输出 | ||
var txInWords = Parser.txInWords(execer, payload, 'cn') | ||
// 16进制字符串转unicode字符串 | ||
var hex2str = Parser.hex2str(hex) | ||
// unicode字符串转16进制字符串 | ||
var str2hex = Parser.str2hex(str) | ||
``` |
156
src/index.ts
@@ -0,16 +1,12 @@ | ||
import txmap from './txmap' | ||
/** | ||
* 辅助解析交易的含义 | ||
* | ||
* 第一层的key 取自 execer | ||
* actions 取自 payload payload[action] | ||
* | ||
* coinAmount 为币的数量所在字段 payload[action][coinAmount] | ||
* 若为空 则为0 | ||
* coinSymbol 为币的代称所在字段 payload[action][coinSymbol] | ||
* 若为空 则取 coinStaticSymbol | ||
* | ||
* coinStaticSymbol 为默认币种名称 | ||
* | ||
*/ | ||
const txParser = function (execer: any, payload, lang="cn") { | ||
* @description 解析交易内容 | ||
* @param {string} execer 执行器 | ||
* @param {any} payload 交易内容 | ||
* @param {string} [lang='cn'] 解析后语言选择, 默认是中文 | ||
* @returns {object} | ||
*/ | ||
const txParser = function (execer: string, payload: any, lang: string = 'cn') { | ||
if (/^user\./.test(execer)) return { execerName: execer, action: {} } | ||
@@ -50,26 +46,26 @@ if (/^user\.p\./.test(execer)) return { execerName: '平行链', action: {} } | ||
} | ||
return { execerName, action } | ||
}; | ||
/** | ||
* | ||
* 输出交易信息为字符串 | ||
* @export | ||
* @param {*} execer | ||
* @param {*} payload | ||
* @param {string} [lang='cn'] | ||
* @returns | ||
} | ||
/** | ||
* @description 解析交易内容 | ||
* @param {string} execer 执行器 | ||
* @param {any} payload 交易内容 | ||
* @param {string} [lang='cn'] 解析后语言选择, 默认是中文 | ||
* @returns {string} 输出信息为字符串 | ||
*/ | ||
const txInWords = function (execer: any, payload: object, lang = 'cn') { | ||
const txInWords = function (execer: string, payload: any, lang: string = 'cn'): string { | ||
let { execerName, action } = txParser(execer, payload, lang) | ||
return `${execerName} ${JSON.stringify(action)}` | ||
} | ||
} | ||
/** | ||
* 16进制字符串转unicode字符串 | ||
* | ||
* @param {string} [hex=''] | ||
* @returns | ||
*/ | ||
const hex2str = function (hex = '') : string { | ||
/** | ||
* @description 16进制字符串转unicode字符串 | ||
* @param {string} [hex=''] 16进制字符串 | ||
* @returns {string} unicode字符串 | ||
*/ | ||
const hex2str = function (hex:string = '') { | ||
if (typeof hex !== 'string') throw new Error('hex2str required a string') | ||
@@ -84,4 +80,4 @@ hex = hex.trim() | ||
} | ||
let curCharCode : number | ||
let resCharCode : string | ||
let curCharCode:number | ||
let resCharCode:string | ||
let resultStr = [] | ||
@@ -99,12 +95,10 @@ for (var i = 0; i < len; i = i + 2) { | ||
/** | ||
* unicode字符串转16进制字符串 | ||
* | ||
* @export | ||
* @param {*} str | ||
* @returns | ||
*/ | ||
const str2hex = function (str : string) : string { | ||
* @description unicode字符串转16进制字符串 | ||
* @param {string} [hex=''] unicode字符串 | ||
* @returns {string} 16进制字符串 | ||
*/ | ||
const str2hex = function (str:string) { | ||
if (typeof str !== 'string') throw new Error('str2hex required a string') | ||
let hexArr: Array<string> = ['0x'] | ||
const len: number = str.length | ||
let hexArr = ['0x'] | ||
const len = str.length | ||
for (let i = 0; i < len; i++) { | ||
@@ -116,73 +110,2 @@ hexArr.push(str.charCodeAt(i).toString(16)) | ||
const txmap = { | ||
"ticket": { | ||
"name": { | ||
"cn": "购票", | ||
"en": "ticket" | ||
}, | ||
"actions": { | ||
"miner": { | ||
"name": { | ||
"cn": "挖矿", | ||
"en": "miner" | ||
}, | ||
"coinAmount": "reward", | ||
"coinSymbol": null, | ||
"coinStaticSymbol": "BTY", | ||
}, | ||
"topen": { | ||
"name": { | ||
"cn": "开启挖矿", | ||
"en": "topen" | ||
}, | ||
"coinAmount": "reward", | ||
"coinSymbol": null, | ||
"coinStaticSymbol": "BTY", | ||
}, | ||
"tclose": { | ||
"name": { | ||
"cn": "关闭挖矿", | ||
"en": "tclose" | ||
}, | ||
"coinAmount": "reward", | ||
"coinSymbol": null, | ||
"coinStaticSymbol": "BTY", | ||
}, | ||
} | ||
}, | ||
"coins": { | ||
"name": { | ||
"cn": "币", | ||
"en": "coins" | ||
}, | ||
"actions": { | ||
"withdraw": { | ||
"name": { | ||
"cn": "提币", | ||
"en": "withdraw" | ||
}, | ||
"coinAmount": "amount", | ||
"coinSymbol": "cointoken", | ||
"coinStaticSymbol": "BTY", | ||
}, | ||
"transfer": { | ||
"name": { | ||
"cn": "转账", | ||
"en": "transfer" | ||
}, | ||
"coinAmount": "amount", | ||
"coinSymbol": "cointoken", | ||
"coinStaticSymbol": "BTY", | ||
} | ||
} | ||
}, | ||
"none": { | ||
"name": { | ||
"cn": "空交易", | ||
"en": "none", | ||
}, | ||
} | ||
} | ||
export default { | ||
@@ -192,4 +115,3 @@ txParser, | ||
hex2str, | ||
str2hex, | ||
txmap | ||
} | ||
str2hex | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12299
7
398
16
1