@arcblock/did-util 1.17.6 → 1.17.7

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.
Files changed (3) hide show
  1. package/lib/index.d.ts +14 -118
  2. package/lib/index.js +41 -166
  3. package/package.json +30 -38
package/lib/index.d.ts CHANGED
@@ -1,133 +1,29 @@
1
- // Generate by [js2dts@0.3.3](https://github.com/whxaxes/js2dts#readme)
2
-
3
- /**
4
- * Create an asset address
5
- *
6
- * @public
7
- * @static
8
- * @param {object} itx - an object of `CreateAssetTx`
9
- * @returns {string} asset address without `did:abt:` prefix
10
- */
11
- declare function toAssetAddress(itx: any): string;
12
- /**
13
- * Create an asset did
14
- *
15
- * @public
16
- * @static
17
- * @param {object} itx - an object of `CreateAssetTx`
18
- * @returns {string} asset address without `did:abt:` prefix
19
- */
20
- declare function toAssetDid(itx: any): string;
1
+ import { TCreateAssetTx, TCreateFactoryTx, TCreateRollupTx, TCreateTokenTx } from '@ocap/types';
21
2
  /**
22
3
  * Create an itx address
23
- *
24
- * @public
25
- * @static
26
- * @param {object} itx - an object of forge supported itx
27
- * @param {string} type - itx type string
28
- * @param {enum} [role=types.RoleType.ROLE_TX] - role type
29
- * @returns {string} itx address without `did:abt:` prefix
30
4
  */
31
- declare function toItxAddress(itx: any, type: string, role?: any): string;
5
+ export declare function toItxAddress(itx: object, type: string, role?: number): string;
32
6
  /**
33
- * Create an itx did
34
- *
35
- * @public
36
- * @static
37
- * @param {object} itx - an object of forge supported itx
38
- * @returns {string} itx address without `did:abt:` prefix
39
- */
40
- declare function toItxDid(itx: any, type: any): string;
41
- /**
42
- * Generate an stake address, eg: the did of the stake
43
- *
44
- * @public
45
- * @static
46
- * @param {string} sender - sender address
47
- * @param {string} receiver - receiver address
48
- * @returns {string} stake address without `did:abt:` prefix
49
- */
50
- declare function toStakeAddress(sender: string, receiver: string): string;
51
- /**
52
- * Generate an delegate address, eg: the did of the delegation
53
- *
54
- * @public
55
- * @static
56
- * @param {string} addr1 - delegator address
57
- * @param {string} addr2 - delegatee address
58
- * @returns {string} delegation address that can be used to retrieve delegation state
59
- */
60
- declare function toDelegateAddress(addr1: string, addr2: string): string;
61
- /**
62
- * Generate an stake address, eg: the did of the stake
63
- *
64
- * @public
65
- * @static
66
- * @param {string} sender - sender address
67
- * @param {string} receiver - receiver address
68
- * @returns {string} stake address without `did:abt:` prefix
7
+ * Create an asset address
69
8
  */
70
- declare function toStakeDid(sender: string, receiver: string): string;
9
+ export declare function toAssetAddress(itx: Partial<TCreateAssetTx>): string;
71
10
  /**
72
- * Generate a tether address from the deposit tether tx hash
73
- *
74
- * @public
75
- * @static
76
- * @param {string} hash - DepositTetherTx hash
77
- * @returns {string} stake address without `did:abt:` prefix
11
+ * Create an asset factory address
78
12
  */
79
- declare function toTetherAddress(hash: string): string;
13
+ export declare function toFactoryAddress(itx: Partial<TCreateFactoryTx>): string;
80
14
  /**
81
- * Generate a swap address from the setup swap tx hash
82
- *
83
- * @public
84
- * @static
85
- * @param {string} hash - SetupSwapTx hash
86
- * @returns {string} swap address without `did:abt:` prefix
15
+ * Create an token address
87
16
  */
88
- declare function toSwapAddress(hash: string): string;
17
+ export declare function toTokenAddress(itx: Partial<TCreateTokenTx>): string;
89
18
  /**
90
- * Create an token address
91
- *
92
- * @public
93
- * @static
94
- * @param {object} itx - an object of `CreateTokenTx`
95
- * @returns {string} token address without `did:abt:` prefix
19
+ * Create an rollup address
96
20
  */
97
- declare function toTokenAddress(itx: any): string;
21
+ export declare function toRollupAddress(itx: Partial<TCreateRollupTx>): string;
98
22
  /**
99
- * Create an factory address
100
- *
101
- * @public
102
- * @static
103
- * @param {object} itx - an object of `CreateFactoryTx`
104
- * @returns {string} factory address without `did:abt:` prefix
23
+ * Generate an stake address, eg: the did of the stake
105
24
  */
106
- declare function toFactoryAddress(itx: any): string;
25
+ export declare function toStakeAddress(sender: string, receiver: string): string;
107
26
  /**
108
- * Create an rollup address
109
- *
110
- * @public
111
- * @static
112
- * @param {object} itx - an object of `CreateRollupTx`
113
- * @returns {string} rollup address without `did:abt:` prefix
27
+ * Generate an delegate address, eg: the did of the delegation
114
28
  */
115
- declare function toRollupAddress(itx: any): string;
116
- declare const _Lib: _Lib.T100;
117
- declare namespace _Lib {
118
- export interface T100 {
119
- toAssetAddress: typeof toAssetAddress;
120
- toAssetDid: typeof toAssetDid;
121
- toItxAddress: typeof toItxAddress;
122
- toItxDid: typeof toItxDid;
123
- toStakeAddress: typeof toStakeAddress;
124
- toDelegateAddress: typeof toDelegateAddress;
125
- toStakeDid: typeof toStakeDid;
126
- toTetherAddress: typeof toTetherAddress;
127
- toSwapAddress: typeof toSwapAddress;
128
- toTokenAddress: typeof toTokenAddress;
129
- toFactoryAddress: typeof toFactoryAddress;
130
- toRollupAddress: typeof toRollupAddress;
131
- }
132
- }
133
- export = _Lib;
29
+ export declare function toDelegateAddress(delegator: string, delegatee: string): string;
package/lib/index.js CHANGED
@@ -1,194 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toDelegateAddress = exports.toStakeAddress = exports.toRollupAddress = exports.toTokenAddress = exports.toFactoryAddress = exports.toAssetAddress = exports.toItxAddress = void 0;
4
+ const mcrypto_1 = require("@ocap/mcrypto");
5
+ const did_1 = require("@arcblock/did");
6
+ const message_1 = require("@ocap/message");
7
+ // @ts-ignore FIXME:
8
+ const proto_1 = require("@ocap/proto");
1
9
  /**
2
- * @fileOverview Utility functions to calculate various kinds of did, such as asset address
3
- *
4
- * @module @arcblock/did-util
5
- * @requires @ocap/mcrypto
6
- * @requires @arcblock/did
7
- * @requires @ocap/util
8
- * @requires @ocap/wallet
9
- * @requires @ocap/message
10
+ * Create an itx address
10
11
  */
11
- const { types, Hasher } = require('@ocap/mcrypto');
12
- const { fromHash, toDid } = require('@arcblock/did');
13
- const { createMessage } = require('@ocap/message');
14
- const { transactions } = require('@ocap/proto');
15
-
12
+ function toItxAddress(itx, type, role = mcrypto_1.types.RoleType.ROLE_TX) {
13
+ if (proto_1.transactions.indexOf(type) === -1) {
14
+ throw new Error(`Unsupported itx type ${type}`);
15
+ }
16
+ const message = (0, message_1.createMessage)(type, itx);
17
+ // @ts-ignore FIXME:
18
+ const itxBytes = message.serializeBinary();
19
+ const hash = mcrypto_1.Hasher.SHA3.hash256(itxBytes);
20
+ const address = (0, did_1.fromHash)(hash, role);
21
+ return address;
22
+ }
23
+ exports.toItxAddress = toItxAddress;
16
24
  /**
17
25
  * Create an asset address
18
- *
19
- * @public
20
- * @static
21
- * @param {object} itx - an object of `CreateAssetTx`
22
- * @returns {string} asset address without `did:abt:` prefix
23
26
  */
24
27
  function toAssetAddress(itx) {
25
- return toItxAddress(itx, 'CreateAssetTx', types.RoleType.ROLE_ASSET);
28
+ return toItxAddress(itx, 'CreateAssetTx', mcrypto_1.types.RoleType.ROLE_ASSET);
26
29
  }
27
-
30
+ exports.toAssetAddress = toAssetAddress;
28
31
  /**
29
- * Create an factory address
30
- *
31
- * @public
32
- * @static
33
- * @param {object} itx - an object of `CreateFactoryTx`
34
- * @returns {string} factory address without `did:abt:` prefix
32
+ * Create an asset factory address
35
33
  */
36
34
  function toFactoryAddress(itx) {
37
- return toItxAddress(itx, 'CreateFactoryTx', types.RoleType.ROLE_FACTORY);
35
+ return toItxAddress(itx, 'CreateFactoryTx', mcrypto_1.types.RoleType.ROLE_FACTORY);
38
36
  }
39
-
37
+ exports.toFactoryAddress = toFactoryAddress;
40
38
  /**
41
39
  * Create an token address
42
- *
43
- * @public
44
- * @static
45
- * @param {object} itx - an object of `CreateTokenTx`
46
- * @returns {string} token address without `did:abt:` prefix
47
40
  */
48
41
  function toTokenAddress(itx) {
49
- return toItxAddress(itx, 'CreateTokenTx', types.RoleType.ROLE_TOKEN);
42
+ return toItxAddress(itx, 'CreateTokenTx', mcrypto_1.types.RoleType.ROLE_TOKEN);
50
43
  }
51
-
44
+ exports.toTokenAddress = toTokenAddress;
52
45
  /**
53
- * Create an asset did
54
- *
55
- * @public
56
- * @static
57
- * @param {object} itx - an object of `CreateAssetTx`
58
- * @returns {string} asset address without `did:abt:` prefix
59
- */
60
- function toAssetDid(itx) {
61
- return toDid(toAssetAddress(itx));
62
- }
63
-
64
- /**
65
- * Create an itx address
66
- *
67
- * @public
68
- * @static
69
- * @param {object} itx - an object of forge supported itx
70
- * @param {string} type - itx type string
71
- * @param {enum} [role=types.RoleType.ROLE_TX] - role type
72
- * @returns {string} itx address without `did:abt:` prefix
73
- */
74
- function toItxAddress(itx, type, role = types.RoleType.ROLE_TX) {
75
- if (transactions.indexOf(type) === -1) {
76
- throw new Error(`Unsupported itx type ${type}`);
77
- }
78
-
79
- const message = createMessage(type, itx);
80
- const itxBytes = message.serializeBinary();
81
- const hash = Hasher.SHA3.hash256(itxBytes);
82
- const address = fromHash(hash, role);
83
- return address;
84
- }
85
-
86
- /**
87
- * Create an itx did
88
- *
89
- * @public
90
- * @static
91
- * @param {object} itx - an object of forge supported itx
92
- * @returns {string} itx address without `did:abt:` prefix
46
+ * Create an rollup address
93
47
  */
94
- function toItxDid(itx, type) {
95
- return toDid(toItxAddress(itx, type));
48
+ function toRollupAddress(itx) {
49
+ return toItxAddress(itx, 'CreateRollupTx', mcrypto_1.types.RoleType.ROLE_ROLLUP);
96
50
  }
97
-
51
+ exports.toRollupAddress = toRollupAddress;
98
52
  /**
99
53
  * Generate an stake address, eg: the did of the stake
100
- *
101
- * @public
102
- * @static
103
- * @param {string} sender - sender address
104
- * @param {string} receiver - receiver address
105
- * @returns {string} stake address without `did:abt:` prefix
106
54
  */
107
55
  function toStakeAddress(sender, receiver) {
108
- const senderBuffer = Buffer.from(sender);
109
- const receiverBuffer = Buffer.from(receiver);
110
- const buffer = Buffer.concat([senderBuffer, receiverBuffer]);
111
- const hash = Hasher.SHA3.hash256(buffer);
112
- return fromHash(hash, types.RoleType.ROLE_STAKE);
56
+ const buffer = Buffer.concat([Buffer.from(sender), Buffer.from(receiver)]);
57
+ const hash = mcrypto_1.Hasher.SHA3.hash256(buffer);
58
+ return (0, did_1.fromHash)(hash, mcrypto_1.types.RoleType.ROLE_STAKE);
113
59
  }
114
-
60
+ exports.toStakeAddress = toStakeAddress;
115
61
  /**
116
62
  * Generate an delegate address, eg: the did of the delegation
117
- *
118
- * @public
119
- * @static
120
- * @param {string} addr1 - delegator address
121
- * @param {string} addr2 - delegatee address
122
- * @returns {string} delegation address that can be used to retrieve delegation state
123
- */
124
- function toDelegateAddress(addr1, addr2) {
125
- const addr1Buffer = Buffer.from(addr1);
126
- const addr2Buffer = Buffer.from(addr2);
127
- const buffer = Buffer.concat([addr1Buffer, addr2Buffer]);
128
- const hash = Hasher.SHA3.hash256(buffer);
129
- return fromHash(hash, types.RoleType.ROLE_DELEGATION);
130
- }
131
-
132
- /**
133
- * Generate a tether address from the deposit tether tx hash
134
- *
135
- * @public
136
- * @static
137
- * @param {string} hash - DepositTetherTx hash
138
- * @returns {string} stake address without `did:abt:` prefix
139
- */
140
- function toTetherAddress(hash) {
141
- return fromHash(hash, types.RoleType.ROLE_TETHER);
142
- }
143
-
144
- /**
145
- * Generate a swap address from the setup swap tx hash
146
- *
147
- * @public
148
- * @static
149
- * @param {string} hash - SetupSwapTx hash
150
- * @returns {string} swap address without `did:abt:` prefix
151
63
  */
152
- function toSwapAddress(hash) {
153
- return fromHash(hash, types.RoleType.ROLE_SWAP);
154
- }
155
-
156
- /**
157
- * Generate an stake address, eg: the did of the stake
158
- *
159
- * @public
160
- * @static
161
- * @param {string} sender - sender address
162
- * @param {string} receiver - receiver address
163
- * @returns {string} stake address without `did:abt:` prefix
164
- */
165
- function toStakeDid(sender, receiver) {
166
- toDid(toStakeAddress(sender, receiver));
167
- }
168
-
169
- /**
170
- * Create an rollup address
171
- *
172
- * @public
173
- * @static
174
- * @param {object} itx - an object of `CreateRollupTx`
175
- * @returns {string} rollup address without `did:abt:` prefix
176
- */
177
- function toRollupAddress(itx) {
178
- return toItxAddress(itx, 'CreateRollupTx', types.RoleType.ROLE_ROLLUP);
64
+ function toDelegateAddress(delegator, delegatee) {
65
+ const buffer = Buffer.concat([Buffer.from(delegator), Buffer.from(delegatee)]);
66
+ const hash = mcrypto_1.Hasher.SHA3.hash256(buffer);
67
+ return (0, did_1.fromHash)(hash, mcrypto_1.types.RoleType.ROLE_DELEGATION);
179
68
  }
180
-
181
- module.exports = {
182
- toAssetAddress,
183
- toAssetDid,
184
- toItxAddress,
185
- toItxDid,
186
- toStakeAddress,
187
- toDelegateAddress,
188
- toStakeDid,
189
- toTetherAddress,
190
- toSwapAddress,
191
- toTokenAddress,
192
- toFactoryAddress,
193
- toRollupAddress,
194
- };
69
+ exports.toDelegateAddress = toDelegateAddress;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@arcblock/did-util",
3
3
  "description": "Helper function to calculate did",
4
- "version": "1.17.6",
4
+ "version": "1.17.7",
5
5
  "author": {
6
6
  "name": "wangshijun",
7
7
  "email": "shijun@arcblock.io",
@@ -18,54 +18,46 @@
18
18
  "wangshijun <shijun@arcblock.io> (https://github.com/wangshijun)"
19
19
  ],
20
20
  "dependencies": {
21
- "@arcblock/did": "1.17.6",
22
- "@ocap/mcrypto": "1.17.6",
23
- "@ocap/message": "1.17.6",
24
- "@ocap/proto": "1.17.6",
25
- "@ocap/util": "1.17.6",
26
- "@ocap/wallet": "1.17.6"
27
- },
21
+ "@arcblock/did": "1.17.7",
22
+ "@ocap/mcrypto": "1.17.7",
23
+ "@ocap/message": "1.17.7",
24
+ "@ocap/proto": "1.17.7",
25
+ "@ocap/types": "1.17.7",
26
+ "@ocap/util": "1.17.7",
27
+ "@ocap/wallet": "1.17.7"
28
+ },
29
+ "main": "lib/index.js",
30
+ "typings": "lib/index.d.ts",
31
+ "files": [
32
+ "lib"
33
+ ],
28
34
  "devDependencies": {
29
- "jest": "^27.3.1",
30
- "remark-cli": "^10.0.1",
31
- "remark-preset-github": "^4.0.1"
32
- },
33
- "remarkConfig": {
34
- "plugins": [
35
- "preset-github",
36
- [
37
- {
38
- "repository": "ArcBlock/asset-chain"
39
- }
40
- ]
41
- ]
35
+ "@arcblock/eslint-config-ts": "0.2.2",
36
+ "@types/jest": "^28.1.1",
37
+ "@types/node": "^17.0.42",
38
+ "eslint": "^8.17.0",
39
+ "jest": "^28.1.1",
40
+ "ts-jest": "^28.0.4",
41
+ "typescript": "^4.7.3"
42
42
  },
43
43
  "homepage": "https://github.com/ArcBlock/asset-chain/tree/master/did/did-util",
44
44
  "keywords": [
45
- "blockchain",
46
- "arcblock",
47
- "sdk",
48
- "nodejs"
45
+ "did"
49
46
  ],
50
47
  "license": "Apache-2.0",
51
- "main": "./lib/index.js",
52
- "files": [
53
- "lib"
54
- ],
55
48
  "repository": {
56
49
  "type": "git",
57
50
  "url": "https://github.com/ArcBlock/asset-chain/tree/master/did/did-util"
58
51
  },
59
52
  "scripts": {
60
- "lint": "eslint lib tests",
61
- "lint:fix": "eslint --fix lib tests",
62
- "docs": "yarn gen-dts && yarn gen-docs && yarn cleanup-docs && yarn format-docs",
63
- "cleanup-docs": "node ../../scripts/cleanup-docs.js docs/README.md $npm_package_name",
64
- "gen-docs": "jsdoc2md lib/index.js > docs/README.md",
65
- "gen-dts": "j2d lib/index.js",
66
- "format-docs": "remark . -o",
53
+ "lint": "eslint src tests",
54
+ "lint:fix": "npm run lint -- --fix",
67
55
  "test": "jest --forceExit --detectOpenHandles",
68
- "coverage": "yarn test -- --coverage"
56
+ "coverage": "yarn test -- --coverage",
57
+ "clean": "rm -fr lib",
58
+ "prebuild": "npm run clean",
59
+ "build": "tsc",
60
+ "build:watch": "npm run build -- -w"
69
61
  },
70
- "gitHead": "68e7711c1550c24505e4b8f5de1867a6ce360be8"
62
+ "gitHead": "02d9fa7c14c70f0a5fc1dcc5dc5506e43cb71ba5"
71
63
  }