@ardrive/turbo-sdk 1.0.0-alpha.1
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/LICENSE.md +661 -0
- package/README.md +114 -0
- package/bundles/web.bundle.min.js +55429 -0
- package/lib/LICENSE.md +661 -0
- package/lib/README.md +114 -0
- package/lib/common/factory.d.ts +21 -0
- package/lib/common/factory.js +13 -0
- package/lib/common/http.d.ts +44 -0
- package/lib/common/http.js +45 -0
- package/lib/common/index.d.ts +19 -0
- package/lib/common/index.js +19 -0
- package/lib/common/payment.d.ts +41 -0
- package/lib/common/payment.js +81 -0
- package/lib/common/turbo.d.ts +73 -0
- package/lib/common/turbo.js +100 -0
- package/lib/common/upload.d.ts +29 -0
- package/lib/common/upload.js +54 -0
- package/lib/node/factory.d.ts +22 -0
- package/lib/node/factory.js +30 -0
- package/lib/node/index.d.ts +19 -0
- package/lib/node/index.js +19 -0
- package/lib/node/signer.d.ts +36 -0
- package/lib/node/signer.js +54 -0
- package/lib/package.json +106 -0
- package/lib/types/arweave.d.ts +29 -0
- package/lib/types/arweave.js +1 -0
- package/lib/types/index.d.ts +18 -0
- package/lib/types/index.js +18 -0
- package/lib/types/turbo.d.ts +149 -0
- package/lib/types/turbo.js +1 -0
- package/lib/utils/axiosClient.d.ts +23 -0
- package/lib/utils/axiosClient.js +30 -0
- package/lib/utils/base64.d.ts +9 -0
- package/lib/utils/base64.js +39 -0
- package/lib/utils/errors.d.ts +22 -0
- package/lib/utils/errors.js +28 -0
- package/lib/utils/readableStream.d.ts +22 -0
- package/lib/utils/readableStream.js +24 -0
- package/lib/web/factory.d.ts +22 -0
- package/lib/web/factory.js +30 -0
- package/lib/web/index.d.ts +19 -0
- package/lib/web/index.js +19 -0
- package/lib/web/signer.d.ts +37 -0
- package/lib/web/signer.js +61 -0
- package/package.json +106 -0
package/lib/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# @ardriveapp/turbo-sdk 🚀
|
2
|
+
|
3
|
+
Welcome to the `@ardrive/turbo-sdk`! This SDK provides functionalities for interacting with the Turbo Upload and Payment Services. It is available in both NodeJS and Web environments.
|
4
|
+
|
5
|
+
## Table of Contents
|
6
|
+
|
7
|
+
- [Installation](#installation)
|
8
|
+
- [Usage](#usage):
|
9
|
+
|
10
|
+
- [NodeJS Environments](#nodejs)
|
11
|
+
- [CommonJS](#commonjs)
|
12
|
+
- [ESM](#esm)
|
13
|
+
- [Web Environments](#web)
|
14
|
+
- [Bundlers (Webpack, Rollup, ESbuild, etc.)](#bundlers-webpack-rollup-esbuild-etc)
|
15
|
+
- [Browser](#browser)
|
16
|
+
- [Typescript](#typescript)
|
17
|
+
- [Examples](./examples)
|
18
|
+
|
19
|
+
- [Contributions](#contributions)
|
20
|
+
|
21
|
+
# Installation
|
22
|
+
|
23
|
+
```shell
|
24
|
+
npm install @ardrive/turbo-sdk
|
25
|
+
```
|
26
|
+
|
27
|
+
or
|
28
|
+
|
29
|
+
```shell
|
30
|
+
yarn add @ardrive/turbo-sdk
|
31
|
+
```
|
32
|
+
|
33
|
+
# Usage
|
34
|
+
|
35
|
+
The SDK is available in both CommonJS and ESM formats and is compatible with bundlers such as Webpack, Rollup, and ESbuild.
|
36
|
+
|
37
|
+
## Web
|
38
|
+
|
39
|
+
# Bundlers (Webpack, Rollup, ESbuild, etc.)
|
40
|
+
|
41
|
+
```javascript
|
42
|
+
import { TurboFactory } from '@ardrive/turbo-sdk/web';
|
43
|
+
|
44
|
+
const turbo = TurboFactory.unauthenticated({});
|
45
|
+
const rates = await turbo.getFiatRates();
|
46
|
+
```
|
47
|
+
|
48
|
+
### Browser
|
49
|
+
|
50
|
+
```html
|
51
|
+
<script src="https://cdn.jsdelivr.net/npm/@ardrive/turbo-sdk"></script>
|
52
|
+
<script>
|
53
|
+
const turbo = TurboFactory.unauthenticated({});
|
54
|
+
const rates = await turbo.getFiatRates();
|
55
|
+
</script>
|
56
|
+
```
|
57
|
+
|
58
|
+
## NodeJS
|
59
|
+
|
60
|
+
### CommonJS
|
61
|
+
|
62
|
+
```javascript
|
63
|
+
const { TurboFactory } = require('@ardrive/turbo-sdk/node');
|
64
|
+
|
65
|
+
const turbo = TurboFactory.unauthenticated({});
|
66
|
+
const rates = await turbo.getFiatRates();
|
67
|
+
```
|
68
|
+
|
69
|
+
### ESM
|
70
|
+
|
71
|
+
```javascript
|
72
|
+
import { TurboFactory } from '@ardrive/turbo-sdk/node';
|
73
|
+
|
74
|
+
const turbo = TurboFactory.unauthenticated({});
|
75
|
+
const rates = await turbo.getFiatRates();
|
76
|
+
```
|
77
|
+
|
78
|
+
## Typescript
|
79
|
+
|
80
|
+
The SDK provides TypeScript typings. When you import the SDK in a TypeScript project:
|
81
|
+
|
82
|
+
```typescript
|
83
|
+
import Ardrive from '@ardrive/turbo-sdk/web';
|
84
|
+
|
85
|
+
// or '@ardrive/turbo-sdk/node' for Node.js projects
|
86
|
+
```
|
87
|
+
|
88
|
+
The provided typings (`./lib/types/index.d.ts`) will be automatically recognized, offering type checking and autocompletion benefits.
|
89
|
+
|
90
|
+
# APIs (WIP)
|
91
|
+
|
92
|
+
## TurboFactory
|
93
|
+
|
94
|
+
- `public()`
|
95
|
+
- `private()`
|
96
|
+
|
97
|
+
## TurboUnauthenticatedClient
|
98
|
+
|
99
|
+
- `getFiatRates()`
|
100
|
+
- `getFiatToAR()`
|
101
|
+
- `getSupportedCountries()`
|
102
|
+
- `getSupportedCurrencies()`
|
103
|
+
- `getWincForFiat()`
|
104
|
+
- `getUploadCosts()`
|
105
|
+
- `uploadSignedDataItem()`
|
106
|
+
|
107
|
+
## TurboAuthenticatedClient
|
108
|
+
|
109
|
+
- `getBalance()`
|
110
|
+
- `uploadFile()`
|
111
|
+
|
112
|
+
# Contributions
|
113
|
+
|
114
|
+
If you encounter any issues or have feature requests, please file an issue on our GitHub repository. Contributions, pull requests, and feedback are welcome and encouraged.
|
@@ -0,0 +1,21 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (C) 2022-2023 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 { TurboPublicConfiguration } from '../types/turbo.js';
|
18
|
+
import { TurboUnauthenticatedClient } from './turbo.js';
|
19
|
+
export declare class TurboBaseFactory {
|
20
|
+
static unauthenticated({ paymentServiceConfig, uploadServiceConfig, }?: TurboPublicConfiguration): TurboUnauthenticatedClient;
|
21
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { TurboUnauthenticatedPaymentService } from './payment.js';
|
2
|
+
import { TurboUnauthenticatedClient } from './turbo.js';
|
3
|
+
import { TurboUnauthenticatedUploadService } from './upload.js';
|
4
|
+
export class TurboBaseFactory {
|
5
|
+
static unauthenticated({ paymentServiceConfig = {}, uploadServiceConfig = {}, } = {}) {
|
6
|
+
const paymentService = new TurboUnauthenticatedPaymentService(Object.assign({}, paymentServiceConfig));
|
7
|
+
const uploadService = new TurboUnauthenticatedUploadService(Object.assign({}, uploadServiceConfig));
|
8
|
+
return new TurboUnauthenticatedClient({
|
9
|
+
uploadService,
|
10
|
+
paymentService,
|
11
|
+
});
|
12
|
+
}
|
13
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
3
|
+
/// <reference types="node" resolution-mode="require"/>
|
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
|
+
import { AxiosInstance } from 'axios';
|
21
|
+
import { RetryConfig } from 'retry-axios';
|
22
|
+
import { Readable } from 'stream';
|
23
|
+
import { ReadableStream } from 'stream/web';
|
24
|
+
import { TurboHTTPServiceInterface, TurboSignedRequestHeaders } from '../types/turbo.js';
|
25
|
+
export declare class TurboHTTPService implements TurboHTTPServiceInterface {
|
26
|
+
protected axios: AxiosInstance;
|
27
|
+
constructor({ url, retryConfig, }: {
|
28
|
+
url: string;
|
29
|
+
retryConfig?: RetryConfig;
|
30
|
+
});
|
31
|
+
get<T>({ endpoint, signal, allowedStatuses, headers, }: {
|
32
|
+
endpoint: string;
|
33
|
+
signal?: AbortSignal;
|
34
|
+
allowedStatuses?: number[];
|
35
|
+
headers?: Partial<TurboSignedRequestHeaders> & Record<string, string>;
|
36
|
+
}): Promise<T>;
|
37
|
+
post<T>({ endpoint, signal, allowedStatuses, headers, data, }: {
|
38
|
+
endpoint: string;
|
39
|
+
signal?: AbortSignal;
|
40
|
+
allowedStatuses?: number[];
|
41
|
+
headers?: Partial<TurboSignedRequestHeaders> & Record<string, string>;
|
42
|
+
data: Readable | Buffer | ReadableStream;
|
43
|
+
}): Promise<T>;
|
44
|
+
}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { createAxiosInstance } from '../utils/axiosClient.js';
|
11
|
+
import { FailedRequestError } from '../utils/errors.js';
|
12
|
+
export class TurboHTTPService {
|
13
|
+
constructor({ url, retryConfig, }) {
|
14
|
+
this.axios = createAxiosInstance({
|
15
|
+
axiosConfig: {
|
16
|
+
baseURL: url,
|
17
|
+
},
|
18
|
+
retryConfig,
|
19
|
+
});
|
20
|
+
}
|
21
|
+
get({ endpoint, signal, allowedStatuses = [200, 202], headers, }) {
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
23
|
+
const { status, statusText, data } = yield this.axios.get(endpoint, {
|
24
|
+
headers,
|
25
|
+
signal,
|
26
|
+
});
|
27
|
+
if (!allowedStatuses.includes(status)) {
|
28
|
+
throw new FailedRequestError(status, statusText);
|
29
|
+
}
|
30
|
+
return data;
|
31
|
+
});
|
32
|
+
}
|
33
|
+
post({ endpoint, signal, allowedStatuses = [200, 202], headers, data, }) {
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
35
|
+
const { status, statusText, data: response, } = yield this.axios.post(endpoint, data, {
|
36
|
+
headers,
|
37
|
+
signal,
|
38
|
+
});
|
39
|
+
if (!allowedStatuses.includes(status)) {
|
40
|
+
throw new FailedRequestError(status, statusText);
|
41
|
+
}
|
42
|
+
return response;
|
43
|
+
});
|
44
|
+
}
|
45
|
+
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (C) 2022-2023 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
|
+
export * from './upload.js';
|
18
|
+
export * from './payment.js';
|
19
|
+
export * from './turbo.js';
|
@@ -0,0 +1,19 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (C) 2022-2023 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
|
+
export * from './upload.js';
|
18
|
+
export * from './payment.js';
|
19
|
+
export * from './turbo.js';
|
@@ -0,0 +1,41 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (C) 2022-2023 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 { Currency, TurboAuthenticatedPaymentServiceInterface, TurboAuthenticatedPaymentServiceInterfaceConfiguration, TurboBalanceResponse, TurboCountriesResponse, TurboCurrenciesResponse, TurboFiatToArResponse, TurboPriceResponse, TurboRatesResponse, TurboUnauthenticatedPaymentServiceInterface, TurboUnauthenticatedPaymentServiceInterfaceConfiguration, TurboWalletSigner } from '../types/turbo.js';
|
18
|
+
import { TurboHTTPService } from './http.js';
|
19
|
+
export declare class TurboUnauthenticatedPaymentService implements TurboUnauthenticatedPaymentServiceInterface {
|
20
|
+
protected readonly httpService: TurboHTTPService;
|
21
|
+
constructor({ url, retryConfig, }: TurboUnauthenticatedPaymentServiceInterfaceConfiguration);
|
22
|
+
getFiatRates(): Promise<TurboRatesResponse>;
|
23
|
+
getFiatToAR({ currency, }: {
|
24
|
+
currency: Currency;
|
25
|
+
}): Promise<TurboFiatToArResponse>;
|
26
|
+
getSupportedCountries(): Promise<TurboCountriesResponse>;
|
27
|
+
getSupportedCurrencies(): Promise<TurboCurrenciesResponse>;
|
28
|
+
getUploadCosts({ bytes, }: {
|
29
|
+
bytes: number[];
|
30
|
+
}): Promise<TurboPriceResponse[]>;
|
31
|
+
getWincForFiat({ amount, currency }: {
|
32
|
+
amount: any;
|
33
|
+
currency: any;
|
34
|
+
}): Promise<TurboPriceResponse>;
|
35
|
+
}
|
36
|
+
export declare class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymentService implements TurboAuthenticatedPaymentServiceInterface {
|
37
|
+
protected readonly httpService: TurboHTTPService;
|
38
|
+
protected readonly signer: TurboWalletSigner;
|
39
|
+
constructor({ url, retryConfig, signer, }: TurboAuthenticatedPaymentServiceInterfaceConfiguration);
|
40
|
+
getBalance(): Promise<TurboBalanceResponse>;
|
41
|
+
}
|
@@ -0,0 +1,81 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { TurboHTTPService } from './http.js';
|
11
|
+
export class TurboUnauthenticatedPaymentService {
|
12
|
+
constructor({ url = 'https://payment.ardrive.dev', retryConfig, }) {
|
13
|
+
this.httpService = new TurboHTTPService({
|
14
|
+
url: `${url}/v1`,
|
15
|
+
retryConfig,
|
16
|
+
});
|
17
|
+
}
|
18
|
+
getFiatRates() {
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
20
|
+
return this.httpService.get({
|
21
|
+
endpoint: '/rates',
|
22
|
+
});
|
23
|
+
});
|
24
|
+
}
|
25
|
+
getFiatToAR({ currency, }) {
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
27
|
+
return this.httpService.get({
|
28
|
+
endpoint: `/rates/${currency}`,
|
29
|
+
});
|
30
|
+
});
|
31
|
+
}
|
32
|
+
getSupportedCountries() {
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
34
|
+
return this.httpService.get({
|
35
|
+
endpoint: '/countries',
|
36
|
+
});
|
37
|
+
});
|
38
|
+
}
|
39
|
+
getSupportedCurrencies() {
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
41
|
+
return this.httpService.get({
|
42
|
+
endpoint: '/currencies',
|
43
|
+
});
|
44
|
+
});
|
45
|
+
}
|
46
|
+
getUploadCosts({ bytes, }) {
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
48
|
+
const fetchPricePromises = bytes.map((byteCount) => this.httpService.get({
|
49
|
+
endpoint: `/price/bytes/${byteCount}`,
|
50
|
+
}));
|
51
|
+
const wincCostsForBytes = yield Promise.all(fetchPricePromises);
|
52
|
+
return wincCostsForBytes;
|
53
|
+
});
|
54
|
+
}
|
55
|
+
getWincForFiat({ amount, currency }) {
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
57
|
+
return this.httpService.get({
|
58
|
+
endpoint: `/price/${currency}/${amount}`,
|
59
|
+
});
|
60
|
+
});
|
61
|
+
}
|
62
|
+
}
|
63
|
+
// NOTE: to avoid redundancy, we use inheritance here - but generally prefer composition over inheritance
|
64
|
+
export class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymentService {
|
65
|
+
constructor({ url = 'https://payment.ardrive.dev', retryConfig, signer, }) {
|
66
|
+
super({ url, retryConfig });
|
67
|
+
this.signer = signer;
|
68
|
+
}
|
69
|
+
getBalance() {
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
71
|
+
const headers = yield this.signer.generateSignedRequestHeaders();
|
72
|
+
const balance = yield this.httpService.get({
|
73
|
+
endpoint: '/balance',
|
74
|
+
headers,
|
75
|
+
allowedStatuses: [200, 404],
|
76
|
+
});
|
77
|
+
// 404's don't return a balance, so default to 0
|
78
|
+
return balance.winc ? balance : { winc: '0' };
|
79
|
+
});
|
80
|
+
}
|
81
|
+
}
|
@@ -0,0 +1,73 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (C) 2022-2023 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 { Currency, TurboAbortSignal, TurboAuthenticatedClientInterface, TurboAuthenticatedPaymentServiceInterface, TurboAuthenticatedUploadServiceInterface, TurboBalanceResponse, TurboCountriesResponse, TurboCurrenciesResponse, TurboFiatToArResponse, TurboFileFactory, TurboPriceResponse, TurboPrivateClientConfiguration, TurboPublicClientConfiguration, TurboRatesResponse, TurboSignedDataItemFactory, TurboUnauthenticatedClientInterface, TurboUnauthenticatedPaymentServiceInterface, TurboUnauthenticatedUploadServiceInterface, TurboUploadDataItemResponse } from '../types/index.js';
|
18
|
+
export declare class TurboUnauthenticatedClient implements TurboUnauthenticatedClientInterface {
|
19
|
+
protected readonly paymentService: TurboUnauthenticatedPaymentServiceInterface;
|
20
|
+
protected readonly uploadService: TurboUnauthenticatedUploadServiceInterface;
|
21
|
+
constructor({ uploadService, paymentService, }: TurboPublicClientConfiguration);
|
22
|
+
/**
|
23
|
+
* Returns the supported fiat currency conversion rate for 1AR based on current market prices.
|
24
|
+
*/
|
25
|
+
getFiatToAR({ currency, }: {
|
26
|
+
currency: Currency;
|
27
|
+
}): Promise<TurboFiatToArResponse>;
|
28
|
+
/**
|
29
|
+
* Returns the latest conversion rates to purchase 1GiB of data for all supported currencies, including all adjustments and fees.
|
30
|
+
*
|
31
|
+
* Note: this does not take into account varying adjustments and promotions for different sizes of data. If you want to calculate the total
|
32
|
+
* cost in 'winc' for a given number of bytes, use getUploadCosts.
|
33
|
+
*/
|
34
|
+
getFiatRates(): Promise<TurboRatesResponse>;
|
35
|
+
/**
|
36
|
+
* Returns a comprehensive list of supported countries that can purchase credits through the Turbo Payment Service.
|
37
|
+
*/
|
38
|
+
getSupportedCountries(): Promise<TurboCountriesResponse>;
|
39
|
+
/**
|
40
|
+
* Returns a list of all supported fiat currencies.
|
41
|
+
*/
|
42
|
+
getSupportedCurrencies(): Promise<TurboCurrenciesResponse>;
|
43
|
+
/**
|
44
|
+
* Determines the price in 'winc' to upload one data item of a specific size in bytes, including all Turbo cost adjustments and fees.
|
45
|
+
*/
|
46
|
+
getUploadCosts({ bytes, }: {
|
47
|
+
bytes: number[];
|
48
|
+
}): Promise<TurboPriceResponse[]>;
|
49
|
+
/**
|
50
|
+
* Determines the amount of 'winc' that would be returned for a given currency and amount, including all Turbo cost adjustments and fees.
|
51
|
+
*/
|
52
|
+
getWincForFiat({ amount, currency, }: {
|
53
|
+
amount: number;
|
54
|
+
currency: Currency;
|
55
|
+
}): Promise<Omit<TurboPriceResponse, 'adjustments'>>;
|
56
|
+
/**
|
57
|
+
* Uploads a signed data item to the Turbo Upload Service.
|
58
|
+
*/
|
59
|
+
uploadSignedDataItem({ dataItemStreamFactory, signal, }: TurboSignedDataItemFactory & TurboAbortSignal): Promise<TurboUploadDataItemResponse>;
|
60
|
+
}
|
61
|
+
export declare class TurboAuthenticatedClient extends TurboUnauthenticatedClient implements TurboAuthenticatedClientInterface {
|
62
|
+
protected readonly paymentService: TurboAuthenticatedPaymentServiceInterface;
|
63
|
+
protected readonly uploadService: TurboAuthenticatedUploadServiceInterface;
|
64
|
+
constructor({ paymentService, uploadService, }: TurboPrivateClientConfiguration);
|
65
|
+
/**
|
66
|
+
* Returns the current balance of the user's wallet in 'winc'.
|
67
|
+
*/
|
68
|
+
getBalance(): Promise<TurboBalanceResponse>;
|
69
|
+
/**
|
70
|
+
* Signs and uploads raw data to the Turbo Upload Service.
|
71
|
+
*/
|
72
|
+
uploadFile({ fileStreamFactory, signal, }: TurboFileFactory & TurboAbortSignal): Promise<TurboUploadDataItemResponse>;
|
73
|
+
}
|
@@ -0,0 +1,100 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { TurboUnauthenticatedPaymentService } from './payment.js';
|
11
|
+
import { TurboUnauthenticatedUploadService } from './upload.js';
|
12
|
+
export class TurboUnauthenticatedClient {
|
13
|
+
constructor({ uploadService = new TurboUnauthenticatedUploadService({}), paymentService = new TurboUnauthenticatedPaymentService({}), }) {
|
14
|
+
this.paymentService = paymentService;
|
15
|
+
this.uploadService = uploadService;
|
16
|
+
}
|
17
|
+
/**
|
18
|
+
* Returns the supported fiat currency conversion rate for 1AR based on current market prices.
|
19
|
+
*/
|
20
|
+
getFiatToAR({ currency, }) {
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
22
|
+
return this.paymentService.getFiatToAR({ currency });
|
23
|
+
});
|
24
|
+
}
|
25
|
+
/**
|
26
|
+
* Returns the latest conversion rates to purchase 1GiB of data for all supported currencies, including all adjustments and fees.
|
27
|
+
*
|
28
|
+
* Note: this does not take into account varying adjustments and promotions for different sizes of data. If you want to calculate the total
|
29
|
+
* cost in 'winc' for a given number of bytes, use getUploadCosts.
|
30
|
+
*/
|
31
|
+
getFiatRates() {
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
33
|
+
return this.paymentService.getFiatRates();
|
34
|
+
});
|
35
|
+
}
|
36
|
+
/**
|
37
|
+
* Returns a comprehensive list of supported countries that can purchase credits through the Turbo Payment Service.
|
38
|
+
*/
|
39
|
+
getSupportedCountries() {
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
41
|
+
return this.paymentService.getSupportedCountries();
|
42
|
+
});
|
43
|
+
}
|
44
|
+
/**
|
45
|
+
* Returns a list of all supported fiat currencies.
|
46
|
+
*/
|
47
|
+
getSupportedCurrencies() {
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
49
|
+
return this.paymentService.getSupportedCurrencies();
|
50
|
+
});
|
51
|
+
}
|
52
|
+
/**
|
53
|
+
* Determines the price in 'winc' to upload one data item of a specific size in bytes, including all Turbo cost adjustments and fees.
|
54
|
+
*/
|
55
|
+
getUploadCosts({ bytes, }) {
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
57
|
+
return this.paymentService.getUploadCosts({ bytes });
|
58
|
+
});
|
59
|
+
}
|
60
|
+
/**
|
61
|
+
* Determines the amount of 'winc' that would be returned for a given currency and amount, including all Turbo cost adjustments and fees.
|
62
|
+
*/
|
63
|
+
getWincForFiat({ amount, currency, }) {
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
65
|
+
return this.paymentService.getWincForFiat({ amount, currency });
|
66
|
+
});
|
67
|
+
}
|
68
|
+
/**
|
69
|
+
* Uploads a signed data item to the Turbo Upload Service.
|
70
|
+
*/
|
71
|
+
uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
73
|
+
return this.uploadService.uploadSignedDataItem({
|
74
|
+
dataItemStreamFactory,
|
75
|
+
signal,
|
76
|
+
});
|
77
|
+
});
|
78
|
+
}
|
79
|
+
}
|
80
|
+
export class TurboAuthenticatedClient extends TurboUnauthenticatedClient {
|
81
|
+
constructor({ paymentService, uploadService, }) {
|
82
|
+
super({ paymentService, uploadService });
|
83
|
+
}
|
84
|
+
/**
|
85
|
+
* Returns the current balance of the user's wallet in 'winc'.
|
86
|
+
*/
|
87
|
+
getBalance() {
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
89
|
+
return this.paymentService.getBalance();
|
90
|
+
});
|
91
|
+
}
|
92
|
+
/**
|
93
|
+
* Signs and uploads raw data to the Turbo Upload Service.
|
94
|
+
*/
|
95
|
+
uploadFile({ fileStreamFactory, signal, }) {
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
97
|
+
return this.uploadService.uploadFile({ fileStreamFactory, signal });
|
98
|
+
});
|
99
|
+
}
|
100
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (C) 2022-2023 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 { TurboAbortSignal, TurboAuthenticatedUploadServiceConfiguration, TurboAuthenticatedUploadServiceInterface, TurboFileFactory, TurboSignedDataItemFactory, TurboUnauthenticatedUploadServiceInterface, TurboUnauthenticatedUploadServiceInterfaceConfiguration, TurboUploadDataItemResponse, TurboWalletSigner } from '../types/turbo.js';
|
18
|
+
import { TurboHTTPService } from './http.js';
|
19
|
+
export declare class TurboUnauthenticatedUploadService implements TurboUnauthenticatedUploadServiceInterface {
|
20
|
+
protected httpService: TurboHTTPService;
|
21
|
+
constructor({ url, retryConfig, }: TurboUnauthenticatedUploadServiceInterfaceConfiguration);
|
22
|
+
uploadSignedDataItem({ dataItemStreamFactory, signal, }: TurboSignedDataItemFactory & TurboAbortSignal): Promise<TurboUploadDataItemResponse>;
|
23
|
+
}
|
24
|
+
export declare class TurboAuthenticatedUploadService extends TurboUnauthenticatedUploadService implements TurboAuthenticatedUploadServiceInterface {
|
25
|
+
protected httpService: TurboHTTPService;
|
26
|
+
protected signer: TurboWalletSigner;
|
27
|
+
constructor({ url, retryConfig, signer, }: TurboAuthenticatedUploadServiceConfiguration);
|
28
|
+
uploadFile({ fileStreamFactory, signal, }: TurboFileFactory & TurboAbortSignal): Promise<TurboUploadDataItemResponse>;
|
29
|
+
}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { TurboHTTPService } from './http.js';
|
11
|
+
export class TurboUnauthenticatedUploadService {
|
12
|
+
constructor({ url = 'https://upload.ardrive.dev', retryConfig, }) {
|
13
|
+
this.httpService = new TurboHTTPService({
|
14
|
+
url: `${url}/v1`,
|
15
|
+
retryConfig,
|
16
|
+
});
|
17
|
+
}
|
18
|
+
uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
20
|
+
// TODO: add p-limit constraint or replace with separate upload class
|
21
|
+
return this.httpService.post({
|
22
|
+
endpoint: `/tx`,
|
23
|
+
signal,
|
24
|
+
data: dataItemStreamFactory(),
|
25
|
+
headers: {
|
26
|
+
'content-type': 'application/octet-stream',
|
27
|
+
},
|
28
|
+
});
|
29
|
+
});
|
30
|
+
}
|
31
|
+
}
|
32
|
+
// NOTE: to avoid redundancy, we use inheritance here - but generally prefer composition over inheritance
|
33
|
+
export class TurboAuthenticatedUploadService extends TurboUnauthenticatedUploadService {
|
34
|
+
constructor({ url = 'https://upload.ardrive.dev', retryConfig, signer, }) {
|
35
|
+
super({ url, retryConfig });
|
36
|
+
this.signer = signer;
|
37
|
+
}
|
38
|
+
uploadFile({ fileStreamFactory, signal, }) {
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
40
|
+
const signedDataItem = yield this.signer.signDataItem({
|
41
|
+
fileStreamFactory,
|
42
|
+
});
|
43
|
+
// TODO: add p-limit constraint or replace with separate upload class
|
44
|
+
return this.httpService.post({
|
45
|
+
endpoint: `/tx`,
|
46
|
+
signal,
|
47
|
+
data: signedDataItem,
|
48
|
+
headers: {
|
49
|
+
'content-type': 'application/octet-stream',
|
50
|
+
},
|
51
|
+
});
|
52
|
+
});
|
53
|
+
}
|
54
|
+
}
|