@ocap/message 1.6.5 → 1.6.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
4
4
  [![docs](https://img.shields.io/badge/powered%20by-arcblock-green.svg)](https://docs.arcblock.io)
5
- [![Gitter](https://badges.gitter.im/ArcBlock/community.svg)](https://gitter.im/ArcBlock/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
5
+ [![Gitter](https://badges.gitter.im/ArcBlock/community.svg)](https://gitter.im/ArcBlock/community?utm_source=badge\&utm_medium=badge\&utm_campaign=pr-badge)
6
6
 
7
7
  > Utility function to encode and format message that can be sent to or received from forge framework.
8
8
 
@@ -34,10 +34,9 @@ const message = createMessage('Transaction', {
34
34
  nonce: 0,
35
35
  signature: 'abc',
36
36
  itx: {
37
- type: 'PokeTx',
37
+ type: 'TransferTx',
38
38
  value: {
39
- date: '2019-04-25',
40
- address: 'zzzzzzzzzzzzzzzzzzzzz',
39
+ to: '2019-04-25',
41
40
  },
42
41
  },
43
42
  });
@@ -49,11 +48,11 @@ const buffer = message.serializeBinary();
49
48
 
50
49
  ## Documentation
51
50
 
52
- For full documentation, checkout [https://ocap-js.netlify.com](https://ocap-js.netlify.com/)
51
+ For full documentation, checkout [https://asset-chain.netlify.com](https://asset-chain.netlify.com/)
53
52
 
54
53
 
55
54
  ## Contributors
56
55
 
57
- | Name | Website |
58
- | -------------- | -------------------------- |
59
- | **wangshijun** | <https://ocap.arcblock.io> |
56
+ | Name | Website |
57
+ | -------------- | ------------------------------- |
58
+ | **wangshijun** | <https://github.com/wangshijun> |
package/index.d.ts CHANGED
@@ -1,166 +1,5 @@
1
1
  // Generate by [js2dts@0.3.3](https://github.com/whxaxes/js2dts#readme)
2
2
 
3
- /**
4
- * Format an message from RPC to UI friendly
5
- *
6
- * @public
7
- * @static
8
- * @param {string} type - input type
9
- * @param {object} data - input data
10
- * @returns {object} [almost same structure as input]
11
- */
12
- declare function formatMessage(type: string, data: any): any;
13
- /**
14
- * Create an protobuf encoded Typed message with specified data, ready to send to rpc server
15
- *
16
- * @public
17
- * @static
18
- * @param {string} type - message type defined in forge-proto
19
- * @param {object} params - message content
20
- * @returns {object} Message instance
21
- * @example
22
- * const { createMessage } = require('@ocap/message');
23
- * const message = createMessage ('CreateAssetTx', {
24
- * moniker: 'asset',
25
- * address: 'zaAKEJRKQWsdfjksdfjkASRD',
26
- * });
27
- *
28
- * message.getMoniker(); // 'asset'
29
- * message.getAddress(); // 'zaAKEJRKQWsdfjksdfjkASRD'
30
- * message.getReadonly(); // false
31
- * message.setReadonly(true);
32
- */
33
- declare function createMessage(type: string, params: any): any;
34
- /**
35
- * Generated a fake message for a type, the message can be RPC request/response
36
- *
37
- * @public
38
- * @static
39
- * @param {string} type - Message type string, should be defined in forge-abi or forge-core-protocol
40
- * @returns {object}
41
- * @example
42
- * const { fakeMessage} = require('@ocap/message');
43
- * const message = fakeMessage('CreateAssetTx');
44
- * // will output
45
- * {
46
- * moniker: 'arcblock',
47
- * data: { type: 'string', value: 'ABCD 1234' },
48
- * readonly: true,
49
- * transferrable: true,
50
- * ttl: 2,
51
- * parent: 'arcblock',
52
- * address: 'F2D072CBD4954A20F26280730795D91AC1039996CEB6E24A31E9CE548DCB5E55',
53
- * }
54
- */
55
- declare function fakeMessage(type: string): any;
56
- /**
57
- * Decode an google.protobuf.Any%{ typeUrl, value } => { type, value }
58
- *
59
- * @public
60
- * @static
61
- * @param {object} data encoded data object
62
- * @returns {object} Object%{type, value}
63
- */
64
- declare function decodeAny(data: any): any;
65
- /**
66
- * Encode { type, value } => google.protobuf.Any%{ typeUrl, value }
67
- * Does nothing on already encoded message
68
- *
69
- * @public
70
- * @static
71
- * @param {object} data
72
- * @returns {object} google.protobuf.Any
73
- */
74
- declare function encodeAny(data: any): any;
75
- /**
76
- * Convert an { seconds, nanos } | date-string to google.protobuf.Timestamp object
77
- *
78
- * @public
79
- * @static
80
- * @param {string|object} value
81
- * @returns {object} instanceof google.protobuf.Timestamp
82
- */
83
- declare function encodeTimestamp(value: any): any;
84
- /**
85
- * Decode google.protobuf.Timestamp message to ISO Date String
86
- *
87
- * FIXME: node strictly equal because we rounded the `nanos` field
88
- *
89
- * @public
90
- * @static
91
- * @param {object} data
92
- * @returns {strong} String timestamp
93
- */
94
- declare function decodeTimestamp(data: any): any;
95
- /**
96
- * Encode BigUint and BigSint types defined in forge-sdk, double encoding is avoided
97
- *
98
- * @public
99
- * @static
100
- * @param {buffer|string|number} value - value to encode
101
- * @param {string} type - type names defined in forge-proto
102
- * @returns {object} Message
103
- */
104
- declare function encodeBigInt(value: any, type: string): any;
105
- /**
106
- * Convert BigUint and BigSint to string representation of numbers
107
- *
108
- * @public
109
- * @static
110
- * @link https://stackoverflow.com/questions/23948278/how-to-convert-byte-array-into-a-signed-big-integer-in-javascript
111
- * @param {object} data - usually from encodeBigInt
112
- * @param {buffer} data.value
113
- * @param {boolean} data.minus
114
- * @returns {string} human readable number
115
- */
116
- declare function decodeBigInt(data: _OcapMessage.T100): string;
117
- /**
118
- * Attach an $format method to rpc response
119
- *
120
- * @private
121
- * @param {object} data
122
- * @param {string} type
123
- */
124
- declare function attachFormatFn(type: string, data: any, key?: string): void;
125
- /**
126
- * Attach an example method to
127
- *
128
- * @private
129
- * @param {object} data
130
- * @param {string} type
131
- */
132
- declare function attachExampleFn(type: string, host: any, key: any): void;
133
- /**
134
- * Add type provider that can be used to format/create messages
135
- *
136
- * @param {object} provider - proto generated from {@see @ocap/proto}
137
- */
138
- declare function addProvider(provider: any): void;
139
- declare function getMessageType(type: any): any;
140
- declare function toTypeUrl(type: any): any;
141
- declare function fromTypeUrl(url: any): any;
142
- declare const _OcapMessage: _OcapMessage.T101;
143
- declare namespace _OcapMessage {
144
- export interface T100 {
145
- value: any;
146
- minus: boolean;
147
- }
148
- export interface T101 {
149
- formatMessage: typeof formatMessage;
150
- createMessage: typeof createMessage;
151
- fakeMessage: typeof fakeMessage;
152
- decodeAny: typeof decodeAny;
153
- encodeAny: typeof encodeAny;
154
- encodeTimestamp: typeof encodeTimestamp;
155
- decodeTimestamp: typeof decodeTimestamp;
156
- encodeBigInt: typeof encodeBigInt;
157
- decodeBigInt: typeof decodeBigInt;
158
- attachFormatFn: typeof attachFormatFn;
159
- attachExampleFn: typeof attachExampleFn;
160
- addProvider: typeof addProvider;
161
- getMessageType: typeof getMessageType;
162
- toTypeUrl: typeof toTypeUrl;
163
- fromTypeUrl: typeof fromTypeUrl;
164
- }
165
- }
3
+ import * as LibMessage from './lib/message';
4
+ declare const _OcapMessage: typeof LibMessage;
166
5
  export = _OcapMessage;
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Format an message from RPC to UI friendly
3
+ *
4
+ * @public
5
+ * @static
6
+ * @param {string} type - input type
7
+ * @param {object} data - input data
8
+ * @returns {object} [almost same structure as input]
9
+ */
10
+ export function formatMessage(type: string, data: object): object;
11
+ /**
12
+ * Create an protobuf encoded Typed message with specified data, ready to send to rpc server
13
+ *
14
+ * @public
15
+ * @static
16
+ * @param {string} type - message type defined in forge-proto
17
+ * @param {object} params - message content
18
+ * @returns {object} Message instance
19
+ * @example
20
+ * const { createMessage } = require('@ocap/message');
21
+ * const message = createMessage ('CreateAssetTx', {
22
+ * moniker: 'asset',
23
+ * address: 'zaAKEJRKQWsdfjksdfjkASRD',
24
+ * });
25
+ *
26
+ * message.getMoniker(); // 'asset'
27
+ * message.getAddress(); // 'zaAKEJRKQWsdfjksdfjkASRD'
28
+ * message.getReadonly(); // false
29
+ * message.setReadonly(true);
30
+ */
31
+ export function createMessage(type: string, params: object): object;
32
+ /**
33
+ * Generated a fake message for a type, the message can be RPC request/response
34
+ *
35
+ * @public
36
+ * @static
37
+ * @param {string} type - Message type string, should be defined in forge-abi or forge-core-protocol
38
+ * @returns {object}
39
+ * @example
40
+ * const { fakeMessage} = require('@ocap/message');
41
+ * const message = fakeMessage('CreateAssetTx');
42
+ * // will output
43
+ * {
44
+ * moniker: 'arcblock',
45
+ * data: { type: 'string', value: 'ABCD 1234' },
46
+ * readonly: true,
47
+ * transferrable: true,
48
+ * ttl: 2,
49
+ * parent: 'arcblock',
50
+ * address: 'F2D072CBD4954A20F26280730795D91AC1039996CEB6E24A31E9CE548DCB5E55',
51
+ * }
52
+ */
53
+ export function fakeMessage(type: string): object;
54
+ /**
55
+ * Decode an google.protobuf.Any%{ typeUrl, value } => { type, value }
56
+ *
57
+ * @public
58
+ * @static
59
+ * @param {object} data encoded data object
60
+ * @returns {object} Object%{type, value}
61
+ */
62
+ export function decodeAny(data: object): object;
63
+ /**
64
+ * Encode { type, value } => google.protobuf.Any%{ typeUrl, value }
65
+ * Does nothing on already encoded message
66
+ *
67
+ * @public
68
+ * @static
69
+ * @param {object} data
70
+ * @returns {object} google.protobuf.Any
71
+ */
72
+ export function encodeAny(data: object): object;
73
+ /**
74
+ * Convert an { seconds, nanos } | date-string to google.protobuf.Timestamp object
75
+ *
76
+ * @public
77
+ * @static
78
+ * @param {string|object} value
79
+ * @returns {object} instanceof google.protobuf.Timestamp
80
+ */
81
+ export function encodeTimestamp(value: string | object): object;
82
+ /**
83
+ * Decode google.protobuf.Timestamp message to ISO Date String
84
+ *
85
+ * FIXME: node strictly equal because we rounded the `nanos` field
86
+ *
87
+ * @public
88
+ * @static
89
+ * @param {object} data
90
+ * @returns {strong} String timestamp
91
+ */
92
+ export function decodeTimestamp(data: object): strong;
93
+ /**
94
+ * Encode BigUint and BigSint types defined in forge-sdk, double encoding is avoided
95
+ *
96
+ * @public
97
+ * @static
98
+ * @param {buffer|string|number} value - value to encode
99
+ * @param {string} type - type names defined in forge-proto
100
+ * @returns {object} Message
101
+ */
102
+ export function encodeBigInt(value: buffer | string | number, type: string): object;
103
+ /**
104
+ * Convert BigUint and BigSint to string representation of numbers
105
+ *
106
+ * @public
107
+ * @static
108
+ * @link https://stackoverflow.com/questions/23948278/how-to-convert-byte-array-into-a-signed-big-integer-in-javascript
109
+ * @param {object} data - usually from encodeBigInt
110
+ * @param {buffer} data.value
111
+ * @param {boolean} data.minus
112
+ * @returns {string} human readable number
113
+ */
114
+ export function decodeBigInt(data: {
115
+ value: buffer;
116
+ minus: boolean;
117
+ }): string;
118
+ /**
119
+ * Attach an $format method to rpc response
120
+ *
121
+ * @private
122
+ * @param {object} data
123
+ * @param {string} type
124
+ */
125
+ export function attachFormatFn(type: string, data: object, key?: string): void;
126
+ /**
127
+ * Attach an example method to
128
+ *
129
+ * @private
130
+ * @param {object} data
131
+ * @param {string} type
132
+ */
133
+ export function attachExampleFn(type: string, host: any, key: any): void;
134
+ import { addProvider } from "./provider";
135
+ import { getMessageType } from "./provider";
136
+ import { toTypeUrl } from "./provider";
137
+ import { fromTypeUrl } from "./provider";
138
+ export { addProvider, getMessageType, toTypeUrl, fromTypeUrl };
package/lib/message.js CHANGED
@@ -54,7 +54,7 @@ const fakeValues = {
54
54
  double: '12.3',
55
55
 
56
56
  string: 'arcblock',
57
- bytes: Uint8Array.from([]),
57
+ bytes: new Uint8Array(),
58
58
  enums: (type) => Object.values(enums[type])[0],
59
59
  };
60
60
 
@@ -393,9 +393,14 @@ function decodeAny(data) {
393
393
  return { type: 'Unknown', value: '' };
394
394
  }
395
395
 
396
+ if (data.type && data.value) {
397
+ return data;
398
+ }
399
+
396
400
  const { typeUrl, value } = data;
397
401
  if (['json', 'vc'].includes(typeUrl)) {
398
- return { type: typeUrl, value: JSON.parse(Buffer.from(value, 'base64')) };
402
+ const decoded = JSON.parse(Buffer.from(value, 'base64'));
403
+ return { type: typeUrl, value: decoded };
399
404
  }
400
405
 
401
406
  const type = fromTypeUrl(typeUrl);
@@ -431,16 +436,16 @@ function encodeAny(data) {
431
436
  if (data.typeUrl === 'fg:x:address') {
432
437
  anyMessage.setValue(data.value);
433
438
  } else if (['json', 'vc'].includes(data.typeUrl)) {
434
- anyMessage.setValue(Uint8Array.from(Buffer.from(JSON.stringify(data.value))));
439
+ anyMessage.setValue(new Uint8Array(Buffer.from(JSON.stringify(data.value))));
435
440
  } else {
436
- anyMessage.setValue(Uint8Array.from(Buffer.from(data.value, 'base64')));
441
+ anyMessage.setValue(new Uint8Array(Buffer.from(data.value, 'base64')));
437
442
  }
438
443
  } else {
439
444
  const { value: anyValue, type: anyType } = data;
440
445
  const typeUrl = toTypeUrl(anyType);
441
446
  anyMessage.setTypeUrl(typeUrl);
442
447
  if (['json', 'vc'].includes(typeUrl)) {
443
- anyMessage.setValue(Uint8Array.from(Buffer.from(JSON.stringify(anyValue))));
448
+ anyMessage.setValue(new Uint8Array(Buffer.from(JSON.stringify(anyValue))));
444
449
  } else {
445
450
  const anyValueBinary = createMessage(anyType, anyValue);
446
451
  anyMessage.setValue(anyValueBinary.serializeBinary());
@@ -526,7 +531,7 @@ function encodeBigInt(value, type) {
526
531
 
527
532
  const number = toBN(value);
528
533
  const zero = toBN(0);
529
- message.setValue(Uint8Array.from(number.toArray()));
534
+ message.setValue(new Uint8Array(number.toArray()));
530
535
  if (type === 'BigSint') {
531
536
  message.setMinus(number.lt(zero));
532
537
  }
package/lib/patch.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Add type provider that can be used to format/create messages
3
+ *
4
+ * @param {object} provider - proto generated from {@see @ocap/proto}
5
+ */
6
+ export function addProvider(provider: object): void;
7
+ export function resetProviders(): void;
8
+ export const enums: {};
9
+ export const messages: {};
10
+ export function getMessageType(type: any): any;
11
+ export function toTypeUrl(type: any): any;
12
+ export function fromTypeUrl(url: any): any;
package/package.json CHANGED
@@ -1,46 +1,47 @@
1
1
  {
2
2
  "name": "@ocap/message",
3
3
  "description": "Utility functions to encode and decode message that can send to forge",
4
- "version": "1.6.5",
4
+ "version": "1.6.10",
5
5
  "author": {
6
6
  "name": "wangshijun",
7
7
  "email": "shijun@arcblock.io",
8
8
  "url": "https://github.com/wangshijun"
9
9
  },
10
+ "contributors": [
11
+ "wangshijun <shijun@arcblock.io> (https://github.com/wangshijun)"
12
+ ],
10
13
  "bugs": {
11
- "url": "https://github.com/ArcBlock/ocap-js/issues",
14
+ "url": "https://github.com/ArcBlock/asset-chain/issues",
12
15
  "email": "shijun@arcblock.io"
13
16
  },
14
17
  "publishConfig": {
15
18
  "access": "public"
16
19
  },
17
20
  "dependencies": {
18
- "@ocap/proto": "^1.6.5",
19
- "@ocap/util": "^1.6.5",
20
- "debug": "^4.3.1",
21
- "google-protobuf": "^3.9.0",
21
+ "@ocap/proto": "1.6.10",
22
+ "@ocap/util": "1.6.10",
23
+ "debug": "^4.3.3",
24
+ "google-protobuf": "3.18.0",
22
25
  "lodash": "^4.17.21"
23
26
  },
24
27
  "devDependencies": {
25
- "jest": "^26.6.3",
26
- "jsdoc-to-markdown": "^5.0.0",
27
- "remark-cli": "^5.0.0",
28
- "remark-preset-github": "^0.0.9"
28
+ "jest": "^27.3.1",
29
+ "jsdoc-to-markdown": "^7.1.1",
30
+ "remark-cli": "^10.0.1",
31
+ "remark-preset-github": "^4.0.1"
29
32
  },
30
33
  "remarkConfig": {
31
34
  "plugins": [
32
35
  "preset-github",
33
36
  [
34
- "validate-links",
35
37
  {
36
- "repository": "ArcBlock/ocap-js"
38
+ "repository": "ArcBlock/asset-chain"
37
39
  }
38
40
  ]
39
41
  ]
40
42
  },
41
- "homepage": "https://github.com/ArcBlock/ocap-js/tree/master/core/forge-message",
43
+ "homepage": "https://github.com/ArcBlock/asset-chain/tree/master/core/forge-message",
42
44
  "keywords": [
43
- "forge",
44
45
  "blockchain",
45
46
  "arcblock",
46
47
  "sdk",
@@ -55,18 +56,18 @@
55
56
  ],
56
57
  "repository": {
57
58
  "type": "git",
58
- "url": "https://github.com/ArcBlock/ocap-js/tree/master/core/forge-message"
59
+ "url": "https://github.com/ArcBlock/asset-chain/tree/master/core/forge-message"
59
60
  },
60
61
  "scripts": {
61
62
  "lint": "eslint lib *.js tests",
62
63
  "lint:fix": "eslint --fix lib *.js tests",
63
64
  "docs": "yarn gen-dts && yarn gen-docs && yarn cleanup-docs && yarn format-docs",
64
- "cleanup-docs": "node ../../tools/cleanup-docs.js docs/README.md $npm_package_name",
65
+ "cleanup-docs": "node ../../scripts/cleanup-docs.js docs/README.md $npm_package_name",
65
66
  "gen-docs": "jsdoc2md lib/message.js > docs/README.md",
66
67
  "format-docs": "remark . -o",
67
68
  "gen-dts": "j2d index.js",
68
- "test": "node tools/jest.js",
69
+ "test": "jest --forceExit --detectOpenHandles",
69
70
  "coverage": "yarn test -- --coverage"
70
71
  },
71
- "gitHead": "5779448f13824de38978df3c84c9da0c1e1ad989"
72
+ "gitHead": "ab272e8db3a15c6571cc7fae7cc3d3e0fdd4bdb1"
72
73
  }