@ardrive/turbo-sdk 1.0.0-alpha.20 → 1.0.0-alpha.22
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/README.md +134 -23
- package/bundles/web.bundle.min.js +37 -14
- package/lib/cjs/common/factory.js +6 -2
- package/lib/cjs/common/http.js +16 -29
- package/lib/cjs/common/payment.js +29 -52
- package/lib/cjs/common/turbo.js +24 -46
- package/lib/cjs/common/upload.js +27 -34
- package/lib/cjs/node/factory.js +8 -2
- package/lib/cjs/node/signer.js +42 -22
- package/lib/cjs/utils/axiosClient.js +4 -1
- package/lib/cjs/utils/readableStream.js +13 -22
- package/lib/cjs/web/factory.js +8 -2
- package/lib/cjs/web/signer.js +24 -33
- package/lib/esm/common/factory.js +6 -2
- package/lib/esm/common/http.js +16 -29
- package/lib/esm/common/payment.js +29 -52
- package/lib/esm/common/turbo.js +24 -46
- package/lib/esm/common/upload.js +27 -34
- package/lib/esm/node/factory.js +8 -2
- package/lib/esm/node/signer.js +42 -22
- package/lib/esm/utils/axiosClient.js +4 -1
- package/lib/esm/utils/readableStream.js +13 -22
- package/lib/esm/web/factory.js +8 -2
- package/lib/esm/web/signer.js +24 -33
- package/lib/types/common/payment.d.ts +0 -1
- package/lib/types/common/payment.d.ts.map +1 -1
- package/lib/types/common/turbo.d.ts +6 -6
- package/lib/types/common/turbo.d.ts.map +1 -1
- package/lib/types/common/upload.d.ts +2 -3
- package/lib/types/common/upload.d.ts.map +1 -1
- package/lib/types/node/signer.d.ts +8 -3
- package/lib/types/node/signer.d.ts.map +1 -1
- package/lib/types/types.d.ts +5 -2
- package/lib/types/types.d.ts.map +1 -1
- package/lib/types/utils/readableStream.d.ts +2 -1
- package/lib/types/utils/readableStream.d.ts.map +1 -1
- package/lib/types/web/signer.d.ts +7 -3
- package/lib/types/web/signer.d.ts.map +1 -1
- package/package.json +1 -1
package/lib/cjs/common/upload.js
CHANGED
@@ -1,13 +1,4 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
-
});
|
10
|
-
};
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
3
|
exports.TurboAuthenticatedUploadService = exports.TurboUnauthenticatedUploadService = void 0;
|
13
4
|
const http_js_1 = require("./http.js");
|
@@ -18,17 +9,17 @@ class TurboUnauthenticatedUploadService {
|
|
18
9
|
retryConfig,
|
19
10
|
});
|
20
11
|
}
|
21
|
-
uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
}
|
31
|
-
}
|
12
|
+
async uploadSignedDataItem({ dataItemStreamFactory, dataItemSizeFactory, signal, }) {
|
13
|
+
const fileSize = dataItemSizeFactory();
|
14
|
+
// TODO: add p-limit constraint or replace with separate upload class
|
15
|
+
return this.httpService.post({
|
16
|
+
endpoint: `/tx`,
|
17
|
+
signal,
|
18
|
+
data: dataItemStreamFactory(),
|
19
|
+
headers: {
|
20
|
+
'content-type': 'application/octet-stream',
|
21
|
+
'content-length': `${fileSize}`,
|
22
|
+
},
|
32
23
|
});
|
33
24
|
}
|
34
25
|
}
|
@@ -39,20 +30,22 @@ class TurboAuthenticatedUploadService extends TurboUnauthenticatedUploadService
|
|
39
30
|
super({ url, retryConfig });
|
40
31
|
this.signer = signer;
|
41
32
|
}
|
42
|
-
uploadFile({ fileStreamFactory, signal, }) {
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
33
|
+
async uploadFile({ fileStreamFactory, fileSizeFactory, signal, }) {
|
34
|
+
const { dataItemStreamFactory, dataItemSizeFactory } = await this.signer.signDataItem({
|
35
|
+
fileStreamFactory,
|
36
|
+
fileSizeFactory,
|
37
|
+
});
|
38
|
+
const signedDataItem = dataItemStreamFactory();
|
39
|
+
const fileSize = dataItemSizeFactory();
|
40
|
+
// TODO: add p-limit constraint or replace with separate upload class
|
41
|
+
return this.httpService.post({
|
42
|
+
endpoint: `/tx`,
|
43
|
+
signal,
|
44
|
+
data: signedDataItem,
|
45
|
+
headers: {
|
46
|
+
'content-type': 'application/octet-stream',
|
47
|
+
'content-length': `${fileSize}`,
|
48
|
+
},
|
56
49
|
});
|
57
50
|
}
|
58
51
|
}
|
package/lib/cjs/node/factory.js
CHANGED
@@ -23,8 +23,14 @@ const signer_js_1 = require("./signer.js");
|
|
23
23
|
class TurboFactory extends factory_js_1.TurboBaseFactory {
|
24
24
|
static authenticated({ privateKey, paymentServiceConfig = {}, uploadServiceConfig = {}, }) {
|
25
25
|
const signer = new signer_js_1.TurboNodeArweaveSigner({ privateKey });
|
26
|
-
const paymentService = new index_js_1.TurboAuthenticatedPaymentService(
|
27
|
-
|
26
|
+
const paymentService = new index_js_1.TurboAuthenticatedPaymentService({
|
27
|
+
...paymentServiceConfig,
|
28
|
+
signer,
|
29
|
+
});
|
30
|
+
const uploadService = new index_js_1.TurboAuthenticatedUploadService({
|
31
|
+
...uploadServiceConfig,
|
32
|
+
signer,
|
33
|
+
});
|
28
34
|
return new index_js_1.TurboAuthenticatedClient({
|
29
35
|
uploadService,
|
30
36
|
paymentService,
|
package/lib/cjs/node/signer.js
CHANGED
@@ -1,13 +1,4 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
-
});
|
10
|
-
};
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
4
|
};
|
@@ -39,23 +30,52 @@ class TurboNodeArweaveSigner {
|
|
39
30
|
this.privateKey = privateKey;
|
40
31
|
this.signer = new arbundles_1.ArweaveSigner(this.privateKey);
|
41
32
|
}
|
42
|
-
signDataItem({ fileStreamFactory, }) {
|
33
|
+
async signDataItem({ fileStreamFactory, fileSizeFactory, }) {
|
43
34
|
// TODO: replace with our own signer implementation
|
44
35
|
const [stream1, stream2] = [fileStreamFactory(), fileStreamFactory()];
|
45
|
-
|
36
|
+
const signedDataItem = await (0, arbundles_1.streamSigner)(stream1, stream2, this.signer);
|
37
|
+
// TODO: support target, anchor, and tags
|
38
|
+
const signedDataItemSize = this.calculateSignedDataHeadersSize({
|
39
|
+
dataSize: fileSizeFactory(),
|
40
|
+
});
|
41
|
+
return {
|
42
|
+
dataItemStreamFactory: () => signedDataItem,
|
43
|
+
dataItemSizeFactory: () => signedDataItemSize,
|
44
|
+
};
|
46
45
|
}
|
47
46
|
// NOTE: this might be better in a parent class or elsewhere - easy enough to leave in here now and does require specific environment version of crypto
|
48
|
-
generateSignedRequestHeaders() {
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
47
|
+
async generateSignedRequestHeaders() {
|
48
|
+
const nonce = (0, node_crypto_1.randomBytes)(16).toString('hex');
|
49
|
+
const buffer = Buffer.from(nonce);
|
50
|
+
const signature = await index_js_1.default.crypto.sign(this.privateKey, buffer);
|
51
|
+
return {
|
52
|
+
'x-public-key': this.privateKey.n,
|
53
|
+
'x-nonce': nonce,
|
54
|
+
'x-signature': (0, base64_js_1.toB64Url)(Buffer.from(signature)),
|
55
|
+
};
|
56
|
+
}
|
57
|
+
// TODO: make dynamic that accepts anchor and target and tags to return the size of the headers + data
|
58
|
+
// reference https://github.com/ArweaveTeam/arweave-standards/blob/master/ans/ANS-104.md#13-dataitem-format
|
59
|
+
calculateSignedDataHeadersSize({ dataSize }) {
|
60
|
+
const anchor = 1; // + whatever they provide (max of 33)
|
61
|
+
const target = 1; // + whatever they provide (max of 33)
|
62
|
+
const tags = 0;
|
63
|
+
const signatureLength = 512;
|
64
|
+
const ownerLength = 512;
|
65
|
+
const signatureTypeLength = 2;
|
66
|
+
const numOfTagsLength = 8; // https://github.com/Bundlr-Network/arbundles/blob/master/src/tags.ts#L191-L198
|
67
|
+
const numOfTagsBytesLength = 8;
|
68
|
+
return [
|
69
|
+
anchor,
|
70
|
+
target,
|
71
|
+
tags,
|
72
|
+
signatureLength,
|
73
|
+
ownerLength,
|
74
|
+
signatureTypeLength,
|
75
|
+
numOfTagsLength,
|
76
|
+
numOfTagsBytesLength,
|
77
|
+
dataSize,
|
78
|
+
].reduce((totalSize, currentSize) => (totalSize += currentSize));
|
59
79
|
}
|
60
80
|
}
|
61
81
|
exports.TurboNodeArweaveSigner = TurboNodeArweaveSigner;
|
@@ -56,7 +56,10 @@ const createAxiosInstance = ({ axiosConfig = {}, retryConfig = {
|
|
56
56
|
console.debug(`Request failed, ${error}. Retry attempt #${retryCount}...`);
|
57
57
|
},
|
58
58
|
}, }) => {
|
59
|
-
const axiosInstance = axios_1.default.create(
|
59
|
+
const axiosInstance = axios_1.default.create({
|
60
|
+
...axiosConfig,
|
61
|
+
validateStatus: () => true, // don't throw on non-200 status codes
|
62
|
+
});
|
60
63
|
// eslint-disable-next-line
|
61
64
|
if (retryConfig.retries && retryConfig.retries > 0) {
|
62
65
|
(0, axios_retry_1.default)(axiosInstance, retryConfig);
|
@@ -1,28 +1,19 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
-
});
|
10
|
-
};
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
3
|
exports.readableStreamToBuffer = void 0;
|
13
|
-
function readableStreamToBuffer({ stream, }) {
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
4
|
+
async function readableStreamToBuffer({ stream, size, }) {
|
5
|
+
const reader = stream.getReader();
|
6
|
+
const buffer = Buffer.alloc(size);
|
7
|
+
let offset = 0;
|
8
|
+
let done = false;
|
9
|
+
while (!done) {
|
10
|
+
const { done: streamDone, value } = await reader.read();
|
11
|
+
done = streamDone;
|
12
|
+
if (!done) {
|
13
|
+
buffer.set(value, offset);
|
14
|
+
offset += value.byteLength;
|
24
15
|
}
|
25
|
-
|
26
|
-
|
16
|
+
}
|
17
|
+
return buffer;
|
27
18
|
}
|
28
19
|
exports.readableStreamToBuffer = readableStreamToBuffer;
|
package/lib/cjs/web/factory.js
CHANGED
@@ -23,8 +23,14 @@ const signer_js_1 = require("./signer.js");
|
|
23
23
|
class TurboFactory extends factory_js_1.TurboBaseFactory {
|
24
24
|
static authenticated({ privateKey, paymentServiceConfig = {}, uploadServiceConfig = {}, }) {
|
25
25
|
const signer = new signer_js_1.TurboWebArweaveSigner({ privateKey });
|
26
|
-
const paymentService = new index_js_1.TurboAuthenticatedPaymentService(
|
27
|
-
|
26
|
+
const paymentService = new index_js_1.TurboAuthenticatedPaymentService({
|
27
|
+
...paymentServiceConfig,
|
28
|
+
signer,
|
29
|
+
});
|
30
|
+
const uploadService = new index_js_1.TurboAuthenticatedUploadService({
|
31
|
+
...uploadServiceConfig,
|
32
|
+
signer,
|
33
|
+
});
|
28
34
|
return new index_js_1.TurboAuthenticatedClient({
|
29
35
|
uploadService,
|
30
36
|
paymentService,
|
package/lib/cjs/web/signer.js
CHANGED
@@ -1,13 +1,4 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
-
});
|
10
|
-
};
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
4
|
};
|
@@ -39,33 +30,33 @@ class TurboWebArweaveSigner {
|
|
39
30
|
this.privateKey = privateKey;
|
40
31
|
this.signer = new arbundles_1.ArweaveSigner(this.privateKey);
|
41
32
|
}
|
42
|
-
signDataItem({ fileStreamFactory, }) {
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
// TODO: add payload size to get performance improvements
|
48
|
-
});
|
49
|
-
const signedDataItem = (0, arbundles_1.createData)(buffer, this.signer);
|
50
|
-
yield signedDataItem.sign(this.signer);
|
51
|
-
return signedDataItem.getRaw();
|
33
|
+
async signDataItem({ fileStreamFactory, fileSizeFactory, }) {
|
34
|
+
// TODO: converts the readable stream to a buffer bc incrementally signing ReadableStreams is not trivial
|
35
|
+
const buffer = await (0, readableStream_js_1.readableStreamToBuffer)({
|
36
|
+
stream: fileStreamFactory(),
|
37
|
+
size: fileSizeFactory(),
|
52
38
|
});
|
39
|
+
// TODO: support target, anchor and tags for upload
|
40
|
+
const signedDataItem = (0, arbundles_1.createData)(buffer, this.signer, {});
|
41
|
+
await signedDataItem.sign(this.signer);
|
42
|
+
return {
|
43
|
+
// while this returns a Buffer - it needs to match our return type for uploading
|
44
|
+
dataItemStreamFactory: () => signedDataItem.getRaw(),
|
45
|
+
dataItemSizeFactory: () => signedDataItem.getRaw().length,
|
46
|
+
};
|
53
47
|
}
|
54
48
|
// NOTE: this might be better in a parent class or elsewhere - easy enough to leave in here now and does require specific environment version of crypto
|
55
|
-
generateSignedRequestHeaders() {
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
'x-signature': (0, base64_js_1.toB64Url)(Buffer.from(signature)),
|
67
|
-
};
|
68
|
-
});
|
49
|
+
async generateSignedRequestHeaders() {
|
50
|
+
// a bit hacky - but arweave exports cause issues in tests vs. browser
|
51
|
+
const arweave = arweave_1.default.default ?? arweave_1.default;
|
52
|
+
const nonce = (0, node_crypto_1.randomBytes)(16).toString('hex');
|
53
|
+
const buffer = Buffer.from(nonce);
|
54
|
+
const signature = await arweave.crypto.sign(this.privateKey, buffer, {});
|
55
|
+
return {
|
56
|
+
'x-public-key': this.privateKey.n,
|
57
|
+
'x-nonce': nonce,
|
58
|
+
'x-signature': (0, base64_js_1.toB64Url)(Buffer.from(signature)),
|
59
|
+
};
|
69
60
|
}
|
70
61
|
}
|
71
62
|
exports.TurboWebArweaveSigner = TurboWebArweaveSigner;
|
@@ -3,8 +3,12 @@ import { TurboUnauthenticatedClient } from './turbo.js';
|
|
3
3
|
import { TurboUnauthenticatedUploadService } from './upload.js';
|
4
4
|
export class TurboBaseFactory {
|
5
5
|
static unauthenticated({ paymentServiceConfig = {}, uploadServiceConfig = {}, } = {}) {
|
6
|
-
const paymentService = new TurboUnauthenticatedPaymentService(
|
7
|
-
|
6
|
+
const paymentService = new TurboUnauthenticatedPaymentService({
|
7
|
+
...paymentServiceConfig,
|
8
|
+
});
|
9
|
+
const uploadService = new TurboUnauthenticatedUploadService({
|
10
|
+
...uploadServiceConfig,
|
11
|
+
});
|
8
12
|
return new TurboUnauthenticatedClient({
|
9
13
|
uploadService,
|
10
14
|
paymentService,
|
package/lib/esm/common/http.js
CHANGED
@@ -1,12 +1,3 @@
|
|
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
1
|
import { createAxiosInstance } from '../utils/axiosClient.js';
|
11
2
|
import { FailedRequestError } from '../utils/errors.js';
|
12
3
|
export class TurboHTTPService {
|
@@ -18,28 +9,24 @@ export class TurboHTTPService {
|
|
18
9
|
retryConfig,
|
19
10
|
});
|
20
11
|
}
|
21
|
-
get({ endpoint, signal, allowedStatuses = [200, 202], headers, }) {
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
signal,
|
26
|
-
});
|
27
|
-
if (!allowedStatuses.includes(status)) {
|
28
|
-
throw new FailedRequestError(status, statusText);
|
29
|
-
}
|
30
|
-
return data;
|
12
|
+
async get({ endpoint, signal, allowedStatuses = [200, 202], headers, }) {
|
13
|
+
const { status, statusText, data } = await this.axios.get(endpoint, {
|
14
|
+
headers,
|
15
|
+
signal,
|
31
16
|
});
|
17
|
+
if (!allowedStatuses.includes(status)) {
|
18
|
+
throw new FailedRequestError(status, statusText);
|
19
|
+
}
|
20
|
+
return data;
|
32
21
|
}
|
33
|
-
post({ endpoint, signal, allowedStatuses = [200, 202], headers, data, }) {
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
signal,
|
38
|
-
});
|
39
|
-
if (!allowedStatuses.includes(status)) {
|
40
|
-
throw new FailedRequestError(status, statusText);
|
41
|
-
}
|
42
|
-
return response;
|
22
|
+
async post({ endpoint, signal, allowedStatuses = [200, 202], headers, data, }) {
|
23
|
+
const { status, statusText, data: response, } = await this.axios.post(endpoint, data, {
|
24
|
+
headers,
|
25
|
+
signal,
|
43
26
|
});
|
27
|
+
if (!allowedStatuses.includes(status)) {
|
28
|
+
throw new FailedRequestError(status, statusText);
|
29
|
+
}
|
30
|
+
return response;
|
44
31
|
}
|
45
32
|
}
|
@@ -1,12 +1,3 @@
|
|
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
1
|
import { TurboHTTPService } from './http.js';
|
11
2
|
export class TurboUnauthenticatedPaymentService {
|
12
3
|
constructor({ url = 'https://payment.ardrive.dev', retryConfig, }) {
|
@@ -15,48 +6,36 @@ export class TurboUnauthenticatedPaymentService {
|
|
15
6
|
retryConfig,
|
16
7
|
});
|
17
8
|
}
|
18
|
-
getFiatRates() {
|
19
|
-
return
|
20
|
-
|
21
|
-
endpoint: '/rates',
|
22
|
-
});
|
9
|
+
async getFiatRates() {
|
10
|
+
return this.httpService.get({
|
11
|
+
endpoint: '/rates',
|
23
12
|
});
|
24
13
|
}
|
25
|
-
getFiatToAR({ currency, }) {
|
26
|
-
return
|
27
|
-
|
28
|
-
endpoint: `/rates/${currency}`,
|
29
|
-
});
|
14
|
+
async getFiatToAR({ currency, }) {
|
15
|
+
return this.httpService.get({
|
16
|
+
endpoint: `/rates/${currency}`,
|
30
17
|
});
|
31
18
|
}
|
32
|
-
getSupportedCountries() {
|
33
|
-
return
|
34
|
-
|
35
|
-
endpoint: '/countries',
|
36
|
-
});
|
19
|
+
async getSupportedCountries() {
|
20
|
+
return this.httpService.get({
|
21
|
+
endpoint: '/countries',
|
37
22
|
});
|
38
23
|
}
|
39
|
-
getSupportedCurrencies() {
|
40
|
-
return
|
41
|
-
|
42
|
-
endpoint: '/currencies',
|
43
|
-
});
|
24
|
+
async getSupportedCurrencies() {
|
25
|
+
return this.httpService.get({
|
26
|
+
endpoint: '/currencies',
|
44
27
|
});
|
45
28
|
}
|
46
|
-
getUploadCosts({ bytes, }) {
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
return wincCostsForBytes;
|
53
|
-
});
|
29
|
+
async getUploadCosts({ bytes, }) {
|
30
|
+
const fetchPricePromises = bytes.map((byteCount) => this.httpService.get({
|
31
|
+
endpoint: `/price/bytes/${byteCount}`,
|
32
|
+
}));
|
33
|
+
const wincCostsForBytes = await Promise.all(fetchPricePromises);
|
34
|
+
return wincCostsForBytes;
|
54
35
|
}
|
55
|
-
getWincForFiat({ amount, currency }) {
|
56
|
-
return
|
57
|
-
|
58
|
-
endpoint: `/price/${currency}/${amount}`,
|
59
|
-
});
|
36
|
+
async getWincForFiat({ amount, currency }) {
|
37
|
+
return this.httpService.get({
|
38
|
+
endpoint: `/price/${currency}/${amount}`,
|
60
39
|
});
|
61
40
|
}
|
62
41
|
}
|
@@ -66,16 +45,14 @@ export class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymen
|
|
66
45
|
super({ url, retryConfig });
|
67
46
|
this.signer = signer;
|
68
47
|
}
|
69
|
-
getBalance() {
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
allowedStatuses: [200, 404],
|
76
|
-
});
|
77
|
-
// 404's don't return a balance, so default to 0
|
78
|
-
return balance.winc ? balance : { winc: '0' };
|
48
|
+
async getBalance() {
|
49
|
+
const headers = await this.signer.generateSignedRequestHeaders();
|
50
|
+
const balance = await this.httpService.get({
|
51
|
+
endpoint: '/balance',
|
52
|
+
headers,
|
53
|
+
allowedStatuses: [200, 404],
|
79
54
|
});
|
55
|
+
// 404's don't return a balance, so default to 0
|
56
|
+
return balance.winc ? balance : { winc: '0' };
|
80
57
|
}
|
81
58
|
}
|
package/lib/esm/common/turbo.js
CHANGED
@@ -1,12 +1,3 @@
|
|
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
1
|
import { TurboUnauthenticatedPaymentService } from './payment.js';
|
11
2
|
import { TurboUnauthenticatedUploadService } from './upload.js';
|
12
3
|
export class TurboUnauthenticatedClient {
|
@@ -17,10 +8,8 @@ export class TurboUnauthenticatedClient {
|
|
17
8
|
/**
|
18
9
|
* Returns the supported fiat currency conversion rate for 1AR based on current market prices.
|
19
10
|
*/
|
20
|
-
getFiatToAR({ currency, }) {
|
21
|
-
return
|
22
|
-
return this.paymentService.getFiatToAR({ currency });
|
23
|
-
});
|
11
|
+
async getFiatToAR({ currency, }) {
|
12
|
+
return this.paymentService.getFiatToAR({ currency });
|
24
13
|
}
|
25
14
|
/**
|
26
15
|
* Returns the latest conversion rates to purchase 1GiB of data for all supported currencies, including all adjustments and fees.
|
@@ -28,52 +17,41 @@ export class TurboUnauthenticatedClient {
|
|
28
17
|
* Note: this does not take into account varying adjustments and promotions for different sizes of data. If you want to calculate the total
|
29
18
|
* cost in 'winc' for a given number of bytes, use getUploadCosts.
|
30
19
|
*/
|
31
|
-
getFiatRates() {
|
32
|
-
return
|
33
|
-
return this.paymentService.getFiatRates();
|
34
|
-
});
|
20
|
+
async getFiatRates() {
|
21
|
+
return this.paymentService.getFiatRates();
|
35
22
|
}
|
36
23
|
/**
|
37
24
|
* Returns a comprehensive list of supported countries that can purchase credits through the Turbo Payment Service.
|
38
25
|
*/
|
39
|
-
getSupportedCountries() {
|
40
|
-
return
|
41
|
-
return this.paymentService.getSupportedCountries();
|
42
|
-
});
|
26
|
+
async getSupportedCountries() {
|
27
|
+
return this.paymentService.getSupportedCountries();
|
43
28
|
}
|
44
29
|
/**
|
45
30
|
* Returns a list of all supported fiat currencies.
|
46
31
|
*/
|
47
|
-
getSupportedCurrencies() {
|
48
|
-
return
|
49
|
-
return this.paymentService.getSupportedCurrencies();
|
50
|
-
});
|
32
|
+
async getSupportedCurrencies() {
|
33
|
+
return this.paymentService.getSupportedCurrencies();
|
51
34
|
}
|
52
35
|
/**
|
53
36
|
* Determines the price in 'winc' to upload one data item of a specific size in bytes, including all Turbo cost adjustments and fees.
|
54
37
|
*/
|
55
|
-
getUploadCosts({ bytes, }) {
|
56
|
-
return
|
57
|
-
return this.paymentService.getUploadCosts({ bytes });
|
58
|
-
});
|
38
|
+
async getUploadCosts({ bytes, }) {
|
39
|
+
return this.paymentService.getUploadCosts({ bytes });
|
59
40
|
}
|
60
41
|
/**
|
61
42
|
* Determines the amount of 'winc' that would be returned for a given currency and amount, including all Turbo cost adjustments and fees.
|
62
43
|
*/
|
63
|
-
getWincForFiat({ amount, currency, }) {
|
64
|
-
return
|
65
|
-
return this.paymentService.getWincForFiat({ amount, currency });
|
66
|
-
});
|
44
|
+
async getWincForFiat({ amount, currency, }) {
|
45
|
+
return this.paymentService.getWincForFiat({ amount, currency });
|
67
46
|
}
|
68
47
|
/**
|
69
48
|
* Uploads a signed data item to the Turbo Upload Service.
|
70
49
|
*/
|
71
|
-
uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
|
72
|
-
return
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
});
|
50
|
+
async uploadSignedDataItem({ dataItemStreamFactory, dataItemSizeFactory, signal, }) {
|
51
|
+
return this.uploadService.uploadSignedDataItem({
|
52
|
+
dataItemStreamFactory,
|
53
|
+
dataItemSizeFactory,
|
54
|
+
signal,
|
77
55
|
});
|
78
56
|
}
|
79
57
|
}
|
@@ -84,17 +62,17 @@ export class TurboAuthenticatedClient extends TurboUnauthenticatedClient {
|
|
84
62
|
/**
|
85
63
|
* Returns the current balance of the user's wallet in 'winc'.
|
86
64
|
*/
|
87
|
-
getBalance() {
|
88
|
-
return
|
89
|
-
return this.paymentService.getBalance();
|
90
|
-
});
|
65
|
+
async getBalance() {
|
66
|
+
return this.paymentService.getBalance();
|
91
67
|
}
|
92
68
|
/**
|
93
69
|
* Signs and uploads raw data to the Turbo Upload Service.
|
94
70
|
*/
|
95
|
-
uploadFile({ fileStreamFactory, signal, }) {
|
96
|
-
return
|
97
|
-
|
71
|
+
async uploadFile({ fileStreamFactory, fileSizeFactory, signal, }) {
|
72
|
+
return this.uploadService.uploadFile({
|
73
|
+
fileStreamFactory,
|
74
|
+
fileSizeFactory,
|
75
|
+
signal,
|
98
76
|
});
|
99
77
|
}
|
100
78
|
}
|