@ardrive/turbo-sdk 1.7.1 → 1.8.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 (46) hide show
  1. package/bundles/web.bundle.min.js +68876 -50766
  2. package/lib/cjs/common/factory.js +98 -0
  3. package/lib/cjs/common/index.js +1 -1
  4. package/lib/cjs/common/payment.js +5 -12
  5. package/lib/cjs/common/signer.js +14 -0
  6. package/lib/cjs/common/{token.js → token/arweave.js} +13 -10
  7. package/lib/cjs/common/token/index.js +26 -0
  8. package/lib/cjs/common/token/solana.js +112 -0
  9. package/lib/cjs/node/factory.js +0 -49
  10. package/lib/cjs/types.js +4 -2
  11. package/lib/cjs/version.js +1 -1
  12. package/lib/cjs/web/factory.js +46 -13
  13. package/lib/esm/common/factory.js +101 -3
  14. package/lib/esm/common/index.js +1 -1
  15. package/lib/esm/common/payment.js +5 -12
  16. package/lib/esm/common/signer.js +11 -0
  17. package/lib/esm/common/{token.js → token/arweave.js} +13 -10
  18. package/lib/esm/common/token/index.js +9 -0
  19. package/lib/esm/common/token/solana.js +103 -0
  20. package/lib/esm/node/factory.js +0 -49
  21. package/lib/esm/types.js +2 -1
  22. package/lib/esm/version.js +1 -1
  23. package/lib/esm/web/factory.js +47 -14
  24. package/lib/types/common/factory.d.ts +5 -18
  25. package/lib/types/common/factory.d.ts.map +1 -1
  26. package/lib/types/common/index.d.ts +1 -1
  27. package/lib/types/common/index.d.ts.map +1 -1
  28. package/lib/types/common/payment.d.ts +3 -3
  29. package/lib/types/common/payment.d.ts.map +1 -1
  30. package/lib/types/common/signer.d.ts.map +1 -1
  31. package/lib/types/common/{token.d.ts → token/arweave.d.ts} +12 -15
  32. package/lib/types/common/token/arweave.d.ts.map +1 -0
  33. package/lib/types/common/token/index.d.ts +21 -0
  34. package/lib/types/common/token/index.d.ts.map +1 -0
  35. package/lib/types/common/token/solana.d.ts +37 -0
  36. package/lib/types/common/token/solana.d.ts.map +1 -0
  37. package/lib/types/node/factory.d.ts +16 -3
  38. package/lib/types/node/factory.d.ts.map +1 -1
  39. package/lib/types/types.d.ts +31 -17
  40. package/lib/types/types.d.ts.map +1 -1
  41. package/lib/types/version.d.ts +1 -1
  42. package/lib/types/version.d.ts.map +1 -1
  43. package/lib/types/web/factory.d.ts +4 -2
  44. package/lib/types/web/factory.d.ts.map +1 -1
  45. package/package.json +4 -2
  46. package/lib/types/common/token.d.ts.map +0 -1
@@ -1,8 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TurboBaseFactory = void 0;
4
+ /**
5
+ * Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved.
6
+ *
7
+ * This program is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU Affero General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU Affero General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Affero General Public License
18
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
+ */
20
+ const arbundles_1 = require("arbundles");
21
+ const signer_js_1 = require("../node/signer.js");
22
+ const types_js_1 = require("../types.js");
23
+ const signer_js_2 = require("../web/signer.js");
4
24
  const logger_js_1 = require("./logger.js");
5
25
  const payment_js_1 = require("./payment.js");
26
+ const index_js_1 = require("./token/index.js");
6
27
  const turbo_js_1 = require("./turbo.js");
7
28
  const upload_js_1 = require("./upload.js");
8
29
  class TurboBaseFactory {
@@ -26,6 +47,83 @@ class TurboBaseFactory {
26
47
  paymentService,
27
48
  });
28
49
  }
50
+ static getSigner(providedSigner, providedPrivateKey, token) {
51
+ let signer;
52
+ if (providedSigner !== undefined) {
53
+ signer = providedSigner;
54
+ }
55
+ else if (providedPrivateKey !== undefined) {
56
+ if (token === 'solana') {
57
+ signer = new arbundles_1.HexSolanaSigner(providedPrivateKey);
58
+ // TODO: else if (token === 'ethereum') {signer = new EthereumSigner(providedPrivateKey);}
59
+ }
60
+ else {
61
+ if (!(0, types_js_1.isJWK)(providedPrivateKey)) {
62
+ throw new Error('A JWK must be provided for ArweaveSigner.');
63
+ }
64
+ signer = new arbundles_1.ArweaveSigner(providedPrivateKey);
65
+ }
66
+ }
67
+ else {
68
+ throw new Error('A privateKey or signer must be provided.');
69
+ }
70
+ if (typeof window !== 'undefined') {
71
+ return new signer_js_2.TurboWebArweaveSigner({
72
+ signer,
73
+ logger: this.logger,
74
+ });
75
+ }
76
+ return new signer_js_1.TurboNodeSigner({
77
+ signer,
78
+ logger: this.logger,
79
+ });
80
+ }
81
+ static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, tokenMap, gatewayUrl, tokenTools, }) {
82
+ if (!token) {
83
+ if (providedSigner) {
84
+ // Derive token from signer if not provided
85
+ if (providedSigner instanceof arbundles_1.EthereumSigner) {
86
+ token = 'ethereum';
87
+ }
88
+ else if (providedSigner instanceof arbundles_1.HexSolanaSigner) {
89
+ token = 'solana';
90
+ }
91
+ else {
92
+ token = 'arweave';
93
+ }
94
+ }
95
+ else {
96
+ token = 'arweave';
97
+ }
98
+ }
99
+ const turboSigner = this.getSigner(providedSigner, privateKey, token);
100
+ if (!tokenTools) {
101
+ if (tokenMap && token === 'arweave') {
102
+ tokenTools = tokenMap.arweave;
103
+ }
104
+ tokenTools = index_js_1.defaultTokenMap[token]?.({
105
+ gatewayUrl,
106
+ logger: this.logger,
107
+ });
108
+ }
109
+ const paymentService = new payment_js_1.TurboAuthenticatedPaymentService({
110
+ ...paymentServiceConfig,
111
+ signer: turboSigner,
112
+ logger: this.logger,
113
+ token,
114
+ tokenTools,
115
+ });
116
+ const uploadService = new upload_js_1.TurboAuthenticatedUploadService({
117
+ ...uploadServiceConfig,
118
+ signer: turboSigner,
119
+ logger: this.logger,
120
+ token,
121
+ });
122
+ return new turbo_js_1.TurboAuthenticatedClient({
123
+ uploadService,
124
+ paymentService,
125
+ });
126
+ }
29
127
  }
30
128
  exports.TurboBaseFactory = TurboBaseFactory;
31
129
  TurboBaseFactory.logger = new logger_js_1.TurboWinstonLogger();
@@ -34,4 +34,4 @@ __exportStar(require("./upload.js"), exports);
34
34
  __exportStar(require("./payment.js"), exports);
35
35
  __exportStar(require("./turbo.js"), exports);
36
36
  __exportStar(require("./currency.js"), exports);
37
- __exportStar(require("./token.js"), exports);
37
+ __exportStar(require("./token/index.js"), exports);
@@ -20,7 +20,6 @@ exports.TurboAuthenticatedPaymentService = exports.TurboUnauthenticatedPaymentSe
20
20
  const bignumber_js_1 = require("bignumber.js");
21
21
  const http_js_1 = require("./http.js");
22
22
  const logger_js_1 = require("./logger.js");
23
- const token_js_1 = require("./token.js");
24
23
  exports.developmentPaymentServiceURL = 'https://payment.ardrive.dev';
25
24
  exports.defaultPaymentServiceURL = 'https://payment.ardrive.io';
26
25
  class TurboUnauthenticatedPaymentService {
@@ -134,14 +133,10 @@ class TurboUnauthenticatedPaymentService {
134
133
  exports.TurboUnauthenticatedPaymentService = TurboUnauthenticatedPaymentService;
135
134
  // NOTE: to avoid redundancy, we use inheritance here - but generally prefer composition over inheritance
136
135
  class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymentService {
137
- constructor({ url = exports.defaultPaymentServiceURL, retryConfig, signer, logger = new logger_js_1.TurboWinstonLogger(), token = 'arweave', tokenMap = {
138
- arweave: new token_js_1.ArweaveToken({
139
- logger,
140
- }),
141
- }, }) {
136
+ constructor({ url = exports.defaultPaymentServiceURL, retryConfig, signer, logger = new logger_js_1.TurboWinstonLogger(), token = 'arweave', tokenTools, }) {
142
137
  super({ url, retryConfig, logger, token });
143
138
  this.signer = signer;
144
- this.tokenMap = tokenMap;
139
+ this.tokenTools = tokenTools;
145
140
  }
146
141
  async getBalance() {
147
142
  const headers = await this.signer.generateSignedRequestHeaders();
@@ -173,7 +168,7 @@ class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymentServic
173
168
  return walletAddress;
174
169
  }
175
170
  async topUpWithTokens({ feeMultiplier = 1, tokenAmount: tokenAmountV, }) {
176
- if (!this.tokenMap[this.token]) {
171
+ if (!this.tokenTools) {
177
172
  throw new Error(`Token type not supported for crypto fund ${this.token}`);
178
173
  }
179
174
  const tokenAmount = new bignumber_js_1.BigNumber(tokenAmountV);
@@ -183,18 +178,16 @@ class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymentServic
183
178
  tokenAmount,
184
179
  target,
185
180
  });
186
- const fundTx = await this.tokenMap[this.token].createSignedTx({
181
+ const fundTx = await this.tokenTools.createAndSubmitTx({
187
182
  target,
188
183
  tokenAmount,
189
184
  feeMultiplier,
190
185
  signer: this.signer,
191
186
  });
192
187
  const txId = fundTx.id;
193
- this.logger.debug('Submitting fund transaction...', { txId });
194
- await this.tokenMap[this.token].submitTx({ tx: fundTx });
195
188
  try {
196
189
  // Let transaction settle some time
197
- await this.tokenMap[this.token].pollForTxBeingAvailable({ txId });
190
+ await this.tokenTools.pollForTxBeingAvailable({ txId });
198
191
  return {
199
192
  ...(await this.submitFundTransaction({ txId })),
200
193
  target: fundTx.target,
@@ -1,4 +1,7 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.TurboDataItemAbstractSigner = void 0;
4
7
  /**
@@ -19,6 +22,7 @@ exports.TurboDataItemAbstractSigner = void 0;
19
22
  */
20
23
  const arbundles_1 = require("arbundles");
21
24
  const crypto_1 = require("crypto");
25
+ const tweetnacl_1 = __importDefault(require("tweetnacl"));
22
26
  const base64_js_1 = require("../utils/base64.js");
23
27
  /**
24
28
  * Abstract class for signing TurboDataItems.
@@ -62,6 +66,16 @@ class TurboDataItemAbstractSigner {
62
66
  return this.signer.publicKey;
63
67
  }
64
68
  async signData(dataToSign) {
69
+ if (this.signer instanceof arbundles_1.HexSolanaSigner) {
70
+ const privateKey = this.signer.key;
71
+ const publicKey = Uint8Array.from(await this.getPublicKey());
72
+ // Concatenate the private and public keys correctly
73
+ const combinedKey = new Uint8Array(privateKey.length + publicKey.length);
74
+ combinedKey.set(privateKey);
75
+ combinedKey.set(publicKey, privateKey.length);
76
+ const signature = tweetnacl_1.default.sign.detached(dataToSign, combinedKey);
77
+ return signature;
78
+ }
65
79
  return this.signer.sign(dataToSign);
66
80
  }
67
81
  }
@@ -22,23 +22,23 @@ exports.ARToTokenAmount = exports.WinstonToTokenAmount = exports.ArweaveToken =
22
22
  */
23
23
  const arweave_1 = __importDefault(require("@irys/arweave"));
24
24
  const bignumber_js_1 = require("bignumber.js");
25
- const base64_js_1 = require("../utils/base64.js");
26
- const common_js_1 = require("../utils/common.js");
27
- const logger_js_1 = require("./logger.js");
25
+ const base64_js_1 = require("../../utils/base64.js");
26
+ const common_js_1 = require("../../utils/common.js");
27
+ const logger_js_1 = require("../logger.js");
28
28
  class ArweaveToken {
29
- constructor({ arweave = arweave_1.default.init({
30
- url: 'https://arweave.net',
29
+ constructor({ gatewayUrl = 'https://arweave.net', arweave = arweave_1.default.init({
30
+ url: gatewayUrl,
31
31
  }), logger = new logger_js_1.TurboWinstonLogger(), mintU = true, pollingOptions = {
32
32
  maxAttempts: 10,
33
33
  pollingIntervalMs: 3000,
34
34
  initialBackoffMs: 7000,
35
- }, }) {
35
+ }, } = {}) {
36
36
  this.arweave = arweave;
37
37
  this.logger = logger;
38
38
  this.mintU = mintU;
39
39
  this.pollingOptions = pollingOptions;
40
40
  }
41
- async createSignedTx({ feeMultiplier, target, tokenAmount, signer, }) {
41
+ async createAndSubmitTx({ feeMultiplier, target, tokenAmount, signer, }) {
42
42
  const tx = await this.arweave.createTransaction({
43
43
  target,
44
44
  quantity: tokenAmount.toString(),
@@ -66,7 +66,9 @@ class ArweaveToken {
66
66
  owner: publicKeyB64Url,
67
67
  signature: (0, base64_js_1.toB64Url)(signatureBuffer),
68
68
  });
69
- return tx;
69
+ this.logger.debug('Submitting fund transaction...', { id });
70
+ await this.submitTx(tx);
71
+ return { id, target, reward: tx.reward };
70
72
  }
71
73
  async pollForTxBeingAvailable({ txId, }) {
72
74
  const { maxAttempts, pollingIntervalMs, initialBackoffMs } = this.pollingOptions;
@@ -111,12 +113,13 @@ class ArweaveToken {
111
113
  }
112
114
  throw new Error('Transaction not found after polling, transaction id: ' + txId);
113
115
  }
114
- async submitTx({ tx }) {
116
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
117
+ async submitTx(tx) {
115
118
  try {
116
119
  const response = await this.arweave.transactions.post(tx);
117
120
  if (response.status !== 200) {
118
121
  throw new Error('Failed to post transaction -- ' +
119
- `Status ${response.status}, ${response.statusText}`);
122
+ `Status ${response.status}, ${response.statusText}, ${response.data}`);
120
123
  }
121
124
  this.logger.debug('Successfully posted fund transaction...', { tx });
122
125
  }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.defaultTokenMap = void 0;
18
+ const arweave_js_1 = require("./arweave.js");
19
+ const solana_js_1 = require("./solana.js");
20
+ exports.defaultTokenMap = {
21
+ arweave: (config) => new arweave_js_1.ArweaveToken(config),
22
+ solana: (config) => new solana_js_1.SolanaToken(config),
23
+ // ethereum: (config: TokenConfig) => new EthereumToken(config)
24
+ };
25
+ __exportStar(require("./arweave.js"), exports);
26
+ __exportStar(require("./solana.js"), exports);
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SolanaToken = exports.SOLToTokenAmount = exports.lamportToTokenAmount = void 0;
7
+ /**
8
+ * Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved.
9
+ *
10
+ * This program is free software: you can redistribute it and/or modify
11
+ * it under the terms of the GNU Affero General Public License as published by
12
+ * the Free Software Foundation, either version 3 of the License, or
13
+ * (at your option) any later version.
14
+ *
15
+ * This program is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ * GNU Affero General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU Affero General Public License
21
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
22
+ */
23
+ const web3_js_1 = require("@solana/web3.js");
24
+ const bignumber_js_1 = require("bignumber.js");
25
+ const bs58_1 = __importDefault(require("bs58"));
26
+ const common_js_1 = require("../../utils/common.js");
27
+ const logger_js_1 = require("../logger.js");
28
+ const lamportToTokenAmount = (winston) => winston;
29
+ exports.lamportToTokenAmount = lamportToTokenAmount;
30
+ const SOLToTokenAmount = (sol) => new bignumber_js_1.BigNumber(sol).times(1e9).valueOf();
31
+ exports.SOLToTokenAmount = SOLToTokenAmount;
32
+ class SolanaToken {
33
+ constructor({ logger = new logger_js_1.TurboWinstonLogger(), gatewayUrl = 'https://api.mainnet-beta.solana.com', pollingOptions = {
34
+ maxAttempts: 10,
35
+ pollingIntervalMs: 5000,
36
+ initialBackoffMs: 7000,
37
+ }, } = {}) {
38
+ this.logger = logger;
39
+ this.gatewayUrl = gatewayUrl;
40
+ this.connection = new web3_js_1.Connection(gatewayUrl, 'confirmed');
41
+ this.pollingOptions = pollingOptions;
42
+ }
43
+ async createAndSubmitTx({ target, tokenAmount, signer, }) {
44
+ const publicKey = new web3_js_1.PublicKey(bs58_1.default.encode(await signer.getPublicKey()));
45
+ const tx = new web3_js_1.Transaction({
46
+ feePayer: publicKey,
47
+ ...(await this.connection.getLatestBlockhash()),
48
+ });
49
+ tx.add(web3_js_1.SystemProgram.transfer({
50
+ fromPubkey: publicKey,
51
+ toPubkey: new web3_js_1.PublicKey(target),
52
+ lamports: +new bignumber_js_1.BigNumber(tokenAmount),
53
+ }));
54
+ const serializedTx = tx.serializeMessage();
55
+ const signature = await signer.signData(serializedTx);
56
+ tx.addSignature(publicKey, Buffer.from(signature));
57
+ const id = bs58_1.default.encode(signature);
58
+ await this.submitTx(tx, id);
59
+ return { id, target };
60
+ }
61
+ async submitTx(tx, id) {
62
+ this.logger.debug('Submitting fund transaction...', { id });
63
+ await this.connection.sendRawTransaction(tx.serialize(), {
64
+ maxRetries: this.pollingOptions.maxAttempts,
65
+ });
66
+ if (tx.recentBlockhash === undefined ||
67
+ tx.lastValidBlockHeight === undefined) {
68
+ throw new Error('Failed to submit Transaction -- missing blockhash or lastValidBlockHeight from transaction creation. Solana Gateway Url:' +
69
+ this.gatewayUrl);
70
+ }
71
+ await this.connection.confirmTransaction({
72
+ signature: id,
73
+ blockhash: tx.recentBlockhash,
74
+ lastValidBlockHeight: tx.lastValidBlockHeight,
75
+ }, 'finalized');
76
+ }
77
+ async pollForTxBeingAvailable({ txId, }) {
78
+ const { maxAttempts, pollingIntervalMs, initialBackoffMs } = this.pollingOptions;
79
+ this.logger.debug('Polling for transaction...', {
80
+ txId,
81
+ pollingOptions: this.pollingOptions,
82
+ });
83
+ await (0, common_js_1.sleep)(initialBackoffMs);
84
+ let attempts = 0;
85
+ while (attempts < maxAttempts) {
86
+ let status = undefined;
87
+ attempts++;
88
+ try {
89
+ status = await this.connection.getSignatureStatus(txId);
90
+ }
91
+ catch (err) {
92
+ // Continue retries when request errors
93
+ this.logger.debug('Failed to poll for transaction...', { err });
94
+ }
95
+ if (status && status.value && status.value.err !== null) {
96
+ throw new Error(`Transaction failed: ${status.value.err}`);
97
+ }
98
+ if (status && status.value && status.value.slot !== null) {
99
+ return;
100
+ }
101
+ this.logger.debug('Transaction not found, polling...', {
102
+ txId,
103
+ attempts,
104
+ maxAttempts,
105
+ pollingIntervalMs,
106
+ });
107
+ await (0, common_js_1.sleep)(pollingIntervalMs);
108
+ }
109
+ throw new Error('Transaction not found after polling, transaction id: ' + txId);
110
+ }
111
+ }
112
+ exports.SolanaToken = SolanaToken;
@@ -17,56 +17,7 @@ exports.TurboFactory = void 0;
17
17
  * You should have received a copy of the GNU Affero General Public License
18
18
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
  */
20
- const arbundles_1 = require("arbundles");
21
20
  const factory_js_1 = require("../common/factory.js");
22
- const index_js_1 = require("../common/index.js");
23
- const signer_js_1 = require("../web/signer.js");
24
- const signer_js_2 = require("./signer.js");
25
21
  class TurboFactory extends factory_js_1.TurboBaseFactory {
26
- static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, tokenMap, token, }) {
27
- let signer;
28
- if (providedSigner) {
29
- signer = providedSigner;
30
- if (!token) {
31
- if (signer instanceof arbundles_1.EthereumSigner) {
32
- token = 'ethereum';
33
- }
34
- else if (signer instanceof arbundles_1.HexSolanaSigner) {
35
- token = 'solana';
36
- }
37
- }
38
- }
39
- else if (privateKey) {
40
- signer = new arbundles_1.ArweaveSigner(privateKey);
41
- }
42
- else {
43
- throw new Error('A privateKey or signer must be provided.');
44
- }
45
- // when in browser, we use TurboWebArweaveSigner
46
- const turboSigner = typeof window !== 'undefined'
47
- ? new signer_js_1.TurboWebArweaveSigner({
48
- signer,
49
- logger: this.logger,
50
- })
51
- : new signer_js_2.TurboNodeSigner({
52
- signer,
53
- logger: this.logger,
54
- });
55
- const paymentService = new index_js_1.TurboAuthenticatedPaymentService({
56
- ...paymentServiceConfig,
57
- signer: turboSigner,
58
- logger: this.logger,
59
- tokenMap,
60
- });
61
- const uploadService = new index_js_1.TurboAuthenticatedUploadService({
62
- ...uploadServiceConfig,
63
- signer: turboSigner,
64
- logger: this.logger,
65
- });
66
- return new index_js_1.TurboAuthenticatedClient({
67
- uploadService,
68
- paymentService,
69
- });
70
- }
71
22
  }
72
23
  exports.TurboFactory = TurboFactory;
package/lib/cjs/types.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tokenTypes = exports.allowedFiatTokens = void 0;
3
+ exports.isJWK = exports.tokenTypes = exports.allowedFiatTokens = void 0;
4
4
  // TODO: Remove this var and Allow all tokens when crypto fund implemented for each PE-5993, PE-5992
5
5
  exports.allowedFiatTokens = ['arweave', 'solana', 'ethereum'];
6
- exports.tokenTypes = ['arweave' /*'solana', 'ethereum'*/];
6
+ exports.tokenTypes = ['arweave', 'solana' /* 'ethereum'*/];
7
+ const isJWK = (wallet) => wallet.kty !== undefined;
8
+ exports.isJWK = isJWK;
@@ -18,4 +18,4 @@
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.version = void 0;
20
20
  // AUTOMATICALLY GENERATED FILE - DO NOT TOUCH
21
- exports.version = '1.7.1';
21
+ exports.version = '1.8.0';
@@ -20,42 +20,75 @@ exports.TurboFactory = void 0;
20
20
  const arbundles_1 = require("arbundles");
21
21
  const factory_js_1 = require("../common/factory.js");
22
22
  const index_js_1 = require("../common/index.js");
23
+ const types_js_1 = require("../types.js");
23
24
  const signer_js_1 = require("./signer.js");
24
25
  class TurboFactory extends factory_js_1.TurboBaseFactory {
25
- static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, tokenMap, token, }) {
26
+ static getSigner(providedSigner, providedPrivateKey, token) {
26
27
  let signer;
27
- if (providedSigner) {
28
+ if (providedSigner !== undefined) {
28
29
  signer = providedSigner;
29
- if (!token) {
30
- if (signer instanceof arbundles_1.EthereumSigner) {
31
- token = 'ethereum';
32
- }
33
- else if (signer instanceof arbundles_1.HexSolanaSigner) {
34
- token = 'solana';
30
+ }
31
+ else if (providedPrivateKey !== undefined) {
32
+ if (token === 'solana') {
33
+ signer = new arbundles_1.HexSolanaSigner(providedPrivateKey);
34
+ // TODO: else if (token === 'ethereum') {signer = new EthereumSigner(providedPrivateKey);}
35
+ }
36
+ else {
37
+ if (!(0, types_js_1.isJWK)(providedPrivateKey)) {
38
+ throw new Error('A JWK must be provided for ArweaveSigner.');
35
39
  }
40
+ signer = new arbundles_1.ArweaveSigner(providedPrivateKey);
36
41
  }
37
42
  }
38
- else if (privateKey) {
39
- signer = new arbundles_1.ArweaveSigner(privateKey);
40
- }
41
43
  else {
42
44
  throw new Error('A privateKey or signer must be provided.');
43
45
  }
44
- const turboSigner = new signer_js_1.TurboWebArweaveSigner({
46
+ return new signer_js_1.TurboWebArweaveSigner({
45
47
  signer,
46
48
  logger: this.logger,
47
49
  });
50
+ }
51
+ static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, gatewayUrl, tokenMap, tokenTools, }) {
52
+ if (!token) {
53
+ if (providedSigner) {
54
+ // Derive token from signer if not provided
55
+ if (providedSigner instanceof arbundles_1.EthereumSigner) {
56
+ token = 'ethereum';
57
+ }
58
+ else if (providedSigner instanceof arbundles_1.HexSolanaSigner) {
59
+ token = 'solana';
60
+ }
61
+ else {
62
+ token = 'arweave';
63
+ }
64
+ }
65
+ else {
66
+ token = 'arweave';
67
+ }
68
+ }
69
+ const turboSigner = this.getSigner(providedSigner, privateKey, token);
70
+ token ??= 'arweave'; // default to arweave if token is not provided
71
+ if (!tokenTools) {
72
+ if (tokenMap && token === 'arweave') {
73
+ tokenTools = tokenMap.arweave;
74
+ }
75
+ tokenTools = index_js_1.defaultTokenMap[token]?.({
76
+ gatewayUrl,
77
+ logger: this.logger,
78
+ });
79
+ }
48
80
  const paymentService = new index_js_1.TurboAuthenticatedPaymentService({
49
81
  ...paymentServiceConfig,
50
82
  signer: turboSigner,
51
83
  logger: this.logger,
52
- tokenMap,
53
84
  token,
85
+ tokenTools,
54
86
  });
55
87
  const uploadService = new index_js_1.TurboAuthenticatedUploadService({
56
88
  ...uploadServiceConfig,
57
89
  signer: turboSigner,
58
90
  logger: this.logger,
91
+ token,
59
92
  });
60
93
  return new index_js_1.TurboAuthenticatedClient({
61
94
  uploadService,