@blocklet/sdk 1.16.0-beta-b16cb035 → 1.16.0-beta-1d6c582e

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.
@@ -1,4 +1,6 @@
1
- import { AxiosResponse } from 'axios';
1
+ /// <reference types="node" />
2
+ import { AxiosResponse, Method } from 'axios';
3
+ import { IncomingMessage } from 'http';
2
4
  export type MountPoint = {
3
5
  did: string;
4
6
  name: string;
@@ -10,11 +12,20 @@ export type MountPoint = {
10
12
  declare const getChildWebEndpoint: (name: string) => string;
11
13
  declare const getParentWebEndpoint: () => string;
12
14
  declare const getComponentWebEndpoint: (keyword: string) => string;
13
- declare const call: ({ name, path: _path, data }: {
15
+ type CallComponentOptions<D = any, P = any> = {
14
16
  name?: string;
17
+ method?: Method;
15
18
  path: string;
16
- data: any;
17
- }) => Promise<AxiosResponse<any, any>>;
19
+ data?: D;
20
+ params?: P;
21
+ };
22
+ type CallComponent = {
23
+ (options: CallComponentOptions & {
24
+ responseType: 'stream';
25
+ }): Promise<AxiosResponse<IncomingMessage>>;
26
+ <T = any, D = any, P = any>(options: CallComponentOptions<D, P>): Promise<AxiosResponse<T, D>>;
27
+ };
28
+ declare const call: CallComponent;
18
29
  declare const getComponentMountPoint: (keyword: string) => string;
19
30
  export { call };
20
31
  export { getComponentMountPoint };
@@ -22,11 +33,7 @@ export { getComponentWebEndpoint };
22
33
  export { getChildWebEndpoint };
23
34
  export { getParentWebEndpoint };
24
35
  declare const _default: {
25
- call: ({ name, path: _path, data }: {
26
- name?: string;
27
- path: string;
28
- data: any;
29
- }) => Promise<AxiosResponse<any, any>>;
36
+ call: CallComponent;
30
37
  getComponentMountPoint: (keyword: string) => string;
31
38
  getComponentWebEndpoint: (keyword: string) => string;
32
39
  getChildWebEndpoint: (name: string) => string;
@@ -4,13 +4,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.getParentWebEndpoint = exports.getChildWebEndpoint = exports.getComponentWebEndpoint = exports.getComponentMountPoint = exports.call = void 0;
7
+ const util_1 = require("@blocklet/meta/lib/util");
7
8
  const axios_1 = __importDefault(require("axios"));
9
+ const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
8
10
  const get_1 = __importDefault(require("lodash/get"));
9
11
  const url_join_1 = __importDefault(require("url-join"));
10
- const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
11
- const util_1 = require("@blocklet/meta/lib/util");
12
- const wallet_1 = __importDefault(require("../wallet"));
13
12
  const config_1 = require("../config");
13
+ const wallet_1 = __importDefault(require("../wallet"));
14
14
  const parseMountPoints = () => {
15
15
  const mountPoints = JSON.parse(process.env.BLOCKLET_MOUNT_POINTS) || [];
16
16
  mountPoints.forEach((x) => {
@@ -25,9 +25,14 @@ const sign = (data) => {
25
25
  const signData = typeof data === 'undefined' ? {} : data;
26
26
  return { 'x-component-sig': wallet.sign((0, json_stable_stringify_1.default)(signData)) };
27
27
  };
28
- const doCall = async ({ url, data }) => {
28
+ const doCall = async ({ url, ...options }) => {
29
29
  try {
30
- const resp = await axios_1.default.post(url, data, { headers: sign(data), timeout: 60000 });
30
+ const resp = await (0, axios_1.default)({
31
+ url,
32
+ ...options,
33
+ headers: { ...sign(options.data ?? {}) },
34
+ timeout: 60000,
35
+ });
31
36
  config_1.logger.info(`call ${url} api success`);
32
37
  return resp;
33
38
  }
@@ -65,7 +70,7 @@ const getComponentWebEndpoint = (keyword) => {
65
70
  return item ? item.webEndpoint : '';
66
71
  };
67
72
  exports.getComponentWebEndpoint = getComponentWebEndpoint;
68
- const call = async ({ name, path: _path, data }) => {
73
+ const call = async ({ name, method = 'POST', path: _path, ...options }) => {
69
74
  let baseURL;
70
75
  if (name) {
71
76
  baseURL = getComponentWebEndpoint(name) || getChildWebEndpoint(name);
@@ -77,7 +82,7 @@ const call = async ({ name, path: _path, data }) => {
77
82
  throw new Error(`can not find web endpoint for ${name}`);
78
83
  }
79
84
  const url = (0, url_join_1.default)(baseURL, _path);
80
- return doCall({ url, data });
85
+ return doCall({ url, method, ...options });
81
86
  };
82
87
  exports.call = call;
83
88
  const getComponentMountPoint = (keyword) => {
@@ -5,23 +5,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.getAuthenticatorProps = exports.getDelegatee = exports.getDelegator = exports.getDelegation = void 0;
7
7
  const url_join_1 = __importDefault(require("url-join"));
8
- const wallet_1 = __importDefault(require("@blocklet/meta/lib/wallet"));
9
8
  const jwt_1 = require("@arcblock/jwt");
10
9
  const util_1 = require("@blocklet/meta/lib/util");
11
10
  const constants_1 = require("../util/constants");
11
+ const wallet_1 = __importDefault(require("../wallet"));
12
12
  // wraps value in closure or returns closure
13
13
  const closure = (value) => (typeof value === 'function' ? value : () => value);
14
14
  const getDelegator = () => {
15
15
  const { BLOCKLET_APP_SK, BLOCKLET_APP_PSK, BLOCKLET_WALLET_TYPE } = process.env;
16
16
  if (BLOCKLET_APP_SK && BLOCKLET_APP_PSK && BLOCKLET_APP_SK !== BLOCKLET_APP_PSK) {
17
- return (0, wallet_1.default)(BLOCKLET_APP_PSK, undefined, BLOCKLET_WALLET_TYPE);
17
+ return (0, wallet_1.default)(BLOCKLET_WALLET_TYPE, BLOCKLET_APP_PSK);
18
18
  }
19
19
  return null;
20
20
  };
21
21
  exports.getDelegator = getDelegator;
22
22
  const getDelegatee = () => {
23
23
  const { BLOCKLET_APP_SK, BLOCKLET_WALLET_TYPE } = process.env;
24
- return (0, wallet_1.default)(BLOCKLET_APP_SK, undefined, BLOCKLET_WALLET_TYPE);
24
+ return (0, wallet_1.default)(BLOCKLET_WALLET_TYPE, BLOCKLET_APP_SK);
25
25
  };
26
26
  exports.getDelegatee = getDelegatee;
27
27
  const getDelegation = (delegator, delegatee) => {
package/lib/wallet.d.ts CHANGED
@@ -1,3 +1,13 @@
1
1
  import { WalletObject } from '@ocap/wallet';
2
- declare const getWallet: () => WalletObject;
2
+ import { DIDTypeShortcut } from '@arcblock/did';
3
+ /**
4
+ * @param {string} [type=process.env.BLOCKLET_WALLET_TYPE] can only be 'eth|ethereum' or 'default|arcblock'
5
+ * @param {string} [appSk=process.env.BLOCKLET_APP_SK] must be hex
6
+ * @return {WalletObject} {WalletObject}
7
+ */
8
+ declare const getWallet: {
9
+ (type?: DIDTypeShortcut, appSk?: string): WalletObject;
10
+ getPermanentWallet: () => WalletObject<string>;
11
+ getEthereumWallet: (permanent?: boolean) => WalletObject<string>;
12
+ };
3
13
  export = getWallet;
package/lib/wallet.js CHANGED
@@ -1,10 +1,27 @@
1
1
  "use strict";
2
2
  const wallet_1 = require("@ocap/wallet");
3
3
  const mcrypto_1 = require("@ocap/mcrypto");
4
- const getWallet = () => {
5
- const type = process.env.BLOCKLET_WALLET_TYPE !== 'eth'
6
- ? (0, wallet_1.WalletType)({ role: mcrypto_1.types.RoleType.ROLE_APPLICATION, pk: mcrypto_1.types.KeyType.ED25519, hash: mcrypto_1.types.HashType.SHA3 })
7
- : 'eth';
8
- return (0, wallet_1.fromSecretKey)(process.env.BLOCKLET_APP_SK, type);
4
+ const did_1 = require("@arcblock/did");
5
+ /**
6
+ * @param {string} [type=process.env.BLOCKLET_WALLET_TYPE] can only be 'eth|ethereum' or 'default|arcblock'
7
+ * @param {string} [appSk=process.env.BLOCKLET_APP_SK] must be hex
8
+ * @return {WalletObject} {WalletObject}
9
+ */
10
+ const getWallet = (type = process.env.BLOCKLET_WALLET_TYPE, appSk = process.env.BLOCKLET_APP_SK) => {
11
+ let t;
12
+ let sk;
13
+ if ((0, did_1.isEthereumType)((0, did_1.DidType)(type))) {
14
+ sk = appSk.slice(0, 66);
15
+ t = (0, wallet_1.WalletType)(type);
16
+ }
17
+ else {
18
+ sk = appSk;
19
+ t = (0, wallet_1.WalletType)({ role: mcrypto_1.types.RoleType.ROLE_APPLICATION, pk: mcrypto_1.types.KeyType.ED25519, hash: mcrypto_1.types.HashType.SHA3 });
20
+ }
21
+ return (0, wallet_1.fromSecretKey)(sk, t);
9
22
  };
23
+ const getPermanentWallet = () => getWallet(process.env.BLOCKLET_WALLET_TYPE, process.env.BLOCKLET_APP_PSK);
24
+ const getEthereumWallet = (permanent = false) => getWallet('ethereum', permanent ? process.env.BLOCKLET_APP_PSK : process.env.BLOCKLET_APP_SK);
25
+ getWallet.getPermanentWallet = getPermanentWallet;
26
+ getWallet.getEthereumWallet = getEthereumWallet;
10
27
  module.exports = getWallet;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.0-beta-b16cb035",
6
+ "version": "1.16.0-beta-1d6c582e",
7
7
  "description": "graphql client to read/write data on abt node",
8
8
  "main": "lib/index.js",
9
9
  "typings": "lib/index.d.ts",
@@ -26,18 +26,19 @@
26
26
  "author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
27
27
  "license": "MIT",
28
28
  "dependencies": {
29
- "@abtnode/client": "1.16.0-beta-b16cb035",
30
- "@abtnode/constant": "1.16.0-beta-b16cb035",
31
- "@arcblock/did-auth": "1.18.59",
32
- "@arcblock/jwt": "1.18.59",
33
- "@arcblock/ws": "1.18.59",
34
- "@blocklet/constant": "1.16.0-beta-b16cb035",
35
- "@blocklet/meta": "1.16.0-beta-b16cb035",
29
+ "@abtnode/client": "1.16.0-beta-1d6c582e",
30
+ "@abtnode/constant": "1.16.0-beta-1d6c582e",
31
+ "@arcblock/did": "1.18.62",
32
+ "@arcblock/did-auth": "1.18.62",
33
+ "@arcblock/jwt": "1.18.62",
34
+ "@arcblock/ws": "1.18.62",
35
+ "@blocklet/constant": "1.16.0-beta-1d6c582e",
36
+ "@blocklet/meta": "1.16.0-beta-1d6c582e",
36
37
  "@did-connect/authenticator": "^2.1.44",
37
38
  "@did-connect/handler": "^2.1.44",
38
39
  "@nedb/core": "^2.1.5",
39
- "@ocap/mcrypto": "1.18.59",
40
- "@ocap/wallet": "1.18.59",
40
+ "@ocap/mcrypto": "1.18.62",
41
+ "@ocap/wallet": "1.18.62",
41
42
  "axios": "^0.27.2",
42
43
  "cheerio": "^1.0.0-rc.12",
43
44
  "fs-extra": "^10.1.0",
@@ -70,5 +71,5 @@
70
71
  "ts-node": "^10.9.1",
71
72
  "typescript": "^4.8.4"
72
73
  },
73
- "gitHead": "138bd91aee5a35b28c2de4e1e924c44932efaf3a"
74
+ "gitHead": "209fa1413ae05d8961942b2c21f2d83df66f4b68"
74
75
  }