@33cn/chain33-transaction-parser
Advanced tools
Comparing version 1.0.2 to 1.1.2
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var txmap_1 = require("./txmap"); | ||
var tools = require("./tools"); | ||
/** | ||
@@ -11,3 +12,3 @@ * @description 解析交易内容 | ||
*/ | ||
var txParser = function (execer, payload, lang) { | ||
function txParser(execer, payload, lang) { | ||
if (lang === void 0) { lang = 'cn'; } | ||
@@ -49,3 +50,4 @@ if (/^user\./.test(execer)) | ||
return { execerName: execerName, action: action }; | ||
}; | ||
} | ||
exports.txParser = txParser; | ||
/** | ||
@@ -58,56 +60,24 @@ * @description 解析交易内容 | ||
*/ | ||
var txInWords = function (execer, payload, lang) { | ||
function txInWords(execer, payload, lang) { | ||
if (lang === void 0) { lang = 'cn'; } | ||
var _a = txParser(execer, payload, lang), execerName = _a.execerName, action = _a.action; | ||
return execerName + " " + JSON.stringify(action); | ||
}; | ||
} | ||
exports.txInWords = txInWords; | ||
/** | ||
* @description 16进制字符串转unicode字符串 | ||
* @param {string} [hex=''] 16进制字符串 | ||
* @returns {string} unicode字符串 | ||
* 转换交易中的note字段 | ||
* 服务器端考虑到有些 note 是 二进制的 | ||
* protocol buffer 无法保存 非 utf-8 的字符串,所以采用的格式是 bytes | ||
* 这样,json 自动转成了 hex 格式 | ||
* @export | ||
* @param {string} note | ||
* @returns {string} | ||
*/ | ||
var hex2str = function (hex) { | ||
if (hex === void 0) { hex = ''; } | ||
if (typeof hex !== 'string') | ||
throw new Error('hex2str required a string'); | ||
hex = hex.trim(); | ||
if (hex.substr(0, 2).toLowerCase() === "0x") { | ||
hex = hex.substr(2); | ||
function parseTransferNote(note) { | ||
// 低版本的区块链节点的note字段不需要转换 | ||
if (!/^0\x/.test(note)) { | ||
return note; | ||
} | ||
var len = hex.length; | ||
if (len % 2 !== 0) { | ||
throw new Error('Illegal Format ASCII Code!'); | ||
} | ||
var curCharCode; | ||
var resCharCode; | ||
var resultStr = []; | ||
for (var i = 0; i < len; i = i + 2) { | ||
// 16进制转10进制 | ||
curCharCode = parseInt(hex.substr(i, 2), 16); | ||
// 10进制数字转unicode字符 | ||
resCharCode = String.fromCharCode(curCharCode); | ||
resultStr.push(resCharCode); | ||
} | ||
return resultStr.join(''); | ||
}; | ||
/** | ||
* @description unicode字符串转16进制字符串 | ||
* @param {string} [hex=''] unicode字符串 | ||
* @returns {string} 16进制字符串 | ||
*/ | ||
var str2hex = function (str) { | ||
if (typeof str !== 'string') | ||
throw new Error('str2hex required a string'); | ||
var hexArr = ['0x']; | ||
var len = str.length; | ||
for (var i = 0; i < len; i++) { | ||
hexArr.push(str.charCodeAt(i).toString(16)); | ||
} | ||
return hexArr.join(''); | ||
}; | ||
exports.default = { | ||
txParser: txParser, | ||
txInWords: txInWords, | ||
hex2str: hex2str, | ||
str2hex: str2hex | ||
}; | ||
return tools.utf82rstr(tools.hex2str(note)); | ||
} | ||
exports.parseTransferNote = parseTransferNote; |
@@ -62,3 +62,12 @@ "use strict"; | ||
"coinStaticSymbol": "BTY", | ||
} | ||
}, | ||
"transferToExec": { | ||
"name": { | ||
"cn": "transferToExec", | ||
"en": "transferToExec" | ||
}, | ||
"coinAmount": "amount", | ||
"coinSymbol": "cointoken", | ||
"coinStaticSymbol": "BTY", | ||
}, | ||
} | ||
@@ -65,0 +74,0 @@ }, |
{ | ||
"name": "@33cn/chain33-transaction-parser", | ||
"version": "1.0.2", | ||
"version": "1.1.2", | ||
"description": "解析交易", | ||
"main": "build/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"test": "mocha -r ts-node/register ./test/*test.ts", | ||
"build": "tsc" | ||
@@ -16,5 +16,15 @@ }, | ||
"keywords": [ | ||
"chain33" | ||
"chain33", | ||
"transaction parser" | ||
], | ||
"license": "ISC" | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.12.17", | ||
"mocha": "^5.2.0", | ||
"ts-node": "^7.0.1", | ||
"tslint": "^5.11.0", | ||
"typescript": "^3.1.6", | ||
"should": "^13.2.3" | ||
} | ||
} |
import txmap from './txmap' | ||
import * as tools from './tools' | ||
interface TxAction { | ||
execerName: string, | ||
action: object | ||
} | ||
/** | ||
@@ -11,3 +16,3 @@ * @description 解析交易内容 | ||
const txParser = function (execer: string, payload: any, lang: string = 'cn') { | ||
export function txParser (execer: string, payload: any, lang: string = 'cn'): TxAction { | ||
if (/^user\./.test(execer)) return { execerName: execer, action: {} } | ||
@@ -59,3 +64,3 @@ if (/^user\.p\./.test(execer)) return { execerName: '平行链', action: {} } | ||
const txInWords = function (execer: string, payload: any, lang: string = 'cn'): string { | ||
export function txInWords (execer: string, payload: any, lang: string = 'cn'): string { | ||
let { execerName, action } = txParser(execer, payload, lang) | ||
@@ -66,50 +71,17 @@ | ||
/** | ||
* @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') | ||
hex = hex.trim() | ||
if (hex.substr(0, 2).toLowerCase() === "0x") { | ||
hex = hex.substr(2) | ||
} | ||
const len = hex.length; | ||
if (len % 2 !== 0) { | ||
throw new Error('Illegal Format ASCII Code!') | ||
} | ||
let curCharCode:number | ||
let resCharCode:string | ||
let resultStr = [] | ||
for (var i = 0; i < len; i = i + 2) { | ||
// 16进制转10进制 | ||
curCharCode = parseInt(hex.substr(i, 2), 16) | ||
// 10进制数字转unicode字符 | ||
resCharCode = String.fromCharCode(curCharCode) | ||
resultStr.push(resCharCode) | ||
} | ||
return resultStr.join(''); | ||
} | ||
/** | ||
* @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 = ['0x'] | ||
const len = str.length | ||
for (let i = 0; i < len; i++) { | ||
hexArr.push(str.charCodeAt(i).toString(16)) | ||
* 转换交易中的note字段 | ||
* 服务器端考虑到有些 note 是 二进制的 | ||
* protocol buffer 无法保存 非 utf-8 的字符串,所以采用的格式是 bytes | ||
* 这样,json 自动转成了 hex 格式 | ||
* @export | ||
* @param {string} note | ||
* @returns {string} | ||
*/ | ||
export function parseTransferNote (note: string): string { | ||
// 低版本的区块链节点的note字段不需要转换 | ||
if (!/^0\x/.test(note)) { | ||
return note | ||
} | ||
return hexArr.join('') | ||
return tools.utf82rstr(tools.hex2str(note)) | ||
} | ||
export default { | ||
txParser, | ||
txInWords, | ||
hex2str, | ||
str2hex | ||
} |
@@ -61,3 +61,12 @@ export default { | ||
"coinStaticSymbol": "BTY", | ||
} | ||
}, | ||
"transferToExec": { | ||
"name": { | ||
"cn": "transferToExec", | ||
"en": "transferToExec" | ||
}, | ||
"coinAmount": "amount", | ||
"coinSymbol": "cointoken", | ||
"coinStaticSymbol": "BTY", | ||
}, | ||
} | ||
@@ -64,0 +73,0 @@ }, |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
20614
10
709
1
7