@ardrive/turbo-sdk 1.10.0-alpha.1 → 1.10.0-alpha.3
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.
- package/bundles/web.bundle.min.js +49671 -58308
- package/lib/cjs/common/factory.js +5 -116
- package/lib/cjs/common/logger.js +42 -9
- package/lib/cjs/common/payment.js +4 -3
- package/lib/cjs/common/signer.js +4 -3
- package/lib/cjs/common/token/arweave.js +3 -2
- package/lib/cjs/common/token/ethereum.js +1 -1
- package/lib/cjs/common/token/solana.js +3 -2
- package/lib/cjs/common/upload.js +26 -3
- package/lib/cjs/node/factory.js +63 -0
- package/lib/cjs/node/signer.js +2 -1
- package/lib/cjs/node/upload.js +3 -3
- package/lib/cjs/utils/axiosClient.js +1 -1
- package/lib/cjs/utils/base64.js +2 -1
- package/lib/cjs/utils/common.js +30 -3
- package/lib/cjs/utils/readableStream.js +18 -1
- package/lib/cjs/version.js +1 -1
- package/lib/cjs/web/factory.js +7 -27
- package/lib/cjs/web/upload.js +3 -3
- package/lib/esm/common/factory.js +5 -116
- package/lib/esm/common/logger.js +42 -9
- package/lib/esm/common/payment.js +3 -2
- package/lib/esm/common/signer.js +1 -0
- package/lib/esm/common/token/arweave.js +2 -1
- package/lib/esm/common/token/ethereum.js +1 -1
- package/lib/esm/common/token/solana.js +2 -1
- package/lib/esm/common/upload.js +25 -2
- package/lib/esm/node/factory.js +63 -0
- package/lib/esm/node/signer.js +1 -0
- package/lib/esm/node/upload.js +1 -1
- package/lib/esm/utils/axiosClient.js +1 -1
- package/lib/esm/utils/base64.js +1 -0
- package/lib/esm/utils/common.js +26 -0
- package/lib/esm/utils/readableStream.js +17 -0
- package/lib/esm/version.js +1 -1
- package/lib/esm/web/factory.js +9 -29
- package/lib/esm/web/upload.js +1 -1
- package/lib/types/common/factory.d.ts +19 -6
- package/lib/types/common/factory.d.ts.map +1 -1
- package/lib/types/common/logger.d.ts +3 -1
- package/lib/types/common/logger.d.ts.map +1 -1
- package/lib/types/common/payment.d.ts.map +1 -1
- package/lib/types/common/signer.d.ts +1 -0
- package/lib/types/common/signer.d.ts.map +1 -1
- package/lib/types/common/token/arweave.d.ts.map +1 -1
- package/lib/types/common/token/ethereum.d.ts.map +1 -1
- package/lib/types/common/token/solana.d.ts.map +1 -1
- package/lib/types/common/upload.d.ts +1 -0
- package/lib/types/common/upload.d.ts.map +1 -1
- package/lib/types/node/factory.d.ts +5 -16
- package/lib/types/node/factory.d.ts.map +1 -1
- package/lib/types/node/signer.d.ts.map +1 -1
- package/lib/types/node/upload.d.ts +1 -1
- package/lib/types/node/upload.d.ts.map +1 -1
- package/lib/types/utils/base64.d.ts +1 -0
- package/lib/types/utils/base64.d.ts.map +1 -1
- package/lib/types/utils/common.d.ts +6 -16
- package/lib/types/utils/common.d.ts.map +1 -1
- package/lib/types/utils/readableStream.d.ts +1 -0
- package/lib/types/utils/readableStream.d.ts.map +1 -1
- package/lib/types/version.d.ts +1 -1
- package/lib/types/version.d.ts.map +1 -1
- package/lib/types/web/factory.d.ts.map +1 -1
- package/lib/types/web/upload.d.ts +1 -1
- package/lib/types/web/upload.d.ts.map +1 -1
- package/package.json +2 -2
package/lib/cjs/web/factory.js
CHANGED
@@ -20,37 +20,17 @@ 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
|
23
|
+
const common_js_1 = require("../utils/common.js");
|
24
24
|
const signer_js_1 = require("./signer.js");
|
25
25
|
const upload_js_1 = require("./upload.js");
|
26
26
|
class TurboFactory extends factory_js_1.TurboBaseFactory {
|
27
27
|
static getSigner(providedSigner, providedPrivateKey, token) {
|
28
|
-
let signer;
|
29
|
-
if (providedSigner !== undefined) {
|
30
|
-
signer = providedSigner;
|
31
|
-
}
|
32
|
-
else if (providedPrivateKey !== undefined) {
|
33
|
-
if (token === 'solana') {
|
34
|
-
signer = new arbundles_1.HexSolanaSigner(providedPrivateKey);
|
35
|
-
}
|
36
|
-
else if (token === 'ethereum') {
|
37
|
-
if (!(0, types_js_1.isEthPrivateKey)(providedPrivateKey)) {
|
38
|
-
throw new Error('An Ethereum private key must be provided for EthereumSigner.');
|
39
|
-
}
|
40
|
-
signer = new arbundles_1.EthereumSigner(providedPrivateKey);
|
41
|
-
}
|
42
|
-
else {
|
43
|
-
if (!(0, types_js_1.isJWK)(providedPrivateKey)) {
|
44
|
-
throw new Error('A JWK must be provided for ArweaveSigner.');
|
45
|
-
}
|
46
|
-
signer = new arbundles_1.ArweaveSigner(providedPrivateKey);
|
47
|
-
}
|
48
|
-
}
|
49
|
-
else {
|
50
|
-
throw new Error('A privateKey or signer must be provided.');
|
51
|
-
}
|
52
28
|
return new signer_js_1.TurboWebArweaveSigner({
|
53
|
-
signer,
|
29
|
+
signer: (0, common_js_1.createTurboSigner)({
|
30
|
+
signer: providedSigner,
|
31
|
+
privateKey: providedPrivateKey,
|
32
|
+
token,
|
33
|
+
}),
|
54
34
|
logger: this.logger,
|
55
35
|
});
|
56
36
|
}
|
@@ -90,7 +70,7 @@ class TurboFactory extends factory_js_1.TurboBaseFactory {
|
|
90
70
|
token,
|
91
71
|
tokenTools,
|
92
72
|
});
|
93
|
-
const uploadService = new upload_js_1.
|
73
|
+
const uploadService = new upload_js_1.TurboAuthenticatedUploadService({
|
94
74
|
...uploadServiceConfig,
|
95
75
|
signer: turboSigner,
|
96
76
|
logger: this.logger,
|
package/lib/cjs/web/upload.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.TurboAuthenticatedUploadService = void 0;
|
4
4
|
/**
|
5
5
|
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
|
6
6
|
*
|
@@ -19,7 +19,7 @@ exports.TurboAuthenticatedWebUploadService = void 0;
|
|
19
19
|
*/
|
20
20
|
const upload_js_1 = require("../common/upload.js");
|
21
21
|
const types_js_1 = require("../types.js");
|
22
|
-
class
|
22
|
+
class TurboAuthenticatedUploadService extends upload_js_1.TurboAuthenticatedBaseUploadService {
|
23
23
|
constructor({ url = upload_js_1.defaultUploadServiceURL, retryConfig, signer, logger, token, }) {
|
24
24
|
super({ url, retryConfig, logger, token, signer });
|
25
25
|
}
|
@@ -54,4 +54,4 @@ class TurboAuthenticatedWebUploadService extends upload_js_1.TurboAuthenticatedB
|
|
54
54
|
return stream;
|
55
55
|
}
|
56
56
|
}
|
57
|
-
exports.
|
57
|
+
exports.TurboAuthenticatedUploadService = TurboAuthenticatedUploadService;
|
@@ -1,35 +1,13 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
|
3
|
-
*
|
4
|
-
* This program is free software: you can redistribute it and/or modify
|
5
|
-
* it under the terms of the GNU Affero General Public License as published by
|
6
|
-
* the Free Software Foundation, either version 3 of the License, or
|
7
|
-
* (at your option) any later version.
|
8
|
-
*
|
9
|
-
* This program is distributed in the hope that it will be useful,
|
10
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
-
* GNU Affero General Public License for more details.
|
13
|
-
*
|
14
|
-
* You should have received a copy of the GNU Affero General Public License
|
15
|
-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
-
*/
|
17
|
-
import { ArweaveSigner, EthereumSigner, HexSolanaSigner } from 'arbundles';
|
18
|
-
import { TurboNodeSigner } from '../node/signer.js';
|
19
|
-
import { TurboAuthenticatedNodeUploadService } from '../node/upload.js';
|
20
|
-
import { isEthPrivateKey, isJWK, } from '../types.js';
|
21
|
-
import { isWeb } from '../utils/common.js';
|
22
|
-
import { TurboWebArweaveSigner } from '../web/signer.js';
|
23
|
-
import { TurboAuthenticatedWebUploadService } from '../web/upload.js';
|
24
1
|
import { TurboWinstonLogger } from './logger.js';
|
25
|
-
import {
|
26
|
-
import {
|
27
|
-
import { TurboAuthenticatedClient, TurboUnauthenticatedClient, } from './turbo.js';
|
2
|
+
import { TurboUnauthenticatedPaymentService } from './payment.js';
|
3
|
+
import { TurboUnauthenticatedClient } from './turbo.js';
|
28
4
|
import { TurboUnauthenticatedUploadService } from './upload.js';
|
29
5
|
export class TurboBaseFactory {
|
6
|
+
/* @deprecated - use TurboWinstonLogger directly */
|
30
7
|
static setLogLevel(level) {
|
31
8
|
this.logger.setLogLevel(level);
|
32
9
|
}
|
10
|
+
/* @deprecated - use TurboWinstonLogger directly */
|
33
11
|
static setLogFormat(format) {
|
34
12
|
this.logger.setLogFormat(format);
|
35
13
|
}
|
@@ -47,94 +25,5 @@ export class TurboBaseFactory {
|
|
47
25
|
paymentService,
|
48
26
|
});
|
49
27
|
}
|
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 HexSolanaSigner(providedPrivateKey);
|
58
|
-
}
|
59
|
-
else if (token === 'ethereum') {
|
60
|
-
if (!isEthPrivateKey(providedPrivateKey)) {
|
61
|
-
throw new Error('An Ethereum private key must be provided for EthereumSigner.');
|
62
|
-
}
|
63
|
-
signer = new EthereumSigner(providedPrivateKey);
|
64
|
-
}
|
65
|
-
else {
|
66
|
-
if (!isJWK(providedPrivateKey)) {
|
67
|
-
throw new Error('A JWK must be provided for ArweaveSigner.');
|
68
|
-
}
|
69
|
-
signer = new ArweaveSigner(providedPrivateKey);
|
70
|
-
}
|
71
|
-
}
|
72
|
-
else {
|
73
|
-
throw new Error('A privateKey or signer must be provided.');
|
74
|
-
}
|
75
|
-
if (isWeb()) {
|
76
|
-
return new TurboWebArweaveSigner({
|
77
|
-
signer,
|
78
|
-
logger: this.logger,
|
79
|
-
});
|
80
|
-
}
|
81
|
-
return new TurboNodeSigner({
|
82
|
-
signer,
|
83
|
-
logger: this.logger,
|
84
|
-
});
|
85
|
-
}
|
86
|
-
static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, tokenMap, gatewayUrl, tokenTools, }) {
|
87
|
-
if (!token) {
|
88
|
-
if (providedSigner) {
|
89
|
-
// Derive token from signer if not provided
|
90
|
-
if (providedSigner instanceof EthereumSigner) {
|
91
|
-
token = 'ethereum';
|
92
|
-
}
|
93
|
-
else if (providedSigner instanceof HexSolanaSigner) {
|
94
|
-
token = 'solana';
|
95
|
-
}
|
96
|
-
else {
|
97
|
-
token = 'arweave';
|
98
|
-
}
|
99
|
-
}
|
100
|
-
else {
|
101
|
-
token = 'arweave';
|
102
|
-
}
|
103
|
-
}
|
104
|
-
const turboSigner = this.getSigner(providedSigner, privateKey, token);
|
105
|
-
if (!tokenTools) {
|
106
|
-
if (tokenMap && token === 'arweave') {
|
107
|
-
tokenTools = tokenMap.arweave;
|
108
|
-
}
|
109
|
-
tokenTools = defaultTokenMap[token]?.({
|
110
|
-
gatewayUrl,
|
111
|
-
logger: this.logger,
|
112
|
-
});
|
113
|
-
}
|
114
|
-
const paymentService = new TurboAuthenticatedPaymentService({
|
115
|
-
...paymentServiceConfig,
|
116
|
-
signer: turboSigner,
|
117
|
-
logger: this.logger,
|
118
|
-
token,
|
119
|
-
tokenTools,
|
120
|
-
});
|
121
|
-
const uploadService = isWeb()
|
122
|
-
? new TurboAuthenticatedWebUploadService({
|
123
|
-
...uploadServiceConfig,
|
124
|
-
signer: turboSigner,
|
125
|
-
logger: this.logger,
|
126
|
-
token,
|
127
|
-
})
|
128
|
-
: new TurboAuthenticatedNodeUploadService({
|
129
|
-
...uploadServiceConfig,
|
130
|
-
signer: turboSigner,
|
131
|
-
logger: this.logger,
|
132
|
-
token,
|
133
|
-
});
|
134
|
-
return new TurboAuthenticatedClient({
|
135
|
-
uploadService,
|
136
|
-
paymentService,
|
137
|
-
});
|
138
|
-
}
|
139
28
|
}
|
140
|
-
TurboBaseFactory.logger =
|
29
|
+
TurboBaseFactory.logger = TurboWinstonLogger.default;
|
package/lib/esm/common/logger.js
CHANGED
@@ -18,33 +18,66 @@ import { createLogger, format, transports } from 'winston';
|
|
18
18
|
import { version } from '../version.js';
|
19
19
|
export class TurboWinstonLogger {
|
20
20
|
constructor({ level = 'info', logFormat = 'simple', } = {}) {
|
21
|
-
this.
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
}
|
21
|
+
this.silent = false;
|
22
|
+
if (level === 'none') {
|
23
|
+
this.silent = true;
|
24
|
+
}
|
25
|
+
if (typeof window !== 'undefined') {
|
26
|
+
this.logger = console;
|
27
|
+
}
|
28
|
+
else {
|
29
|
+
this.logger = createLogger({
|
30
|
+
level,
|
31
|
+
silent: this.silent,
|
32
|
+
defaultMeta: {
|
33
|
+
name: 'turbo-sdk',
|
34
|
+
version,
|
35
|
+
},
|
36
|
+
format: format.combine(format.timestamp(), format.json()),
|
37
|
+
transports: [
|
38
|
+
new transports.Console({
|
39
|
+
format: getLogFormat(logFormat),
|
40
|
+
}),
|
41
|
+
],
|
42
|
+
});
|
43
|
+
}
|
28
44
|
}
|
29
45
|
info(message, ...args) {
|
46
|
+
if (this.silent)
|
47
|
+
return;
|
30
48
|
this.logger.info(message, ...args);
|
31
49
|
}
|
32
50
|
warn(message, ...args) {
|
51
|
+
if (this.silent)
|
52
|
+
return;
|
33
53
|
this.logger.warn(message, ...args);
|
34
54
|
}
|
35
55
|
error(message, ...args) {
|
56
|
+
if (this.silent)
|
57
|
+
return;
|
36
58
|
this.logger.error(message, ...args);
|
37
59
|
}
|
38
60
|
debug(message, ...args) {
|
61
|
+
if (this.silent)
|
62
|
+
return;
|
39
63
|
this.logger.debug(message, ...args);
|
40
64
|
}
|
41
65
|
setLogLevel(level) {
|
42
|
-
this.
|
66
|
+
this.silent = level === 'none';
|
67
|
+
if ('silent' in this.logger) {
|
68
|
+
this.logger.silent = level === 'none';
|
69
|
+
}
|
70
|
+
if ('level' in this.logger) {
|
71
|
+
this.logger.level = level;
|
72
|
+
}
|
43
73
|
}
|
44
74
|
setLogFormat(logFormat) {
|
45
|
-
this.logger
|
75
|
+
if ('format' in this.logger) {
|
76
|
+
this.logger.format = getLogFormat(logFormat);
|
77
|
+
}
|
46
78
|
}
|
47
79
|
}
|
80
|
+
TurboWinstonLogger.default = new TurboWinstonLogger();
|
48
81
|
function getLogFormat(logFormat) {
|
49
82
|
return format.combine(format((info) => {
|
50
83
|
if (info.stack && info.level !== 'error') {
|
@@ -15,12 +15,13 @@
|
|
15
15
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
16
|
*/
|
17
17
|
import { BigNumber } from 'bignumber.js';
|
18
|
+
import { Buffer } from 'node:buffer';
|
18
19
|
import { TurboHTTPService } from './http.js';
|
19
20
|
import { TurboWinstonLogger } from './logger.js';
|
20
21
|
export const developmentPaymentServiceURL = 'https://payment.ardrive.dev';
|
21
22
|
export const defaultPaymentServiceURL = 'https://payment.ardrive.io';
|
22
23
|
export class TurboUnauthenticatedPaymentService {
|
23
|
-
constructor({ url = defaultPaymentServiceURL, retryConfig, logger =
|
24
|
+
constructor({ url = defaultPaymentServiceURL, retryConfig, logger = TurboWinstonLogger.default, token = 'arweave', }) {
|
24
25
|
this.logger = logger;
|
25
26
|
this.httpService = new TurboHTTPService({
|
26
27
|
url: `${url}/v1`,
|
@@ -129,7 +130,7 @@ export class TurboUnauthenticatedPaymentService {
|
|
129
130
|
}
|
130
131
|
// NOTE: to avoid redundancy, we use inheritance here - but generally prefer composition over inheritance
|
131
132
|
export class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymentService {
|
132
|
-
constructor({ url = defaultPaymentServiceURL, retryConfig, signer, logger =
|
133
|
+
constructor({ url = defaultPaymentServiceURL, retryConfig, signer, logger = TurboWinstonLogger.default, token = 'arweave', tokenTools, }) {
|
133
134
|
super({ url, retryConfig, logger, token });
|
134
135
|
this.signer = signer;
|
135
136
|
this.tokenTools = tokenTools;
|
package/lib/esm/common/signer.js
CHANGED
@@ -17,6 +17,7 @@
|
|
17
17
|
import { EthereumSigner, HexSolanaSigner } from 'arbundles';
|
18
18
|
import { randomBytes } from 'crypto';
|
19
19
|
import { Wallet as EthereumWallet, parseEther } from 'ethers';
|
20
|
+
import { Buffer } from 'node:buffer';
|
20
21
|
import nacl from 'tweetnacl';
|
21
22
|
import { toB64Url } from '../utils/base64.js';
|
22
23
|
/**
|
@@ -16,13 +16,14 @@
|
|
16
16
|
*/
|
17
17
|
import Arweave from '@irys/arweave';
|
18
18
|
import { BigNumber } from 'bignumber.js';
|
19
|
+
import { Buffer } from 'node:buffer';
|
19
20
|
import { sha256B64Url, toB64Url } from '../../utils/base64.js';
|
20
21
|
import { sleep } from '../../utils/common.js';
|
21
22
|
import { TurboWinstonLogger } from '../logger.js';
|
22
23
|
export class ArweaveToken {
|
23
24
|
constructor({ gatewayUrl = 'https://arweave.net', arweave = Arweave.init({
|
24
25
|
url: gatewayUrl,
|
25
|
-
}), logger =
|
26
|
+
}), logger = TurboWinstonLogger.default, mintU = true, pollingOptions = {
|
26
27
|
maxAttempts: 10,
|
27
28
|
pollingIntervalMs: 3_000,
|
28
29
|
initialBackoffMs: 7_000,
|
@@ -20,7 +20,7 @@ import { TurboWinstonLogger } from '../logger.js';
|
|
20
20
|
export const weiToTokenAmount = (wei) => wei;
|
21
21
|
export const ETHToTokenAmount = (eth) => new BigNumber(eth).times(1e18).valueOf();
|
22
22
|
export class EthereumToken {
|
23
|
-
constructor({ logger =
|
23
|
+
constructor({ logger = TurboWinstonLogger.default, gatewayUrl = 'https://cloudflare-eth.com/', pollingOptions = {
|
24
24
|
maxAttempts: 10,
|
25
25
|
pollingIntervalMs: 4_000,
|
26
26
|
initialBackoffMs: 10_000,
|
@@ -17,12 +17,13 @@
|
|
17
17
|
import { Connection, PublicKey, SystemProgram, Transaction, } from '@solana/web3.js';
|
18
18
|
import { BigNumber } from 'bignumber.js';
|
19
19
|
import bs58 from 'bs58';
|
20
|
+
import { Buffer } from 'node:buffer';
|
20
21
|
import { sleep } from '../../utils/common.js';
|
21
22
|
import { TurboWinstonLogger } from '../logger.js';
|
22
23
|
export const lamportToTokenAmount = (winston) => winston;
|
23
24
|
export const SOLToTokenAmount = (sol) => new BigNumber(sol).times(1e9).valueOf();
|
24
25
|
export class SolanaToken {
|
25
|
-
constructor({ logger =
|
26
|
+
constructor({ logger = TurboWinstonLogger.default, gatewayUrl = 'https://api.mainnet-beta.solana.com', pollingOptions = {
|
26
27
|
maxAttempts: 10,
|
27
28
|
pollingIntervalMs: 5_000,
|
28
29
|
initialBackoffMs: 7_000,
|
package/lib/esm/common/upload.js
CHANGED
@@ -1,10 +1,27 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
|
3
|
+
*
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
5
|
+
* it under the terms of the GNU Affero General Public License as published by
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
7
|
+
* (at your option) any later version.
|
8
|
+
*
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
* GNU Affero General Public License for more details.
|
13
|
+
*
|
14
|
+
* You should have received a copy of the GNU Affero General Public License
|
15
|
+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
import { Buffer } from 'node:buffer';
|
1
18
|
import { pLimit } from 'plimit-lit';
|
2
19
|
import { TurboHTTPService } from './http.js';
|
3
20
|
import { TurboWinstonLogger } from './logger.js';
|
4
21
|
export const developmentUploadServiceURL = 'https://upload.ardrive.dev';
|
5
22
|
export const defaultUploadServiceURL = 'https://upload.ardrive.io';
|
6
23
|
export class TurboUnauthenticatedUploadService {
|
7
|
-
constructor({ url = defaultUploadServiceURL, retryConfig, logger =
|
24
|
+
constructor({ url = defaultUploadServiceURL, retryConfig, logger = TurboWinstonLogger.default, token = 'arweave', }) {
|
8
25
|
this.token = token;
|
9
26
|
this.logger = logger;
|
10
27
|
this.httpService = new TurboHTTPService({
|
@@ -87,7 +104,8 @@ export class TurboAuthenticatedBaseUploadService extends TurboUnauthenticatedUpl
|
|
87
104
|
return this.contentTypeFromFile(file);
|
88
105
|
}
|
89
106
|
async uploadFolder(params) {
|
90
|
-
|
107
|
+
this.logger.debug('Uploading folder...', { params });
|
108
|
+
const { dataItemOpts, signal, manifestOptions = {}, maxConcurrentUploads = 1, throwOnFailure = true, } = params;
|
91
109
|
const { disableManifest, indexFile, fallbackFile } = manifestOptions;
|
92
110
|
const paths = {};
|
93
111
|
const response = {
|
@@ -126,6 +144,11 @@ export class TurboAuthenticatedBaseUploadService extends TurboUnauthenticatedUpl
|
|
126
144
|
const files = await this.getFiles(params);
|
127
145
|
const limit = pLimit(maxConcurrentUploads);
|
128
146
|
await Promise.all(files.map((file) => limit(() => uploadFile(file))));
|
147
|
+
this.logger.debug('Finished uploading files', {
|
148
|
+
numFiles: files.length,
|
149
|
+
numErrors: errors.length,
|
150
|
+
results: response.fileResponses,
|
151
|
+
});
|
129
152
|
if (errors.length > 0) {
|
130
153
|
response.errors = errors;
|
131
154
|
}
|
package/lib/esm/node/factory.js
CHANGED
@@ -14,6 +14,69 @@
|
|
14
14
|
* You should have received a copy of the GNU Affero General Public License
|
15
15
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
16
|
*/
|
17
|
+
import { EthereumSigner, HexSolanaSigner } from 'arbundles';
|
17
18
|
import { TurboBaseFactory } from '../common/factory.js';
|
19
|
+
import { defaultTokenMap } from '../common/index.js';
|
20
|
+
import { TurboAuthenticatedPaymentService } from '../common/payment.js';
|
21
|
+
import { TurboAuthenticatedClient } from '../common/turbo.js';
|
22
|
+
import { createTurboSigner } from '../utils/common.js';
|
23
|
+
import { TurboNodeSigner } from './signer.js';
|
24
|
+
import { TurboAuthenticatedUploadService } from './upload.js';
|
18
25
|
export class TurboFactory extends TurboBaseFactory {
|
26
|
+
static getSigner(providedSigner, providedPrivateKey, token) {
|
27
|
+
return new TurboNodeSigner({
|
28
|
+
signer: createTurboSigner({
|
29
|
+
signer: providedSigner,
|
30
|
+
privateKey: providedPrivateKey,
|
31
|
+
token,
|
32
|
+
}),
|
33
|
+
logger: this.logger,
|
34
|
+
});
|
35
|
+
}
|
36
|
+
static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, tokenMap, gatewayUrl, tokenTools, }) {
|
37
|
+
if (!token) {
|
38
|
+
if (providedSigner) {
|
39
|
+
// Derive token from signer if not provided
|
40
|
+
if (providedSigner instanceof EthereumSigner) {
|
41
|
+
token = 'ethereum';
|
42
|
+
}
|
43
|
+
else if (providedSigner instanceof HexSolanaSigner) {
|
44
|
+
token = 'solana';
|
45
|
+
}
|
46
|
+
else {
|
47
|
+
token = 'arweave';
|
48
|
+
}
|
49
|
+
}
|
50
|
+
else {
|
51
|
+
token = 'arweave';
|
52
|
+
}
|
53
|
+
}
|
54
|
+
const turboSigner = this.getSigner(providedSigner, privateKey, token);
|
55
|
+
if (!tokenTools) {
|
56
|
+
if (tokenMap && token === 'arweave') {
|
57
|
+
tokenTools = tokenMap.arweave;
|
58
|
+
}
|
59
|
+
tokenTools = defaultTokenMap[token]?.({
|
60
|
+
gatewayUrl,
|
61
|
+
logger: this.logger,
|
62
|
+
});
|
63
|
+
}
|
64
|
+
const paymentService = new TurboAuthenticatedPaymentService({
|
65
|
+
...paymentServiceConfig,
|
66
|
+
signer: turboSigner,
|
67
|
+
logger: this.logger,
|
68
|
+
token,
|
69
|
+
tokenTools,
|
70
|
+
});
|
71
|
+
const uploadService = new TurboAuthenticatedUploadService({
|
72
|
+
...uploadServiceConfig,
|
73
|
+
signer: turboSigner,
|
74
|
+
logger: this.logger,
|
75
|
+
token,
|
76
|
+
});
|
77
|
+
return new TurboAuthenticatedClient({
|
78
|
+
uploadService,
|
79
|
+
paymentService,
|
80
|
+
});
|
81
|
+
}
|
19
82
|
}
|
package/lib/esm/node/signer.js
CHANGED
@@ -15,6 +15,7 @@
|
|
15
15
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
16
|
*/
|
17
17
|
import { ArconnectSigner, ArweaveSigner, EthereumSigner, HexSolanaSigner, serializeTags, streamSigner, } from 'arbundles';
|
18
|
+
import { Buffer } from 'node:buffer';
|
18
19
|
import { TurboDataItemAbstractSigner } from '../common/signer.js';
|
19
20
|
import { fromB64Url } from '../utils/base64.js';
|
20
21
|
/**
|
package/lib/esm/node/upload.js
CHANGED
@@ -20,7 +20,7 @@ import { Readable } from 'node:stream';
|
|
20
20
|
import { join } from 'path';
|
21
21
|
import { TurboAuthenticatedBaseUploadService, defaultUploadServiceURL, } from '../common/upload.js';
|
22
22
|
import { isNodeUploadFolderParams, } from '../types.js';
|
23
|
-
export class
|
23
|
+
export class TurboAuthenticatedUploadService extends TurboAuthenticatedBaseUploadService {
|
24
24
|
constructor({ url = defaultUploadServiceURL, retryConfig, signer, logger, token, }) {
|
25
25
|
super({ url, retryConfig, logger, token, signer });
|
26
26
|
}
|
@@ -22,7 +22,7 @@ export const defaultRequestHeaders = {
|
|
22
22
|
'x-turbo-source-version': version,
|
23
23
|
'x-turbo-source-identifier': 'turbo-sdk',
|
24
24
|
};
|
25
|
-
export const createAxiosInstance = ({ logger =
|
25
|
+
export const createAxiosInstance = ({ logger = TurboWinstonLogger.default, axiosConfig = {}, retryConfig = {
|
26
26
|
retryDelay: axiosRetry.exponentialDelay,
|
27
27
|
retries: 3,
|
28
28
|
retryCondition: (error) => {
|
package/lib/esm/utils/base64.js
CHANGED
@@ -16,6 +16,7 @@
|
|
16
16
|
*/
|
17
17
|
import { bufferTob64Url } from 'arweave/node/lib/utils.js';
|
18
18
|
import { createHash } from 'crypto';
|
19
|
+
import { Buffer } from 'node:buffer';
|
19
20
|
export const base64URLRegex = /^[a-zA-Z0-9_-]{43}$/;
|
20
21
|
export function jwkToPublicArweaveAddress(jwk) {
|
21
22
|
return ownerToAddress(jwk.n);
|
package/lib/esm/utils/common.js
CHANGED
@@ -14,9 +14,35 @@
|
|
14
14
|
* You should have received a copy of the GNU Affero General Public License
|
15
15
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
16
|
*/
|
17
|
+
import { ArweaveSigner, EthereumSigner, HexSolanaSigner } from 'arbundles';
|
18
|
+
import { isEthPrivateKey, isJWK, } from '../types.js';
|
17
19
|
export function sleep(ms) {
|
18
20
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
19
21
|
}
|
20
22
|
export function isWeb() {
|
21
23
|
return typeof window !== 'undefined';
|
22
24
|
}
|
25
|
+
export function createTurboSigner({ signer: clientProvidedSigner, privateKey: clientProvidedPrivateKey, token = 'arweave', }) {
|
26
|
+
if (clientProvidedSigner !== undefined) {
|
27
|
+
return clientProvidedSigner;
|
28
|
+
}
|
29
|
+
if (clientProvidedPrivateKey === undefined) {
|
30
|
+
throw new Error('A privateKey or signer must be provided.');
|
31
|
+
}
|
32
|
+
if (token === 'solana') {
|
33
|
+
// TODO: Add a type check for SOL private keys shape for detailed error message
|
34
|
+
return new HexSolanaSigner(clientProvidedPrivateKey);
|
35
|
+
}
|
36
|
+
else if (token === 'ethereum') {
|
37
|
+
if (!isEthPrivateKey(clientProvidedPrivateKey)) {
|
38
|
+
throw new Error('An Ethereum private key must be provided for EthereumSigner.');
|
39
|
+
}
|
40
|
+
return new EthereumSigner(clientProvidedPrivateKey);
|
41
|
+
}
|
42
|
+
else {
|
43
|
+
if (!isJWK(clientProvidedPrivateKey)) {
|
44
|
+
throw new Error('A JWK must be provided for ArweaveSigner.');
|
45
|
+
}
|
46
|
+
return new ArweaveSigner(clientProvidedPrivateKey);
|
47
|
+
}
|
48
|
+
}
|
@@ -1,3 +1,20 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
|
3
|
+
*
|
4
|
+
* This program is free software: you can redistribute it and/or modify
|
5
|
+
* it under the terms of the GNU Affero General Public License as published by
|
6
|
+
* the Free Software Foundation, either version 3 of the License, or
|
7
|
+
* (at your option) any later version.
|
8
|
+
*
|
9
|
+
* This program is distributed in the hope that it will be useful,
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
* GNU Affero General Public License for more details.
|
13
|
+
*
|
14
|
+
* You should have received a copy of the GNU Affero General Public License
|
15
|
+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
*/
|
17
|
+
import { Buffer } from 'node:buffer';
|
1
18
|
export async function readableStreamToBuffer({ stream, size, }) {
|
2
19
|
const reader = stream.getReader();
|
3
20
|
const buffer = Buffer.alloc(size);
|
package/lib/esm/version.js
CHANGED
package/lib/esm/web/factory.js
CHANGED
@@ -14,40 +14,20 @@
|
|
14
14
|
* You should have received a copy of the GNU Affero General Public License
|
15
15
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
16
|
*/
|
17
|
-
import {
|
17
|
+
import { EthereumSigner, HexSolanaSigner } from 'arbundles';
|
18
18
|
import { TurboBaseFactory } from '../common/factory.js';
|
19
19
|
import { TurboAuthenticatedClient, TurboAuthenticatedPaymentService, defaultTokenMap, } from '../common/index.js';
|
20
|
-
import {
|
20
|
+
import { createTurboSigner } from '../utils/common.js';
|
21
21
|
import { TurboWebArweaveSigner } from './signer.js';
|
22
|
-
import {
|
22
|
+
import { TurboAuthenticatedUploadService } from './upload.js';
|
23
23
|
export class TurboFactory extends TurboBaseFactory {
|
24
24
|
static getSigner(providedSigner, providedPrivateKey, token) {
|
25
|
-
let signer;
|
26
|
-
if (providedSigner !== undefined) {
|
27
|
-
signer = providedSigner;
|
28
|
-
}
|
29
|
-
else if (providedPrivateKey !== undefined) {
|
30
|
-
if (token === 'solana') {
|
31
|
-
signer = new HexSolanaSigner(providedPrivateKey);
|
32
|
-
}
|
33
|
-
else if (token === 'ethereum') {
|
34
|
-
if (!isEthPrivateKey(providedPrivateKey)) {
|
35
|
-
throw new Error('An Ethereum private key must be provided for EthereumSigner.');
|
36
|
-
}
|
37
|
-
signer = new EthereumSigner(providedPrivateKey);
|
38
|
-
}
|
39
|
-
else {
|
40
|
-
if (!isJWK(providedPrivateKey)) {
|
41
|
-
throw new Error('A JWK must be provided for ArweaveSigner.');
|
42
|
-
}
|
43
|
-
signer = new ArweaveSigner(providedPrivateKey);
|
44
|
-
}
|
45
|
-
}
|
46
|
-
else {
|
47
|
-
throw new Error('A privateKey or signer must be provided.');
|
48
|
-
}
|
49
25
|
return new TurboWebArweaveSigner({
|
50
|
-
signer
|
26
|
+
signer: createTurboSigner({
|
27
|
+
signer: providedSigner,
|
28
|
+
privateKey: providedPrivateKey,
|
29
|
+
token,
|
30
|
+
}),
|
51
31
|
logger: this.logger,
|
52
32
|
});
|
53
33
|
}
|
@@ -87,7 +67,7 @@ export class TurboFactory extends TurboBaseFactory {
|
|
87
67
|
token,
|
88
68
|
tokenTools,
|
89
69
|
});
|
90
|
-
const uploadService = new
|
70
|
+
const uploadService = new TurboAuthenticatedUploadService({
|
91
71
|
...uploadServiceConfig,
|
92
72
|
signer: turboSigner,
|
93
73
|
logger: this.logger,
|
package/lib/esm/web/upload.js
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
*/
|
17
17
|
import { TurboAuthenticatedBaseUploadService, defaultUploadServiceURL, } from '../common/upload.js';
|
18
18
|
import { isWebUploadFolderParams, } from '../types.js';
|
19
|
-
export class
|
19
|
+
export class TurboAuthenticatedUploadService extends TurboAuthenticatedBaseUploadService {
|
20
20
|
constructor({ url = defaultUploadServiceURL, retryConfig, signer, logger, token, }) {
|
21
21
|
super({ url, retryConfig, logger, token, signer });
|
22
22
|
}
|