Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@33cn/chain33-transaction-parser

Package Overview
Dependencies
Maintainers
4
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@33cn/chain33-transaction-parser - npm Package Compare versions

Comparing version 1.1.2 to 1.1.3

2

build/index.js

@@ -43,3 +43,3 @@ "use strict";

name: actionmap['name'][lang],
amount: actionmap["coinAmount"] ? payload[actionKey][actionmap["coinAmount"]] : 0,
amount: actionmap["coinAmount"] ? Number(payload[actionKey][actionmap["coinAmount"]]) : 0,
coin: actionmap["coinSymbol"] ? payload[actionKey][actionmap["coinSymbol"]] : actionmap["coinStaticSymbol"]

@@ -46,0 +46,0 @@ };

{
"name": "@33cn/chain33-transaction-parser",
"version": "1.1.2",
"version": "1.1.3",
"description": "解析交易",

@@ -14,3 +14,6 @@ "main": "build/index.js",

},
"author": "cristilyc",
"contributors": [
"cristilyc",
"zoeying <zoexxying@gmail.com> (http://github.zoe.gift/)"
],
"keywords": [

@@ -17,0 +20,0 @@ "chain33",

@@ -6,12 +6,51 @@ # 解析交易的行为和含义

npm install @33cn/chain33-transaction-parser
import Parser from '@33cn/chain33-transaction-parser'
使用例子:
import { txParser, txInWords, parseTransferNote } from '@33cn/chain33-transaction-parser'
```
确定交易的含义,主要由执行器execer的类别,以及传参payload来确定
```
txmap.ts 文件中包含对execer的翻译以及payload中代表价值的字段判断
```
栗子
```javascript
const execer = 'ticket'
const payload = {
"miner": {
"bits": 508549505,
"reward": "1800000000",
"ticketId": "1bgg6HwQretMiVcSWvayPRvVtwjyKfz1J:0x5b7a322ab74e65d7482ad1d6877a23de98e0f6b5c15d5d92bb3dcbfed5be89f5:0000000000",
"modify": "0x307837306161376436623761326335646362376366643438316464383065663838323437616338656564326135383233636139613862663763633562376238633130",
"privHash": null
},
"ty": 16
}
// 解析交易内容
var txParser = Parser.txParser(execer, payload, 'cn')
// 解析交易内容并以字符串形式输出
var txInWords = Parser.txInWords(execer, payload, 'cn')
const { execerName, action } = txParser(execer, payload, 'cn')
// execerName -> 购票
// action -> {"name":"挖矿","amount":1800000000,"coin":"BTY"}
// 解析交易内容并以字符串形式输出
var words = txInWords(execer, payload, 'cn')
// words -> '购票 {"name":"挖矿","amount":1800000000,"coin":"BTY"}'
```
6.0.2开始 转账的相关交易中note字段底层在处理时会以 byte[] 形式储存
展示时一般需要转换为utf8
```javascript
parseTransferNote('0xe5ae89e585a8e694afe587bae8b4b9e794a83a20313030303030')
// 安全支出费用: 100000
```
tools.ts 中提供了各种格式字符串的转换
```javascript
// 16进制字符串转unicode字符串
var hex2str = Parser.hex2str(hex)
var hex2str = hex2str(hex)
// unicode字符串转16进制字符串
var str2hex = Parser.str2hex(str)
var str2hex = str2hex(str)
// ... 等等 详情参见tools.ts
```

@@ -6,3 +6,7 @@ import txmap from './txmap'

execerName: string,
action: object
action: {
name? :string,
amount? : number,
coin? : number
}
}

@@ -47,3 +51,3 @@ /**

name: actionmap['name'][lang],
amount: actionmap["coinAmount"] ? payload[actionKey][actionmap["coinAmount"]] : 0,
amount: actionmap["coinAmount"] ? Number(payload[actionKey][actionmap["coinAmount"]]) : 0,
coin: actionmap["coinSymbol"] ? payload[actionKey][actionmap["coinSymbol"]] : actionmap["coinStaticSymbol"]

@@ -53,3 +57,2 @@ }

}
return { execerName, action }

@@ -68,3 +71,2 @@ }

let { execerName, action } = txParser(execer, payload, lang)
return `${execerName} ${JSON.stringify(action)}`

@@ -71,0 +73,0 @@ }

@@ -7,4 +7,48 @@ /**

import * as should from 'should';
import { parseTransferNote } from '../src/index'
import { parseTransferNote, txParser, txInWords } from '../src/index'
describe('txInWords deal with a lot of transactions', () => {
const execer = 'ticket'
const payload = {
"miner": {
"bits": 508549505,
"reward": "1800000000",
"ticketId": "1bgg6HwQretMiVcSWvayPRvVtwjyKfz1J:0x5b7a322ab74e65d7482ad1d6877a23de98e0f6b5c15d5d92bb3dcbfed5be89f5:0000000000",
"modify": "0x307837306161376436623761326335646362376366643438316464383065663838323437616338656564326135383233636139613862663763633562376238633130",
"privHash": null
},
"ty": 16
}
it('txInwords should return a string', () => {
const res = txInWords(execer, payload, 'cn')
console.log(res)
should(res).be.String()
})
})
describe('txParser deal with a transaction of miner', () => {
const execer = 'ticket'
const payload = {
"miner": {
"bits": 508549505,
"reward": "1800000000",
"ticketId": "1bgg6HwQretMiVcSWvayPRvVtwjyKfz1J:0x5b7a322ab74e65d7482ad1d6877a23de98e0f6b5c15d5d92bb3dcbfed5be89f5:0000000000",
"modify": "0x307837306161376436623761326335646362376366643438316464383065663838323437616338656564326135383233636139613862663763633562376238633130",
"privHash": null
},
"ty": 16
}
const { execerName, action } = txParser(execer, payload)
it('the execerName should be a string' ,() => {
should(execerName).be.String()
})
it('the action should has three properties: name: string, amount: number, coin: string' ,() => {
should(Object.keys(action)).be.eql(['name', 'amount','coin'])
should(action.name).be.String()
should(action.amount).be.Number()
should(action.coin).be.String()
})
})
describe('the hexadecimal should be parse into utf-8 chars', () => {

@@ -11,0 +55,0 @@ it('"0xe5ae89e585a8e694afe587bae8b4b9e794a83a20313030303030" should be " 安全支出费用: 100000"', () => {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc