@ardrive/turbo-sdk 1.18.1-alpha.1 → 1.19.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.
@@ -1,8 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TurboBaseFactory = void 0;
4
+ /**
5
+ * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ const arbundles_1 = require("@dha-team/arbundles");
20
+ const types_js_1 = require("../types.js");
4
21
  const logger_js_1 = require("./logger.js");
5
22
  const payment_js_1 = require("./payment.js");
23
+ const index_js_1 = require("./token/index.js");
6
24
  const turbo_js_1 = require("./turbo.js");
7
25
  const upload_js_1 = require("./upload.js");
8
26
  class TurboBaseFactory {
@@ -16,6 +34,7 @@ class TurboBaseFactory {
16
34
  }
17
35
  static unauthenticated({ paymentServiceConfig = {}, uploadServiceConfig = {}, token, } = {}) {
18
36
  token = token === 'pol' ? 'matic' : token;
37
+ token ??= 'arweave'; // default to arweave if token is not provided
19
38
  const paymentService = new payment_js_1.TurboUnauthenticatedPaymentService({
20
39
  ...paymentServiceConfig,
21
40
  logger: this.logger,
@@ -31,6 +50,79 @@ class TurboBaseFactory {
31
50
  paymentService,
32
51
  });
33
52
  }
53
+ getAuthenticatedTurbo({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, gatewayUrl, tokenMap, tokenTools, logger, walletAdapter, }) {
54
+ token = token === 'pol' ? 'matic' : token;
55
+ if (!token) {
56
+ if (providedSigner) {
57
+ // Derive token from signer if not provided
58
+ if (providedSigner instanceof arbundles_1.EthereumSigner) {
59
+ token = 'ethereum';
60
+ }
61
+ else if (providedSigner instanceof arbundles_1.HexSolanaSigner) {
62
+ token = 'solana';
63
+ }
64
+ else {
65
+ token = 'arweave';
66
+ }
67
+ }
68
+ else {
69
+ token = 'arweave';
70
+ }
71
+ }
72
+ token ??= 'arweave'; // default to arweave if token is not provided
73
+ if (walletAdapter) {
74
+ providedSigner = this.signerFromAdapter(walletAdapter, token);
75
+ }
76
+ const turboSigner = this.getSigner({
77
+ providedSigner,
78
+ providedPrivateKey: privateKey,
79
+ token,
80
+ logger,
81
+ providedWalletAdapter: walletAdapter,
82
+ });
83
+ if (!tokenTools) {
84
+ if (tokenMap && token === 'arweave') {
85
+ tokenTools = tokenMap.arweave;
86
+ }
87
+ tokenTools = index_js_1.defaultTokenMap[token]?.({
88
+ gatewayUrl,
89
+ logger,
90
+ });
91
+ }
92
+ const paymentService = new payment_js_1.TurboAuthenticatedPaymentService({
93
+ ...paymentServiceConfig,
94
+ signer: turboSigner,
95
+ logger,
96
+ token,
97
+ tokenTools,
98
+ });
99
+ const uploadService = this.getAuthenticatedUploadService({
100
+ ...uploadServiceConfig,
101
+ signer: turboSigner,
102
+ logger,
103
+ token,
104
+ });
105
+ return new turbo_js_1.TurboAuthenticatedClient({
106
+ uploadService,
107
+ paymentService,
108
+ signer: turboSigner,
109
+ });
110
+ }
111
+ signerFromAdapter(walletAdapter, token) {
112
+ if (token === 'solana') {
113
+ if (!(0, types_js_1.isSolanaWalletAdapter)(walletAdapter)) {
114
+ throw new Error('Unsupported wallet adapter -- must implement publicKey and signMessage');
115
+ }
116
+ return new arbundles_1.HexInjectedSolanaSigner(walletAdapter);
117
+ }
118
+ if (token === 'ethereum') {
119
+ if (!(0, types_js_1.isEthereumWalletAdapter)(walletAdapter)) {
120
+ throw new Error('Unsupported wallet adapter -- must implement getSigner');
121
+ }
122
+ return new arbundles_1.InjectedEthereumSigner(walletAdapter);
123
+ }
124
+ throw new Error('Unsupported wallet adapter -- wallet adapter is currently only supported for Solana and Ethereum');
125
+ }
34
126
  }
35
127
  exports.TurboBaseFactory = TurboBaseFactory;
36
128
  TurboBaseFactory.logger = logger_js_1.TurboWinstonLogger.default;
@@ -31,16 +31,18 @@ const ethers_1 = require("ethers");
31
31
  const ethers_2 = require("ethers");
32
32
  const node_buffer_1 = require("node:buffer");
33
33
  const tweetnacl_1 = __importDefault(require("tweetnacl"));
34
+ const types_js_1 = require("../types.js");
34
35
  const base64_js_1 = require("../utils/base64.js");
35
36
  const logger_js_1 = require("./logger.js");
36
37
  /**
37
38
  * Abstract class for signing TurboDataItems.
38
39
  */
39
40
  class TurboDataItemAbstractSigner {
40
- constructor({ signer, logger = logger_js_1.TurboWinstonLogger.default, token, }) {
41
+ constructor({ signer, logger = logger_js_1.TurboWinstonLogger.default, token, walletAdapter, }) {
41
42
  this.logger = logger;
42
43
  this.signer = signer;
43
44
  this.token = token;
45
+ this.walletAdapter = walletAdapter;
44
46
  }
45
47
  ownerToNativeAddress(owner, token) {
46
48
  switch (token) {
@@ -79,6 +81,20 @@ class TurboDataItemAbstractSigner {
79
81
  }
80
82
  /** Let the signer handle sending tx for better compat with cross chain libraries/web wallets */
81
83
  async sendTransaction({ target, amount, gatewayUrl, }) {
84
+ if (this.walletAdapter) {
85
+ if (!(0, types_js_1.isEthereumWalletAdapter)(this.walletAdapter)) {
86
+ throw new Error('Unsupported wallet adapter -- must implement getSigner');
87
+ }
88
+ const signer = this.walletAdapter.getSigner();
89
+ if (signer.sendTransaction === undefined) {
90
+ throw new Error('Unsupported wallet adapter -- getSigner must return a signer with sendTransaction API for crypto funds transfer');
91
+ }
92
+ const { hash } = await signer.sendTransaction({
93
+ to: target,
94
+ value: (0, ethers_1.parseEther)(amount.toFixed(18)),
95
+ });
96
+ return hash;
97
+ }
82
98
  if (!(this.signer instanceof arbundles_1.EthereumSigner)) {
83
99
  throw new Error('Only EthereumSigner is supported for sendTransaction API currently!');
84
100
  }
@@ -95,7 +111,6 @@ class TurboDataItemAbstractSigner {
95
111
  await tx.execute();
96
112
  return tx.txHash;
97
113
  }
98
- // TODO: ETH Web wallet tx signing/sending
99
114
  const provider = new ethers_1.ethers.JsonRpcProvider(gatewayUrl);
100
115
  const ethWalletAndProvider = new ethers_1.Wallet(keyAsStringFromUint8Array, provider);
101
116
  const tx = await ethWalletAndProvider.sendTransaction({
@@ -32,8 +32,8 @@ exports.SOLToTokenAmount = SOLToTokenAmount;
32
32
  class SolanaToken {
33
33
  constructor({ logger = logger_js_1.TurboWinstonLogger.default, gatewayUrl = 'https://api.mainnet-beta.solana.com', pollingOptions = {
34
34
  maxAttempts: 10,
35
- pollingIntervalMs: 5_000,
36
- initialBackoffMs: 7_000,
35
+ pollingIntervalMs: 2_500,
36
+ initialBackoffMs: 500,
37
37
  }, } = {}) {
38
38
  this.logger = logger;
39
39
  this.gatewayUrl = gatewayUrl;
@@ -16,72 +16,39 @@ exports.TurboFactory = void 0;
16
16
  * See the License for the specific language governing permissions and
17
17
  * limitations under the License.
18
18
  */
19
- const arbundles_1 = require("@dha-team/arbundles");
20
19
  const factory_js_1 = require("../common/factory.js");
21
- const index_js_1 = require("../common/index.js");
22
- const payment_js_1 = require("../common/payment.js");
23
- const turbo_js_1 = require("../common/turbo.js");
24
20
  const common_js_1 = require("../utils/common.js");
25
21
  const signer_js_1 = require("./signer.js");
26
22
  const upload_js_1 = require("./upload.js");
27
23
  class TurboFactory extends factory_js_1.TurboBaseFactory {
28
- static getSigner(providedSigner, providedPrivateKey, token) {
24
+ getSigner({ logger, providedPrivateKey, providedSigner, providedWalletAdapter, token, }) {
29
25
  return new signer_js_1.TurboNodeSigner({
30
26
  signer: (0, common_js_1.createTurboSigner)({
31
27
  signer: providedSigner,
32
28
  privateKey: providedPrivateKey,
33
29
  token,
34
30
  }),
35
- logger: this.logger,
31
+ logger,
36
32
  token,
33
+ walletAdapter: providedWalletAdapter,
37
34
  });
38
35
  }
39
- static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, tokenMap, gatewayUrl, tokenTools, }) {
40
- token = token === 'pol' ? 'matic' : token;
41
- if (!token) {
42
- if (providedSigner) {
43
- // Derive token from signer if not provided
44
- if (providedSigner instanceof arbundles_1.EthereumSigner) {
45
- token = 'ethereum';
46
- }
47
- else if (providedSigner instanceof arbundles_1.HexSolanaSigner) {
48
- token = 'solana';
49
- }
50
- else {
51
- token = 'arweave';
52
- }
53
- }
54
- else {
55
- token = 'arweave';
56
- }
57
- }
58
- const turboSigner = this.getSigner(providedSigner, privateKey, token);
59
- if (!tokenTools) {
60
- if (tokenMap && token === 'arweave') {
61
- tokenTools = tokenMap.arweave;
62
- }
63
- tokenTools = index_js_1.defaultTokenMap[token]?.({
64
- gatewayUrl,
65
- logger: this.logger,
66
- });
67
- }
68
- const paymentService = new payment_js_1.TurboAuthenticatedPaymentService({
69
- ...paymentServiceConfig,
70
- signer: turboSigner,
71
- logger: this.logger,
36
+ getAuthenticatedUploadService(config) {
37
+ // Import the TurboAuthenticatedUploadService class from the node upload module
38
+ return new upload_js_1.TurboAuthenticatedUploadService(config);
39
+ }
40
+ static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, tokenMap, gatewayUrl, tokenTools, walletAdapter, }) {
41
+ return new TurboFactory().getAuthenticatedTurbo({
42
+ privateKey,
43
+ signer: providedSigner,
44
+ paymentServiceConfig,
45
+ uploadServiceConfig,
72
46
  token,
47
+ tokenMap,
48
+ gatewayUrl,
73
49
  tokenTools,
74
- });
75
- const uploadService = new upload_js_1.TurboAuthenticatedUploadService({
76
- ...uploadServiceConfig,
77
- signer: turboSigner,
50
+ walletAdapter,
78
51
  logger: this.logger,
79
- token,
80
- });
81
- return new turbo_js_1.TurboAuthenticatedClient({
82
- uploadService,
83
- paymentService,
84
- signer: turboSigner,
85
52
  });
86
53
  }
87
54
  }
package/lib/cjs/types.js CHANGED
@@ -4,6 +4,8 @@ exports.isJWK = exports.isWebUploadFolderParams = exports.isNodeUploadFolderPara
4
4
  exports.isCurrency = isCurrency;
5
5
  exports.isKyvePrivateKey = isKyvePrivateKey;
6
6
  exports.isEthPrivateKey = isEthPrivateKey;
7
+ exports.isSolanaWalletAdapter = isSolanaWalletAdapter;
8
+ exports.isEthereumWalletAdapter = isEthereumWalletAdapter;
7
9
  exports.fiatCurrencyTypes = [
8
10
  'usd',
9
11
  'eur',
@@ -44,3 +46,9 @@ function isEthPrivateKey(wallet) {
44
46
  }
45
47
  const isJWK = (wallet) => wallet.kty !== undefined;
46
48
  exports.isJWK = isJWK;
49
+ function isSolanaWalletAdapter(walletAdapter) {
50
+ return 'publicKey' in walletAdapter && 'signMessage' in walletAdapter;
51
+ }
52
+ function isEthereumWalletAdapter(walletAdapter) {
53
+ return 'getSigner' in walletAdapter;
54
+ }
@@ -17,4 +17,4 @@
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.version = void 0;
19
19
  // AUTOMATICALLY GENERATED FILE - DO NOT TOUCH
20
- exports.version = '1.18.1-alpha.1';
20
+ exports.version = '1.19.0';
@@ -16,71 +16,39 @@ exports.TurboFactory = void 0;
16
16
  * See the License for the specific language governing permissions and
17
17
  * limitations under the License.
18
18
  */
19
- const arbundles_1 = require("@dha-team/arbundles");
20
19
  const factory_js_1 = require("../common/factory.js");
21
- const index_js_1 = require("../common/index.js");
22
20
  const common_js_1 = require("../utils/common.js");
23
21
  const signer_js_1 = require("./signer.js");
24
22
  const upload_js_1 = require("./upload.js");
25
23
  class TurboFactory extends factory_js_1.TurboBaseFactory {
26
- static getSigner(providedSigner, providedPrivateKey, token) {
24
+ getSigner({ providedPrivateKey, logger, providedSigner, providedWalletAdapter, token, }) {
27
25
  return new signer_js_1.TurboWebArweaveSigner({
28
26
  signer: (0, common_js_1.createTurboSigner)({
29
27
  signer: providedSigner,
30
28
  privateKey: providedPrivateKey,
31
29
  token,
32
30
  }),
33
- logger: this.logger,
31
+ logger: logger,
34
32
  token,
33
+ walletAdapter: providedWalletAdapter,
35
34
  });
36
35
  }
37
- static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, gatewayUrl, tokenMap, tokenTools, }) {
38
- token = token === 'pol' ? 'matic' : token;
39
- if (!token) {
40
- if (providedSigner) {
41
- // Derive token from signer if not provided
42
- if (providedSigner instanceof arbundles_1.EthereumSigner) {
43
- token = 'ethereum';
44
- }
45
- else if (providedSigner instanceof arbundles_1.HexSolanaSigner) {
46
- token = 'solana';
47
- }
48
- else {
49
- token = 'arweave';
50
- }
51
- }
52
- else {
53
- token = 'arweave';
54
- }
55
- }
56
- const turboSigner = this.getSigner(providedSigner, privateKey, token);
57
- token ??= 'arweave'; // default to arweave if token is not provided
58
- if (!tokenTools) {
59
- if (tokenMap && token === 'arweave') {
60
- tokenTools = tokenMap.arweave;
61
- }
62
- tokenTools = index_js_1.defaultTokenMap[token]?.({
63
- gatewayUrl,
64
- logger: this.logger,
65
- });
66
- }
67
- const paymentService = new index_js_1.TurboAuthenticatedPaymentService({
68
- ...paymentServiceConfig,
69
- signer: turboSigner,
70
- logger: this.logger,
36
+ getAuthenticatedUploadService(config) {
37
+ // Import the TurboAuthenticatedUploadService class from the web upload module
38
+ return new upload_js_1.TurboAuthenticatedUploadService(config);
39
+ }
40
+ static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, gatewayUrl, tokenMap, tokenTools, walletAdapter, }) {
41
+ return new TurboFactory().getAuthenticatedTurbo({
42
+ privateKey,
43
+ signer: providedSigner,
44
+ paymentServiceConfig,
45
+ uploadServiceConfig,
71
46
  token,
47
+ gatewayUrl,
48
+ tokenMap,
72
49
  tokenTools,
73
- });
74
- const uploadService = new upload_js_1.TurboAuthenticatedUploadService({
75
- ...uploadServiceConfig,
76
- signer: turboSigner,
77
50
  logger: this.logger,
78
- token,
79
- });
80
- return new index_js_1.TurboAuthenticatedClient({
81
- uploadService,
82
- paymentService,
83
- signer: turboSigner,
51
+ walletAdapter,
84
52
  });
85
53
  }
86
54
  }
@@ -21,6 +21,7 @@ Object.defineProperty(exports, "ArconnectSigner", { enumerable: true, get: funct
21
21
  Object.defineProperty(exports, "ArweaveSigner", { enumerable: true, get: function () { return arbundles_1.ArweaveSigner; } });
22
22
  Object.defineProperty(exports, "EthereumSigner", { enumerable: true, get: function () { return arbundles_1.EthereumSigner; } });
23
23
  Object.defineProperty(exports, "HexSolanaSigner", { enumerable: true, get: function () { return arbundles_1.HexSolanaSigner; } });
24
+ const node_buffer_1 = require("node:buffer");
24
25
  const signer_js_1 = require("../common/signer.js");
25
26
  const readableStream_js_1 = require("../utils/readableStream.js");
26
27
  /**
@@ -33,7 +34,8 @@ class TurboWebArweaveSigner extends signer_js_1.TurboDataItemAbstractSigner {
33
34
  async setPublicKey() {
34
35
  // for arconnect, we need to make sure we have the public key before create data
35
36
  if (this.signer.publicKey === undefined &&
36
- this.signer instanceof arbundles_1.ArconnectSigner) {
37
+ (this.signer instanceof arbundles_1.ArconnectSigner ||
38
+ this.signer instanceof arbundles_1.InjectedEthereumSigner)) {
37
39
  await this.signer.setPublicKey();
38
40
  }
39
41
  }
@@ -45,7 +47,7 @@ class TurboWebArweaveSigner extends signer_js_1.TurboDataItemAbstractSigner {
45
47
  await this.setPublicKey();
46
48
  const fileStream = fileStreamFactory();
47
49
  // TODO: converts the readable stream to a buffer bc incrementally signing ReadableStreams is not trivial
48
- const buffer = fileStream instanceof Buffer
50
+ const buffer = fileStream instanceof node_buffer_1.Buffer
49
51
  ? fileStream
50
52
  : await (0, readableStream_js_1.readableStreamToBuffer)({
51
53
  stream: fileStream,
@@ -1,6 +1,24 @@
1
+ /**
2
+ * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { EthereumSigner, HexInjectedSolanaSigner, HexSolanaSigner, InjectedEthereumSigner, } from '@dha-team/arbundles';
17
+ import { isEthereumWalletAdapter, isSolanaWalletAdapter, } from '../types.js';
1
18
  import { TurboWinstonLogger } from './logger.js';
2
- import { TurboUnauthenticatedPaymentService } from './payment.js';
3
- import { TurboUnauthenticatedClient } from './turbo.js';
19
+ import { TurboAuthenticatedPaymentService, TurboUnauthenticatedPaymentService, } from './payment.js';
20
+ import { defaultTokenMap } from './token/index.js';
21
+ import { TurboAuthenticatedClient, TurboUnauthenticatedClient, } from './turbo.js';
4
22
  import { TurboUnauthenticatedUploadService } from './upload.js';
5
23
  export class TurboBaseFactory {
6
24
  /* @deprecated - use TurboWinstonLogger directly */
@@ -13,6 +31,7 @@ export class TurboBaseFactory {
13
31
  }
14
32
  static unauthenticated({ paymentServiceConfig = {}, uploadServiceConfig = {}, token, } = {}) {
15
33
  token = token === 'pol' ? 'matic' : token;
34
+ token ??= 'arweave'; // default to arweave if token is not provided
16
35
  const paymentService = new TurboUnauthenticatedPaymentService({
17
36
  ...paymentServiceConfig,
18
37
  logger: this.logger,
@@ -28,5 +47,78 @@ export class TurboBaseFactory {
28
47
  paymentService,
29
48
  });
30
49
  }
50
+ getAuthenticatedTurbo({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, gatewayUrl, tokenMap, tokenTools, logger, walletAdapter, }) {
51
+ token = token === 'pol' ? 'matic' : token;
52
+ if (!token) {
53
+ if (providedSigner) {
54
+ // Derive token from signer if not provided
55
+ if (providedSigner instanceof EthereumSigner) {
56
+ token = 'ethereum';
57
+ }
58
+ else if (providedSigner instanceof HexSolanaSigner) {
59
+ token = 'solana';
60
+ }
61
+ else {
62
+ token = 'arweave';
63
+ }
64
+ }
65
+ else {
66
+ token = 'arweave';
67
+ }
68
+ }
69
+ token ??= 'arweave'; // default to arweave if token is not provided
70
+ if (walletAdapter) {
71
+ providedSigner = this.signerFromAdapter(walletAdapter, token);
72
+ }
73
+ const turboSigner = this.getSigner({
74
+ providedSigner,
75
+ providedPrivateKey: privateKey,
76
+ token,
77
+ logger,
78
+ providedWalletAdapter: walletAdapter,
79
+ });
80
+ if (!tokenTools) {
81
+ if (tokenMap && token === 'arweave') {
82
+ tokenTools = tokenMap.arweave;
83
+ }
84
+ tokenTools = defaultTokenMap[token]?.({
85
+ gatewayUrl,
86
+ logger,
87
+ });
88
+ }
89
+ const paymentService = new TurboAuthenticatedPaymentService({
90
+ ...paymentServiceConfig,
91
+ signer: turboSigner,
92
+ logger,
93
+ token,
94
+ tokenTools,
95
+ });
96
+ const uploadService = this.getAuthenticatedUploadService({
97
+ ...uploadServiceConfig,
98
+ signer: turboSigner,
99
+ logger,
100
+ token,
101
+ });
102
+ return new TurboAuthenticatedClient({
103
+ uploadService,
104
+ paymentService,
105
+ signer: turboSigner,
106
+ });
107
+ }
108
+ signerFromAdapter(walletAdapter, token) {
109
+ if (token === 'solana') {
110
+ if (!isSolanaWalletAdapter(walletAdapter)) {
111
+ throw new Error('Unsupported wallet adapter -- must implement publicKey and signMessage');
112
+ }
113
+ return new HexInjectedSolanaSigner(walletAdapter);
114
+ }
115
+ if (token === 'ethereum') {
116
+ if (!isEthereumWalletAdapter(walletAdapter)) {
117
+ throw new Error('Unsupported wallet adapter -- must implement getSigner');
118
+ }
119
+ return new InjectedEthereumSigner(walletAdapter);
120
+ }
121
+ throw new Error('Unsupported wallet adapter -- wallet adapter is currently only supported for Solana and Ethereum');
122
+ }
31
123
  }
32
124
  TurboBaseFactory.logger = TurboWinstonLogger.default;
@@ -25,16 +25,18 @@ import { Wallet as EthereumWallet, ethers, parseEther } from 'ethers';
25
25
  import { computeAddress } from 'ethers';
26
26
  import { Buffer } from 'node:buffer';
27
27
  import nacl from 'tweetnacl';
28
+ import { isEthereumWalletAdapter, } from '../types.js';
28
29
  import { fromB64Url, ownerToAddress as ownerToB64Address, toB64Url, } from '../utils/base64.js';
29
30
  import { TurboWinstonLogger } from './logger.js';
30
31
  /**
31
32
  * Abstract class for signing TurboDataItems.
32
33
  */
33
34
  export class TurboDataItemAbstractSigner {
34
- constructor({ signer, logger = TurboWinstonLogger.default, token, }) {
35
+ constructor({ signer, logger = TurboWinstonLogger.default, token, walletAdapter, }) {
35
36
  this.logger = logger;
36
37
  this.signer = signer;
37
38
  this.token = token;
39
+ this.walletAdapter = walletAdapter;
38
40
  }
39
41
  ownerToNativeAddress(owner, token) {
40
42
  switch (token) {
@@ -73,6 +75,20 @@ export class TurboDataItemAbstractSigner {
73
75
  }
74
76
  /** Let the signer handle sending tx for better compat with cross chain libraries/web wallets */
75
77
  async sendTransaction({ target, amount, gatewayUrl, }) {
78
+ if (this.walletAdapter) {
79
+ if (!isEthereumWalletAdapter(this.walletAdapter)) {
80
+ throw new Error('Unsupported wallet adapter -- must implement getSigner');
81
+ }
82
+ const signer = this.walletAdapter.getSigner();
83
+ if (signer.sendTransaction === undefined) {
84
+ throw new Error('Unsupported wallet adapter -- getSigner must return a signer with sendTransaction API for crypto funds transfer');
85
+ }
86
+ const { hash } = await signer.sendTransaction({
87
+ to: target,
88
+ value: parseEther(amount.toFixed(18)),
89
+ });
90
+ return hash;
91
+ }
76
92
  if (!(this.signer instanceof EthereumSigner)) {
77
93
  throw new Error('Only EthereumSigner is supported for sendTransaction API currently!');
78
94
  }
@@ -89,7 +105,6 @@ export class TurboDataItemAbstractSigner {
89
105
  await tx.execute();
90
106
  return tx.txHash;
91
107
  }
92
- // TODO: ETH Web wallet tx signing/sending
93
108
  const provider = new ethers.JsonRpcProvider(gatewayUrl);
94
109
  const ethWalletAndProvider = new EthereumWallet(keyAsStringFromUint8Array, provider);
95
110
  const tx = await ethWalletAndProvider.sendTransaction({
@@ -24,8 +24,8 @@ export const SOLToTokenAmount = (sol) => new BigNumber(sol).times(1e9).valueOf()
24
24
  export class SolanaToken {
25
25
  constructor({ logger = TurboWinstonLogger.default, gatewayUrl = 'https://api.mainnet-beta.solana.com', pollingOptions = {
26
26
  maxAttempts: 10,
27
- pollingIntervalMs: 5_000,
28
- initialBackoffMs: 7_000,
27
+ pollingIntervalMs: 2_500,
28
+ initialBackoffMs: 500,
29
29
  }, } = {}) {
30
30
  this.logger = logger;
31
31
  this.gatewayUrl = gatewayUrl;
@@ -13,72 +13,39 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import { EthereumSigner, HexSolanaSigner } from '@dha-team/arbundles';
17
16
  import { TurboBaseFactory } from '../common/factory.js';
18
- import { defaultTokenMap } from '../common/index.js';
19
- import { TurboAuthenticatedPaymentService } from '../common/payment.js';
20
- import { TurboAuthenticatedClient } from '../common/turbo.js';
21
17
  import { createTurboSigner } from '../utils/common.js';
22
18
  import { TurboNodeSigner } from './signer.js';
23
19
  import { TurboAuthenticatedUploadService } from './upload.js';
24
20
  export class TurboFactory extends TurboBaseFactory {
25
- static getSigner(providedSigner, providedPrivateKey, token) {
21
+ getSigner({ logger, providedPrivateKey, providedSigner, providedWalletAdapter, token, }) {
26
22
  return new TurboNodeSigner({
27
23
  signer: createTurboSigner({
28
24
  signer: providedSigner,
29
25
  privateKey: providedPrivateKey,
30
26
  token,
31
27
  }),
32
- logger: this.logger,
28
+ logger,
33
29
  token,
30
+ walletAdapter: providedWalletAdapter,
34
31
  });
35
32
  }
36
- static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, tokenMap, gatewayUrl, tokenTools, }) {
37
- token = token === 'pol' ? 'matic' : token;
38
- if (!token) {
39
- if (providedSigner) {
40
- // Derive token from signer if not provided
41
- if (providedSigner instanceof EthereumSigner) {
42
- token = 'ethereum';
43
- }
44
- else if (providedSigner instanceof HexSolanaSigner) {
45
- token = 'solana';
46
- }
47
- else {
48
- token = 'arweave';
49
- }
50
- }
51
- else {
52
- token = 'arweave';
53
- }
54
- }
55
- const turboSigner = this.getSigner(providedSigner, privateKey, token);
56
- if (!tokenTools) {
57
- if (tokenMap && token === 'arweave') {
58
- tokenTools = tokenMap.arweave;
59
- }
60
- tokenTools = defaultTokenMap[token]?.({
61
- gatewayUrl,
62
- logger: this.logger,
63
- });
64
- }
65
- const paymentService = new TurboAuthenticatedPaymentService({
66
- ...paymentServiceConfig,
67
- signer: turboSigner,
68
- logger: this.logger,
33
+ getAuthenticatedUploadService(config) {
34
+ // Import the TurboAuthenticatedUploadService class from the node upload module
35
+ return new TurboAuthenticatedUploadService(config);
36
+ }
37
+ static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, tokenMap, gatewayUrl, tokenTools, walletAdapter, }) {
38
+ return new TurboFactory().getAuthenticatedTurbo({
39
+ privateKey,
40
+ signer: providedSigner,
41
+ paymentServiceConfig,
42
+ uploadServiceConfig,
69
43
  token,
44
+ tokenMap,
45
+ gatewayUrl,
70
46
  tokenTools,
71
- });
72
- const uploadService = new TurboAuthenticatedUploadService({
73
- ...uploadServiceConfig,
74
- signer: turboSigner,
47
+ walletAdapter,
75
48
  logger: this.logger,
76
- token,
77
- });
78
- return new TurboAuthenticatedClient({
79
- uploadService,
80
- paymentService,
81
- signer: turboSigner,
82
49
  });
83
50
  }
84
51
  }