@arcblock/did-ext 1.30.0 → 1.30.1

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.
@@ -0,0 +1,13 @@
1
+ import { WalletObject } from "@ocap/wallet";
2
+ import { DIDTypeArg } from "@arcblock/did";
3
+
4
+ //#region src/index.d.ts
5
+
6
+ /**
7
+ * Gen user DID from app did and rootSk
8
+ *
9
+ * Spec: https://github.com/ArcBlock/ABT-DID-Protocol#request-did-authentication
10
+ */
11
+ declare const fromAppDid: (appDid: string, rootSk: string, type?: DIDTypeArg, index?: number) => WalletObject;
12
+ //#endregion
13
+ export { fromAppDid };
package/esm/index.mjs ADDED
@@ -0,0 +1,33 @@
1
+ import { Hasher, Signer, types } from "@ocap/mcrypto";
2
+ import { isHexStrict, stripHexPrefix, toBuffer, toHex } from "@ocap/util";
3
+ import { WalletType, fromSecretKey } from "@ocap/wallet";
4
+ import { HDKey } from "@scure/bip32";
5
+
6
+ //#region src/index.ts
7
+ /**
8
+ * Gen user DID from app did and rootSk
9
+ *
10
+ * Spec: https://github.com/ArcBlock/ABT-DID-Protocol#request-did-authentication
11
+ */
12
+ const fromAppDid = (appDid, rootSk, type = { role: types.RoleType.ROLE_ACCOUNT }, index = 0) => {
13
+ if (!rootSk) throw new Error("Cannot get user wallet with empty root sk");
14
+ const hashSlice = stripHexPrefix(Hasher.SHA3.hash256(toBuffer(appDid))).slice(0, 16);
15
+ const s1 = hashSlice.slice(0, 8);
16
+ const s2 = hashSlice.slice(8, 16);
17
+ const n1 = Number.parseInt(Number(`0x${s1}`).toString(), 10) & 2147483647;
18
+ const n2 = Number.parseInt(Number(`0x${s2}`).toString(), 10) & 2147483647;
19
+ const seedHex = stripHexPrefix(isHexStrict(rootSk) ? rootSk : toHex(rootSk));
20
+ const master = HDKey.fromMasterSeed(Buffer.from(seedHex, "hex"));
21
+ const derivePath = `m/44'/260'/${n1}'/${n2}'/${index}`;
22
+ const child = master.derive(derivePath);
23
+ const typeObj = WalletType(type);
24
+ const { pk = types.KeyType.ED25519 } = typeObj;
25
+ let sk;
26
+ if (pk === types.KeyType.ED25519) sk = Signer.Ed25519.genKeyPair("hex", child.privateKey).secretKey;
27
+ else sk = toHex(child.privateKey);
28
+ if (pk === types.KeyType.ETHEREUM) sk = sk.slice(0, 66);
29
+ return fromSecretKey(sk, typeObj);
30
+ };
31
+
32
+ //#endregion
33
+ export { fromAppDid };
package/lib/index.cjs ADDED
@@ -0,0 +1,33 @@
1
+ let _ocap_mcrypto = require("@ocap/mcrypto");
2
+ let _ocap_util = require("@ocap/util");
3
+ let _ocap_wallet = require("@ocap/wallet");
4
+ let _scure_bip32 = require("@scure/bip32");
5
+
6
+ //#region src/index.ts
7
+ /**
8
+ * Gen user DID from app did and rootSk
9
+ *
10
+ * Spec: https://github.com/ArcBlock/ABT-DID-Protocol#request-did-authentication
11
+ */
12
+ const fromAppDid = (appDid, rootSk, type = { role: _ocap_mcrypto.types.RoleType.ROLE_ACCOUNT }, index = 0) => {
13
+ if (!rootSk) throw new Error("Cannot get user wallet with empty root sk");
14
+ const hashSlice = (0, _ocap_util.stripHexPrefix)(_ocap_mcrypto.Hasher.SHA3.hash256((0, _ocap_util.toBuffer)(appDid))).slice(0, 16);
15
+ const s1 = hashSlice.slice(0, 8);
16
+ const s2 = hashSlice.slice(8, 16);
17
+ const n1 = Number.parseInt(Number(`0x${s1}`).toString(), 10) & 2147483647;
18
+ const n2 = Number.parseInt(Number(`0x${s2}`).toString(), 10) & 2147483647;
19
+ const seedHex = (0, _ocap_util.stripHexPrefix)((0, _ocap_util.isHexStrict)(rootSk) ? rootSk : (0, _ocap_util.toHex)(rootSk));
20
+ const master = _scure_bip32.HDKey.fromMasterSeed(Buffer.from(seedHex, "hex"));
21
+ const derivePath = `m/44'/260'/${n1}'/${n2}'/${index}`;
22
+ const child = master.derive(derivePath);
23
+ const typeObj = (0, _ocap_wallet.WalletType)(type);
24
+ const { pk = _ocap_mcrypto.types.KeyType.ED25519 } = typeObj;
25
+ let sk;
26
+ if (pk === _ocap_mcrypto.types.KeyType.ED25519) sk = _ocap_mcrypto.Signer.Ed25519.genKeyPair("hex", child.privateKey).secretKey;
27
+ else sk = (0, _ocap_util.toHex)(child.privateKey);
28
+ if (pk === _ocap_mcrypto.types.KeyType.ETHEREUM) sk = sk.slice(0, 66);
29
+ return (0, _ocap_wallet.fromSecretKey)(sk, typeObj);
30
+ };
31
+
32
+ //#endregion
33
+ exports.fromAppDid = fromAppDid;
@@ -0,0 +1,13 @@
1
+ import { DIDTypeArg } from "@arcblock/did";
2
+ import { WalletObject } from "@ocap/wallet";
3
+
4
+ //#region src/index.d.ts
5
+
6
+ /**
7
+ * Gen user DID from app did and rootSk
8
+ *
9
+ * Spec: https://github.com/ArcBlock/ABT-DID-Protocol#request-did-authentication
10
+ */
11
+ declare const fromAppDid: (appDid: string, rootSk: string, type?: DIDTypeArg, index?: number) => WalletObject;
12
+ //#endregion
13
+ export { fromAppDid };
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@arcblock/did-ext",
3
- "version": "1.30.0",
3
+ "version": "1.30.1",
4
4
  "description": "Extension to work with ArcBlock DID",
5
5
  "keywords": [
6
6
  "arcblock",
7
7
  "did",
8
8
  "sdk"
9
9
  ],
10
+ "type": "module",
10
11
  "publishConfig": {
11
12
  "access": "public"
12
13
  },
@@ -20,11 +21,25 @@
20
21
  ],
21
22
  "homepage": "https://github.com/ArcBlock/blockchain/tree/master/did/did-ext",
22
23
  "license": "Apache-2.0",
23
- "main": "lib/index.js",
24
+ "sideEffects": false,
25
+ "main": "./lib/index.cjs",
26
+ "module": "./esm/index.mjs",
27
+ "types": "./esm/index.d.mts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./esm/index.d.mts",
31
+ "import": "./esm/index.mjs",
32
+ "default": "./lib/index.cjs"
33
+ }
34
+ },
24
35
  "files": [
25
- "lib"
36
+ "lib",
37
+ "esm"
26
38
  ],
27
- "devDependencies": {},
39
+ "devDependencies": {
40
+ "@types/node": "^22.7.5",
41
+ "tsdown": "^0.18.4"
42
+ },
28
43
  "repository": {
29
44
  "type": "git",
30
45
  "url": "git+https://github.com/ArcBlock/blockchain.git"
@@ -32,25 +47,20 @@
32
47
  "scripts": {
33
48
  "lint": "biome check",
34
49
  "lint:fix": "biome check --write",
35
- "docs": "bun run gen-dts && bun run gen-docs && bun run cleanup-docs && bun run format-docs",
36
- "cleanup-docs": "node ../../scripts/cleanup-docs.js docs/README.md $npm_package_name",
37
- "gen-docs": "jsdoc2md lib/index.js > docs/README.md",
38
- "gen-dts": "j2d lib/index.js",
39
- "format-docs": "remark . -o",
40
50
  "test": "bun test",
41
- "coverage": "npm run test -- --coverage"
51
+ "coverage": "npm run test -- --coverage",
52
+ "build": "tsdown",
53
+ "build:watch": "tsdown -w",
54
+ "clean": "rm -rf lib esm"
42
55
  },
43
56
  "bugs": {
44
57
  "url": "https://github.com/ArcBlock/blockchain/issues"
45
58
  },
46
59
  "dependencies": {
47
- "@arcblock/did": "1.30.0",
48
- "@ocap/mcrypto": "1.30.0",
49
- "@ocap/util": "1.30.0",
50
- "@ocap/wallet": "1.30.0",
51
- "@scure/bip32": "^2.0.1",
52
- "bn.js": "5.2.3",
53
- "debug": "^4.4.3",
54
- "lodash": "^4.17.23"
60
+ "@arcblock/did": "1.30.1",
61
+ "@ocap/mcrypto": "1.30.1",
62
+ "@ocap/util": "1.30.1",
63
+ "@ocap/wallet": "1.30.1",
64
+ "@scure/bip32": "^2.0.1"
55
65
  }
56
66
  }
package/lib/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- // Generate by [js2dts@0.3.3](https://github.com/whxaxes/js2dts#readme)
2
-
3
- declare const _Lib: _Lib.T100;
4
- declare namespace _Lib {
5
- export interface T100 {
6
- fromAppDid: (appDid: string, rootSk: string, type?: any, index?: number) => any;
7
- }
8
- }
9
- export = _Lib;
package/lib/index.js DELETED
@@ -1,57 +0,0 @@
1
- const { HDKey } = require('@scure/bip32');
2
- const Mcrypto = require('@ocap/mcrypto');
3
- const { fromSecretKey, WalletType } = require('@ocap/wallet');
4
- const { isHexStrict, toHex, toBuffer, stripHexPrefix } = require('@ocap/util');
5
-
6
- const { types } = Mcrypto;
7
-
8
- /**
9
- * Gen user DID from app did and rootSk
10
- *
11
- * Spec: https://github.com/ArcBlock/ABT-DID-Protocol#request-did-authentication
12
- *
13
- * @public
14
- * @static
15
- * @param {string} appDid
16
- * @param {string} rootSk
17
- * @param {object} [type={}]
18
- * @param {number} [index=0]
19
- * @returns {object} ForgeWallet instance
20
- */
21
- const fromAppDid = (appDid, rootSk, type = { role: types.RoleType.ROLE_ACCOUNT }, index = 0) => {
22
- if (!rootSk) {
23
- throw new Error('Cannot get user wallet with empty root sk');
24
- }
25
-
26
- const hash = Mcrypto.Hasher.SHA3.hash256(toBuffer(appDid));
27
- const hashSlice = stripHexPrefix(hash).slice(0, 16);
28
- const s1 = hashSlice.slice(0, 8);
29
- const s2 = hashSlice.slice(8, 16);
30
-
31
- const n1 = Number.parseInt(Number(`0x${s1}`), 10) & 0x7fffffff;
32
- const n2 = Number.parseInt(Number(`0x${s2}`), 10) & 0x7fffffff;
33
-
34
- const seedHex = stripHexPrefix(isHexStrict(rootSk) ? rootSk : toHex(rootSk));
35
- const master = HDKey.fromMasterSeed(Buffer.from(seedHex, 'hex'));
36
- const derivePath = `m/44'/260'/${n1}'/${n2}'/${index}`;
37
- const child = master.derive(derivePath);
38
-
39
- const typeObj = WalletType(type);
40
- const { pk = types.KeyType.ED25519 } = typeObj;
41
- let sk;
42
- if (pk === types.KeyType.ED25519) {
43
- const keyPair = Mcrypto.Signer.Ed25519.genKeyPair('hex', child.privateKey);
44
- sk = keyPair.secretKey;
45
- } else {
46
- sk = toHex(child.privateKey);
47
- }
48
-
49
- // To keeps things consistent between ocap and ethereum: 32 bytes private key
50
- if (pk === types.KeyType.ETHEREUM) {
51
- sk = sk.slice(0, 66);
52
- }
53
-
54
- return fromSecretKey(sk, typeObj);
55
- };
56
-
57
- module.exports = { fromAppDid };