paytonz-node-api-sdk 1.0.0

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 (97) hide show
  1. package/README.md +743 -0
  2. package/lib/CpaySDK.d.ts +30 -0
  3. package/lib/CpaySDK.js +35 -0
  4. package/lib/CpaySDKBase.d.ts +30 -0
  5. package/lib/CpaySDKBase.js +216 -0
  6. package/lib/CryptoNowSDK.d.ts +30 -0
  7. package/lib/CryptoNowSDK.js +35 -0
  8. package/lib/CryptoNowSDKBase.d.ts +30 -0
  9. package/lib/CryptoNowSDKBase.js +216 -0
  10. package/lib/PaytonzSDK.d.ts +28 -0
  11. package/lib/PaytonzSDK.js +33 -0
  12. package/lib/PaytonzSDKBase.d.ts +30 -0
  13. package/lib/PaytonzSDKBase.js +198 -0
  14. package/lib/auth/auth.d.ts +9 -0
  15. package/lib/auth/auth.interface.d.ts +25 -0
  16. package/lib/auth/auth.interface.js +2 -0
  17. package/lib/auth/auth.js +37 -0
  18. package/lib/checkout/checkout.d.ts +24 -0
  19. package/lib/checkout/checkout.interface.d.ts +94 -0
  20. package/lib/checkout/checkout.interface.js +2 -0
  21. package/lib/checkout/checkout.js +215 -0
  22. package/lib/client/client.d.ts +11 -0
  23. package/lib/client/client.interface.d.ts +45 -0
  24. package/lib/client/client.interface.js +2 -0
  25. package/lib/client/client.js +62 -0
  26. package/lib/constant.d.ts +1 -0
  27. package/lib/constant.js +4 -0
  28. package/lib/currency/currency.d.ts +10 -0
  29. package/lib/currency/currency.interface.d.ts +20 -0
  30. package/lib/currency/currency.interface.js +2 -0
  31. package/lib/currency/currency.js +50 -0
  32. package/lib/external/external.d.ts +13 -0
  33. package/lib/external/external.interface.d.ts +59 -0
  34. package/lib/external/external.interface.js +12 -0
  35. package/lib/external/external.js +64 -0
  36. package/lib/external/external.solana.d.ts +9 -0
  37. package/lib/external/external.solana.js +47 -0
  38. package/lib/index.d.ts +5 -0
  39. package/lib/index.js +28 -0
  40. package/lib/interfaces/cpay.interface.d.ts +20 -0
  41. package/lib/interfaces/cpay.interface.js +2 -0
  42. package/lib/interfaces/cryptonow.interface.d.ts +20 -0
  43. package/lib/interfaces/cryptonow.interface.js +2 -0
  44. package/lib/interfaces/paytonz.interface.d.ts +20 -0
  45. package/lib/interfaces/paytonz.interface.js +2 -0
  46. package/lib/multisend/multisend.d.ts +11 -0
  47. package/lib/multisend/multisend.interface.d.ts +28 -0
  48. package/lib/multisend/multisend.interface.js +2 -0
  49. package/lib/multisend/multisend.js +75 -0
  50. package/lib/swap/swap.d.ts +11 -0
  51. package/lib/swap/swap.interface.d.ts +84 -0
  52. package/lib/swap/swap.interface.js +2 -0
  53. package/lib/swap/swap.js +75 -0
  54. package/lib/transaction/transaction.d.ts +9 -0
  55. package/lib/transaction/transaction.interface.d.ts +82 -0
  56. package/lib/transaction/transaction.interface.js +2 -0
  57. package/lib/transaction/transaction.js +34 -0
  58. package/lib/utils/httpClient.d.ts +9 -0
  59. package/lib/utils/httpClient.js +64 -0
  60. package/lib/wallet/wallet.d.ts +16 -0
  61. package/lib/wallet/wallet.interface.d.ts +60 -0
  62. package/lib/wallet/wallet.interface.js +2 -0
  63. package/lib/wallet/wallet.js +107 -0
  64. package/lib/wallet/wallet.signature.d.ts +14 -0
  65. package/lib/wallet/wallet.signature.js +104 -0
  66. package/lib/withdrawal/withdrawal.d.ts +14 -0
  67. package/lib/withdrawal/withdrawal.interface.d.ts +79 -0
  68. package/lib/withdrawal/withdrawal.interface.js +14 -0
  69. package/lib/withdrawal/withdrawal.js +104 -0
  70. package/package.json +26 -0
  71. package/src/PaytonzSDK.ts +41 -0
  72. package/src/PaytonzSDKBase.ts +252 -0
  73. package/src/auth/auth.interface.ts +26 -0
  74. package/src/auth/auth.ts +31 -0
  75. package/src/checkout/checkout.interface.ts +110 -0
  76. package/src/checkout/checkout.ts +279 -0
  77. package/src/constant.ts +1 -0
  78. package/src/currency/currency.interface.ts +21 -0
  79. package/src/currency/currency.ts +60 -0
  80. package/src/external/external.interface.ts +65 -0
  81. package/src/external/external.solana.ts +54 -0
  82. package/src/external/external.ts +73 -0
  83. package/src/index.ts +5 -0
  84. package/src/interfaces/paytonz.interface.ts +23 -0
  85. package/src/multisend/multisend.interface.ts +32 -0
  86. package/src/multisend/multisend.ts +91 -0
  87. package/src/swap/swap.interface.ts +90 -0
  88. package/src/swap/swap.ts +93 -0
  89. package/src/transaction/transaction.interface.ts +76 -0
  90. package/src/transaction/transaction.ts +35 -0
  91. package/src/utils/httpClient.ts +60 -0
  92. package/src/wallet/wallet.interface.ts +67 -0
  93. package/src/wallet/wallet.signature.ts +106 -0
  94. package/src/wallet/wallet.ts +103 -0
  95. package/src/withdrawal/withdrawal.interface.ts +89 -0
  96. package/src/withdrawal/withdrawal.ts +135 -0
  97. package/tsconfig.json +21 -0
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.WalletSignature = void 0;
13
+ const PaytonzSDKBase_1 = require("../PaytonzSDKBase");
14
+ class WalletSignature extends PaytonzSDKBase_1.PaytonzSDKBase {
15
+ constructor(parameters) {
16
+ super(parameters);
17
+ }
18
+ on(options, accessToken) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ try {
21
+ if (!accessToken) {
22
+ accessToken = yield this.getToken(true);
23
+ }
24
+ const path = `/api/public/wallet/signature/on`;
25
+ return this.auth_post(`${path}`, options, accessToken);
26
+ }
27
+ catch (err) {
28
+ throw err;
29
+ }
30
+ });
31
+ }
32
+ off(options, accessToken) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ try {
35
+ if (!accessToken) {
36
+ accessToken = yield this.getToken(true);
37
+ }
38
+ const path = `/api/public/wallet/signature/off`;
39
+ return this.auth_post(`${path}`, options, accessToken);
40
+ }
41
+ catch (err) {
42
+ throw err;
43
+ }
44
+ });
45
+ }
46
+ status(accessToken) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ try {
49
+ if (!accessToken) {
50
+ accessToken = yield this.getToken(true);
51
+ }
52
+ const path = `/api/public/wallet/signature/status`;
53
+ return this.auth_get(`${path}`, {}, accessToken);
54
+ }
55
+ catch (err) {
56
+ throw err;
57
+ }
58
+ });
59
+ }
60
+ download(options, accessToken) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ try {
63
+ if (!accessToken) {
64
+ accessToken = yield this.getToken(true);
65
+ }
66
+ const path = `/api/public/wallet/signature/download`;
67
+ return this.auth_post(`${path}`, options, accessToken);
68
+ }
69
+ catch (err) {
70
+ throw err;
71
+ }
72
+ });
73
+ }
74
+ changePassword(options, accessToken) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ try {
77
+ if (!accessToken) {
78
+ accessToken = yield this.getToken(true);
79
+ }
80
+ const path = `/api/public/wallet/signature/change-password`;
81
+ return this.auth_post(`${path}`, options, accessToken);
82
+ }
83
+ catch (err) {
84
+ throw err;
85
+ }
86
+ });
87
+ }
88
+ passwordStatus(accessToken) {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ try {
91
+ if (!accessToken) {
92
+ accessToken = yield this.getToken(true);
93
+ }
94
+ const path = `/api/public/wallet/signature/password-status`;
95
+ return this.auth_get(`${path}`, {}, accessToken);
96
+ }
97
+ catch (err) {
98
+ throw err;
99
+ }
100
+ });
101
+ }
102
+ }
103
+ exports.WalletSignature = WalletSignature;
104
+ exports.default = WalletSignature;
@@ -0,0 +1,14 @@
1
+ import { PaytonzSDKBase, PaytonzSDKBaseOptions } from "../PaytonzSDKBase";
2
+ import { CreateNftWithdrawalOptions, CreateWithdrawalInfo, CreateWithdrawalOptions, EstimateFeeInfo, EstimateFeeNftInfo, EstimateFeeNftOptions, EstimateFeeOptions, EstimateMaxInfo, EstimateMaxOptions, InternalTransferOptions } from "./withdrawal.interface";
3
+ export interface PaytonzSDKOptions extends PaytonzSDKBaseOptions {
4
+ }
5
+ export declare class Withdrawal extends PaytonzSDKBase {
6
+ constructor(parameters: PaytonzSDKOptions);
7
+ create(options: CreateWithdrawalOptions, accessToken?: string): Promise<CreateWithdrawalInfo>;
8
+ nftTransfer(options: CreateNftWithdrawalOptions, accessToken?: string): Promise<CreateWithdrawalInfo>;
9
+ internalTransfer(options: InternalTransferOptions, accessToken?: string): Promise<CreateWithdrawalInfo>;
10
+ estimateFee(options: EstimateFeeOptions, accessToken?: string): Promise<EstimateFeeInfo>;
11
+ estimateNftFee(options: EstimateFeeNftOptions, accessToken?: string): Promise<EstimateFeeNftInfo>;
12
+ estimateMax(options: EstimateMaxOptions, accessToken?: string): Promise<EstimateMaxInfo>;
13
+ }
14
+ export default Withdrawal;
@@ -0,0 +1,79 @@
1
+ import { AccessWalletOptions } from "src/interfaces/paytonz.interface";
2
+ export interface CreateWithdrawalInfo {
3
+ id: string;
4
+ }
5
+ export interface EstimateFeeInfo {
6
+ fee: string;
7
+ minerFee: string;
8
+ systemFee?: string;
9
+ merchantFee?: string;
10
+ currencyMinerFee: string;
11
+ currencyFee: string;
12
+ }
13
+ export interface EstimateFeeNftInfo {
14
+ fee: string;
15
+ minerFee: string;
16
+ systemFee: string;
17
+ currencyMinerFee: string;
18
+ currencySystemFee: string;
19
+ }
20
+ export interface EstimateMaxInfo {
21
+ amount: string;
22
+ }
23
+ export declare enum PriorityTx {
24
+ LOW = "low",
25
+ MEDIUM = "medium",
26
+ HIGH = "high"
27
+ }
28
+ export interface CreateWithdrawalOptions extends AccessWalletOptions {
29
+ to: string;
30
+ amount: string;
31
+ priority?: PriorityTx;
32
+ currencyToken?: string;
33
+ payerFeePrivateKey?: string;
34
+ comment?: string;
35
+ isSmartAccount?: boolean;
36
+ paymasterCurrencyToken?: string;
37
+ isGasless?: boolean;
38
+ idempotencyKey?: string;
39
+ }
40
+ export interface InternalTransferOptions {
41
+ to: string;
42
+ amount: string;
43
+ idempotencyKey?: string;
44
+ }
45
+ export interface EstimateFeeOptions extends AccessWalletOptions {
46
+ to: string;
47
+ amount: string;
48
+ priority?: PriorityTx;
49
+ currencyToken?: string;
50
+ payerFeePrivateKey?: string;
51
+ isSmartAccount?: boolean;
52
+ paymasterCurrencyToken?: string;
53
+ isGasless?: boolean;
54
+ }
55
+ declare enum TypeNFt {
56
+ ERC_721 = "erc721",
57
+ ERC_1155 = "erc1155"
58
+ }
59
+ export interface EstimateFeeNftOptions extends AccessWalletOptions {
60
+ to: string;
61
+ amount: string;
62
+ contractAddress: string;
63
+ tokenId: number;
64
+ type: TypeNFt;
65
+ payerFeePrivateKey?: string;
66
+ idempotencyKey?: string;
67
+ }
68
+ export interface CreateNftWithdrawalOptions extends EstimateFeeNftOptions {
69
+ }
70
+ export interface EstimateMaxOptions extends AccessWalletOptions {
71
+ to: string;
72
+ currencyToken?: string;
73
+ payerFeePrivateKey?: string;
74
+ priority?: PriorityTx;
75
+ isSmartAccount?: boolean;
76
+ paymasterCurrencyToken?: string;
77
+ isGasless?: boolean;
78
+ }
79
+ export {};
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PriorityTx = void 0;
4
+ var PriorityTx;
5
+ (function (PriorityTx) {
6
+ PriorityTx["LOW"] = "low";
7
+ PriorityTx["MEDIUM"] = "medium";
8
+ PriorityTx["HIGH"] = "high";
9
+ })(PriorityTx = exports.PriorityTx || (exports.PriorityTx = {}));
10
+ var TypeNFt;
11
+ (function (TypeNFt) {
12
+ TypeNFt["ERC_721"] = "erc721";
13
+ TypeNFt["ERC_1155"] = "erc1155";
14
+ })(TypeNFt || (TypeNFt = {}));
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Withdrawal = void 0;
13
+ const PaytonzSDKBase_1 = require("../PaytonzSDKBase");
14
+ class Withdrawal extends PaytonzSDKBase_1.PaytonzSDKBase {
15
+ constructor(parameters) {
16
+ super(parameters);
17
+ }
18
+ create(options, accessToken) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ try {
21
+ if (!accessToken) {
22
+ accessToken = yield this.getToken(true);
23
+ }
24
+ const path = `/api/public/withdrawal`;
25
+ return this.auth_post(`${path}`, options, accessToken);
26
+ }
27
+ catch (err) {
28
+ throw err;
29
+ }
30
+ });
31
+ }
32
+ nftTransfer(options, accessToken) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ try {
35
+ if (!accessToken) {
36
+ accessToken = yield this.getToken(true);
37
+ }
38
+ const path = `/api/public/withdrawal/nft`;
39
+ return this.auth_post(`${path}`, options, accessToken);
40
+ }
41
+ catch (err) {
42
+ throw err;
43
+ }
44
+ });
45
+ }
46
+ internalTransfer(options, accessToken) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ try {
49
+ if (!accessToken) {
50
+ accessToken = yield this.getToken(true);
51
+ }
52
+ const path = `/api/public/withdrawal/internal`;
53
+ return this.auth_post(`${path}`, options, accessToken);
54
+ }
55
+ catch (err) {
56
+ throw err;
57
+ }
58
+ });
59
+ }
60
+ estimateFee(options, accessToken) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ try {
63
+ if (!accessToken) {
64
+ accessToken = yield this.getToken(true);
65
+ }
66
+ const path = `/api/public/transaction/fee`;
67
+ return this.auth_post(`${path}`, options, accessToken);
68
+ }
69
+ catch (err) {
70
+ throw err;
71
+ }
72
+ });
73
+ }
74
+ estimateNftFee(options, accessToken) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ try {
77
+ if (!accessToken) {
78
+ accessToken = yield this.getToken(true);
79
+ }
80
+ const path = `/api/public/transaction/feeNft`;
81
+ return this.auth_post(`${path}`, options, accessToken);
82
+ }
83
+ catch (err) {
84
+ throw err;
85
+ }
86
+ });
87
+ }
88
+ estimateMax(options, accessToken) {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ try {
91
+ if (!accessToken) {
92
+ accessToken = yield this.getToken(true);
93
+ }
94
+ const path = `/api/public/withdrawal/estimate-max`;
95
+ return this.auth_post(`${path}`, options, accessToken);
96
+ }
97
+ catch (err) {
98
+ throw err;
99
+ }
100
+ });
101
+ }
102
+ }
103
+ exports.Withdrawal = Withdrawal;
104
+ exports.default = Withdrawal;
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "author": {
3
+ "name": "paytonz"
4
+ },
5
+ "bundleDependencies": [],
6
+ "dependencies": {
7
+ "dayjs": "^1.10.3",
8
+ "got": "^11.8.2",
9
+ "lodash": "^4.17.21",
10
+ "typescript-memoize": "^1.1.1"
11
+ },
12
+ "deprecated": false,
13
+ "description": "PAYTONZ REST API",
14
+ "devDependencies": {
15
+ "typescript": "^4.1.3"
16
+ },
17
+ "license": "ISC",
18
+ "main": "lib/index.js",
19
+ "name": "paytonz-node-api-sdk",
20
+ "scripts": {
21
+ "build": "tsc --build tsconfig.json",
22
+ "publish:patch": "npm version patch && npm publish",
23
+ "deploy": "npm run build && npm publish"
24
+ },
25
+ "version": "1.0.0"
26
+ }
@@ -0,0 +1,41 @@
1
+ import { Auth } from "./auth/auth";
2
+ import Checkout from "./checkout/checkout";
3
+ import { PaytonzSDKBaseOptions } from "./PaytonzSDKBase";
4
+ import { Currency } from "./currency/currency";
5
+ import External from "./external/external";
6
+ import { Multisend } from "./multisend/multisend";
7
+ import { Swap } from "./swap/swap";
8
+ import Transaction from "./transaction/transaction";
9
+ import { Wallet } from "./wallet/wallet";
10
+ import { Withdrawal } from "./withdrawal/withdrawal";
11
+
12
+ export interface PaytonzSDKOptions extends PaytonzSDKBaseOptions {}
13
+
14
+ export class PaytonzSDK {
15
+ /**
16
+ * @param parameters
17
+ */
18
+ constructor(parameters: PaytonzSDKOptions) {
19
+ this.wallet = new Wallet(parameters);
20
+ this.currency = new Currency(parameters);
21
+ this.withdrawal = new Withdrawal(parameters);
22
+ this.multisend = new Multisend(parameters);
23
+ this.checkout = new Checkout(parameters);
24
+ this.transaction = new Transaction(parameters);
25
+ this.externalCall = new External(parameters);
26
+ this.auth = new Auth(parameters);
27
+ this.swap = new Swap(parameters);
28
+ }
29
+
30
+ wallet: Wallet;
31
+ currency: Currency;
32
+ withdrawal: Withdrawal;
33
+ multisend: Multisend;
34
+ checkout: Checkout;
35
+ transaction: Transaction;
36
+ externalCall: External;
37
+ auth: Auth;
38
+ swap: Swap;
39
+ }
40
+
41
+ export default PaytonzSDK;
@@ -0,0 +1,252 @@
1
+ import dayjs from "dayjs";
2
+ import utc from "dayjs/plugin/utc";
3
+ import _ from "lodash";
4
+
5
+ import { REST_URL } from "./constant";
6
+ import { PaytonzToken, IGetToken } from "./interfaces/paytonz.interface";
7
+ import { request, Options as HttpOptions } from "./utils/httpClient";
8
+
9
+ dayjs.extend(utc);
10
+
11
+ export interface PaytonzSDKBaseOptions {
12
+ publicKey?: string;
13
+ privateKey?: string;
14
+
15
+ walletId?: string;
16
+ passphrase?: string;
17
+
18
+ errLogger?: (mssage: string, ...arg: any[]) => void;
19
+
20
+ outLogger?: (mssage: string, ...arg: any[]) => void;
21
+
22
+ httpOptions?: HttpOptions;
23
+ url?: {
24
+ rest?: string;
25
+ };
26
+ }
27
+
28
+ const DEFAUTL_HTTP_OPTIONS = {
29
+ headers: {
30
+ "Content-Type": "application/json",
31
+ "User-Agent":
32
+ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36",
33
+ },
34
+ timeout: 6000,
35
+ };
36
+
37
+ export class PaytonzSDKBase {
38
+ protected options: Required<PaytonzSDKBaseOptions> =
39
+ {} as Required<PaytonzSDKBaseOptions>;
40
+
41
+ constructor(options?: Partial<PaytonzSDKBaseOptions>) {
42
+ if (!options) {
43
+ return;
44
+ }
45
+ this.setOptions(options);
46
+ }
47
+ protected setOptions(options: Partial<PaytonzSDKBaseOptions> = {}) {
48
+ const { httpOptions, url, ...otherOptions } = options;
49
+
50
+ _.merge(this.options, {
51
+ httpOptions: _.merge({}, DEFAUTL_HTTP_OPTIONS, httpOptions || {}),
52
+ url: {
53
+ rest: REST_URL,
54
+ ...(url || {}),
55
+ },
56
+ });
57
+ if (otherOptions) {
58
+ _.merge(this.options, otherOptions);
59
+ }
60
+ }
61
+ protected _request = <T>(path: string, options: HttpOptions): Promise<T> => {
62
+ return request<T>(path, _.merge({}, this.options.httpOptions, options))
63
+ .then((data: any) => {
64
+ try {
65
+ const json = JSON.parse(data);
66
+ if (json) {
67
+ return json.data || json;
68
+ } else {
69
+ this.errLogger(
70
+ options.method as string,
71
+ "-",
72
+ path,
73
+ json.data.message
74
+ );
75
+ return Promise.reject(json.data.message);
76
+ }
77
+ } catch (error) {
78
+ this.errLogger(
79
+ options.method as string,
80
+ "-",
81
+ path,
82
+ "Parse Error",
83
+ error
84
+ );
85
+ return Promise.reject(error);
86
+ }
87
+ })
88
+ .catch((err) => {
89
+ this.errLogger(options.method as string, "-", path, err);
90
+ return Promise.reject(err);
91
+ });
92
+ };
93
+
94
+ protected request = <T>(path: string, options: HttpOptions): Promise<T> => {
95
+ if (!this.options.url.rest) {
96
+ return Promise.reject("Rest url is required.");
97
+ }
98
+ return this._request<T>(`${this.options.url.rest}${path}`, options);
99
+ };
100
+
101
+ protected auth_get = <T = any>(
102
+ path: string,
103
+ params: Record<string, any> = {} as Record<string, any>,
104
+ token: string
105
+ ) => {
106
+ if (!this.options.url.rest) {
107
+ return Promise.reject("Rest url is required.");
108
+ }
109
+ const PATH = `${this.options.url.rest}${path}`;
110
+
111
+ return this._request<T>(PATH, {
112
+ method: "GET",
113
+ headers: {
114
+ Authorization: `Bearer ${token}`,
115
+ },
116
+ searchParams: params,
117
+ });
118
+ };
119
+
120
+ protected auth_post = <T = any>(
121
+ path: string,
122
+ data: Record<string, any>,
123
+ token: string
124
+ ) => {
125
+ const PATH = `${this.options.url.rest}${path}`;
126
+
127
+ const { idempotencyKey, ...payload } = data;
128
+
129
+ const headers: Record<string, string> = {
130
+ Authorization: `Bearer ${token}`,
131
+ };
132
+
133
+ if (idempotencyKey) {
134
+ headers["idempotency-key"] = idempotencyKey;
135
+ }
136
+
137
+ return this._request<T>(PATH, {
138
+ method: "POST",
139
+ headers,
140
+ json: payload,
141
+ });
142
+ };
143
+
144
+ protected post = <T = any>(path: string, data: Record<string, any>) => {
145
+ const PATH = `${this.options.url.rest}${path}`;
146
+
147
+ return this._request<T>(PATH, {
148
+ method: "POST",
149
+ json: data,
150
+ });
151
+ };
152
+
153
+ protected auth_delete = <T = any>(
154
+ path: string,
155
+ data: Record<string, any>,
156
+ token: string
157
+ ) => {
158
+ const PATH = `${this.options.url.rest}${path}`;
159
+
160
+ return this._request<T>(PATH, {
161
+ method: "DELETE",
162
+ headers: {
163
+ Authorization: `Bearer ${token}`,
164
+ },
165
+ json: data,
166
+ });
167
+ };
168
+
169
+ protected auth_patch = <T = any>(
170
+ path: string,
171
+ data: Record<string, any>,
172
+ token: string
173
+ ) => {
174
+ const PATH = `${this.options.url.rest}${path}`;
175
+
176
+ return this._request<T>(PATH, {
177
+ method: "PATCH",
178
+ headers: {
179
+ Authorization: `Bearer ${token}`,
180
+ },
181
+ json: data,
182
+ });
183
+ };
184
+
185
+ protected errLogger = (msg: string, ...arg: any[]) => {
186
+ if (typeof this.options.errLogger === "function") {
187
+ this.options.errLogger(msg, ...arg);
188
+ return;
189
+ }
190
+ const prefix = `[${dayjs()
191
+ .utcOffset(8)
192
+ .format("YYYY-MM-DD HH:mm:ss")}] [ERROR] `;
193
+ console.error(`${prefix} ${msg}`, ...arg);
194
+ };
195
+
196
+ protected outLogger = (msg: string, ...arg: any[]) => {
197
+ if (typeof this.options.outLogger === "function") {
198
+ this.options.outLogger(msg, ...arg);
199
+ return;
200
+ }
201
+ const prefix = `[${dayjs()
202
+ .utcOffset(8)
203
+ .format("YYYY-MM-DD HH:mm:ss")}] [INFO] `;
204
+
205
+ console.log(`${prefix} ${msg}`, ...arg);
206
+ };
207
+
208
+ protected auth(
209
+ publicKey: string,
210
+ privateKey: string,
211
+ walletId?: string,
212
+ passphrase?: string
213
+ ): Promise<PaytonzToken> {
214
+ if (this.options.publicKey && this.options.privateKey) {
215
+ const path = `/api/public/auth`;
216
+ let data = {
217
+ publicKey,
218
+ privateKey,
219
+ };
220
+
221
+ if (walletId) {
222
+ data = Object.assign(data, { walletId });
223
+ if (!passphrase) {
224
+ throw new Error("Passphrase is required.");
225
+ }
226
+ data = Object.assign(data, { passphrase });
227
+ }
228
+
229
+ return this.request<PaytonzToken>(`${path}`, {
230
+ method: "POST",
231
+ json: data,
232
+ });
233
+ } else {
234
+ throw new Error("Keys is required.");
235
+ }
236
+ }
237
+
238
+ protected async getToken(isWallet = false): Promise<string> {
239
+ if (isWallet) {
240
+ if (!this.options.walletId || !this.options.passphrase) {
241
+ throw new Error("WalletId and passphrase is required.");
242
+ }
243
+ }
244
+ const { token } = await this.auth(
245
+ this.options.publicKey,
246
+ this.options.privateKey,
247
+ this.options?.walletId,
248
+ this.options?.passphrase
249
+ );
250
+ return token;
251
+ }
252
+ }
@@ -0,0 +1,26 @@
1
+ export interface RegisterOptions {
2
+ email: string;
3
+ password: string;
4
+ inviterId?: string;
5
+ roles: string[];
6
+ agent?: string;
7
+ }
8
+
9
+ export interface RegisterInfo {
10
+ wallets: {
11
+ id: string;
12
+ address: string;
13
+ privateKey: string;
14
+ mnemonic: string;
15
+ actualBalance: string;
16
+ systemBalance: string;
17
+ passphrase: string;
18
+ nodeType: string;
19
+ }[];
20
+ apiKey: {
21
+ publicKey: string;
22
+ privateKey: string;
23
+ };
24
+ merchantId: string;
25
+ userId: string;
26
+ }
@@ -0,0 +1,31 @@
1
+ import { PaytonzSDKBase, PaytonzSDKBaseOptions } from "../PaytonzSDKBase";
2
+ import { RegisterInfo, RegisterOptions } from "./auth.interface";
3
+
4
+ export interface PaytonzSDKOptions extends PaytonzSDKBaseOptions {}
5
+
6
+ export class Auth extends PaytonzSDKBase {
7
+ constructor(parameters: PaytonzSDKOptions) {
8
+ super(parameters);
9
+ }
10
+
11
+ async register(options: RegisterOptions): Promise<RegisterInfo> {
12
+ try {
13
+ const path = `/api/public/auth/register`;
14
+
15
+ return this.post<RegisterInfo>(`${path}`, { ...options });
16
+ } catch (err) {
17
+ throw err;
18
+ }
19
+ }
20
+
21
+ async login(): Promise<string> {
22
+ const { token } = await this.auth(
23
+ this.options.publicKey,
24
+ this.options.privateKey,
25
+ this.options?.walletId,
26
+ this.options?.passphrase
27
+ );
28
+
29
+ return token;
30
+ }
31
+ }