@ardrive/turbo-sdk 1.0.0-alpha.9 → 1.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/README.md +169 -28
  2. package/bundles/web.bundle.min.js +55776 -0
  3. package/lib/cjs/common/currency.js +42 -0
  4. package/lib/cjs/common/factory.js +6 -2
  5. package/lib/cjs/common/http.js +16 -29
  6. package/lib/cjs/common/index.js +1 -0
  7. package/lib/cjs/common/payment.js +56 -47
  8. package/lib/cjs/common/turbo.js +24 -40
  9. package/lib/cjs/common/upload.js +27 -34
  10. package/lib/cjs/node/factory.js +8 -2
  11. package/lib/cjs/node/index.js +1 -1
  12. package/lib/cjs/node/signer.js +42 -22
  13. package/lib/cjs/package.json +1 -0
  14. package/lib/cjs/utils/axiosClient.js +18 -9
  15. package/lib/cjs/utils/readableStream.js +13 -22
  16. package/lib/cjs/web/factory.js +8 -2
  17. package/lib/cjs/web/index.js +1 -1
  18. package/lib/cjs/web/signer.js +24 -31
  19. package/lib/esm/common/currency.js +27 -0
  20. package/lib/esm/common/factory.js +6 -2
  21. package/lib/esm/common/http.js +16 -29
  22. package/lib/esm/common/index.js +1 -0
  23. package/lib/esm/common/payment.js +56 -47
  24. package/lib/esm/common/turbo.js +24 -40
  25. package/lib/esm/common/upload.js +27 -34
  26. package/lib/esm/node/factory.js +8 -2
  27. package/lib/esm/node/index.js +1 -1
  28. package/lib/esm/node/signer.js +42 -22
  29. package/lib/esm/utils/axiosClient.js +18 -9
  30. package/lib/esm/utils/readableStream.js +13 -22
  31. package/lib/esm/web/factory.js +8 -2
  32. package/lib/esm/web/index.js +1 -1
  33. package/lib/esm/web/signer.js +24 -31
  34. package/lib/types/common/currency.d.ts +43 -0
  35. package/lib/types/common/currency.d.ts.map +1 -0
  36. package/lib/{cjs → types}/common/factory.d.ts +2 -1
  37. package/lib/types/common/factory.d.ts.map +1 -0
  38. package/lib/{cjs → types}/common/http.d.ts +4 -3
  39. package/lib/types/common/http.d.ts.map +1 -0
  40. package/lib/{esm → types}/common/index.d.ts +2 -0
  41. package/lib/types/common/index.d.ts.map +1 -0
  42. package/lib/{esm/types/arweave.d.ts → types/common/jwk.d.ts} +1 -0
  43. package/lib/types/common/jwk.d.ts.map +1 -0
  44. package/lib/{esm → types}/common/payment.d.ts +14 -6
  45. package/lib/types/common/payment.d.ts.map +1 -0
  46. package/lib/{cjs → types}/common/turbo.d.ts +13 -11
  47. package/lib/types/common/turbo.d.ts.map +1 -0
  48. package/lib/{esm → types}/common/upload.d.ts +4 -4
  49. package/lib/types/common/upload.d.ts.map +1 -0
  50. package/lib/{esm → types}/node/factory.d.ts +2 -1
  51. package/lib/types/node/factory.d.ts.map +1 -0
  52. package/lib/{esm → types}/node/index.d.ts +2 -1
  53. package/lib/types/node/index.d.ts.map +1 -0
  54. package/lib/{cjs → types}/node/signer.d.ts +10 -4
  55. package/lib/types/node/signer.d.ts.map +1 -0
  56. package/lib/{cjs/types/turbo.d.ts → types/types.d.ts} +46 -11
  57. package/lib/types/types.d.ts.map +1 -0
  58. package/lib/{esm → types}/utils/axiosClient.d.ts +3 -2
  59. package/lib/types/utils/axiosClient.d.ts.map +1 -0
  60. package/lib/{cjs → types}/utils/base64.d.ts +3 -2
  61. package/lib/types/utils/base64.d.ts.map +1 -0
  62. package/lib/{esm → types}/utils/errors.d.ts +1 -0
  63. package/lib/types/utils/errors.d.ts.map +1 -0
  64. package/lib/{cjs → types}/utils/readableStream.d.ts +3 -1
  65. package/lib/types/utils/readableStream.d.ts.map +1 -0
  66. package/lib/{cjs → types}/web/factory.d.ts +2 -1
  67. package/lib/types/web/factory.d.ts.map +1 -0
  68. package/lib/{cjs → types}/web/index.d.ts +2 -1
  69. package/lib/types/web/index.d.ts.map +1 -0
  70. package/lib/{cjs → types}/web/signer.d.ts +9 -4
  71. package/lib/types/web/signer.d.ts.map +1 -0
  72. package/package.json +13 -15
  73. package/lib/cjs/common/index.d.ts +0 -19
  74. package/lib/cjs/common/payment.d.ts +0 -41
  75. package/lib/cjs/common/upload.d.ts +0 -29
  76. package/lib/cjs/node/factory.d.ts +0 -22
  77. package/lib/cjs/node/index.d.ts +0 -20
  78. package/lib/cjs/types/arweave.d.ts +0 -29
  79. package/lib/cjs/types/index.d.ts +0 -18
  80. package/lib/cjs/types/index.js +0 -34
  81. package/lib/cjs/utils/axiosClient.d.ts +0 -23
  82. package/lib/cjs/utils/errors.d.ts +0 -22
  83. package/lib/esm/common/factory.d.ts +0 -21
  84. package/lib/esm/common/http.d.ts +0 -44
  85. package/lib/esm/common/turbo.d.ts +0 -73
  86. package/lib/esm/node/signer.d.ts +0 -36
  87. package/lib/esm/types/index.d.ts +0 -18
  88. package/lib/esm/types/index.js +0 -18
  89. package/lib/esm/types/turbo.d.ts +0 -149
  90. package/lib/esm/utils/base64.d.ts +0 -9
  91. package/lib/esm/utils/readableStream.d.ts +0 -22
  92. package/lib/esm/web/factory.d.ts +0 -22
  93. package/lib/esm/web/index.d.ts +0 -20
  94. package/lib/esm/web/signer.d.ts +0 -37
  95. /package/lib/cjs/{types/arweave.js → common/jwk.js} +0 -0
  96. /package/lib/cjs/{types/turbo.js → types.js} +0 -0
  97. /package/lib/esm/{types/arweave.js → common/jwk.js} +0 -0
  98. /package/lib/esm/{types/turbo.js → types.js} +0 -0
@@ -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(Object.assign(Object.assign({}, paymentServiceConfig), { signer }));
27
- const uploadService = new index_js_1.TurboAuthenticatedUploadService(Object.assign(Object.assign({}, uploadServiceConfig), { signer }));
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,
@@ -33,4 +33,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
33
  __exportStar(require("./factory.js"), exports);
34
34
  __exportStar(require("./signer.js"), exports);
35
35
  __exportStar(require("../common/index.js"), exports);
36
- __exportStar(require("../types/index.js"), exports);
36
+ __exportStar(require("../types.js"), exports);
@@ -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,31 +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
- return __awaiter(this, void 0, void 0, function* () {
44
- // TODO: converts the readable stream to a buffer bc incrementally signing ReadableStreams is not trivial
45
- const buffer = yield (0, readableStream_js_1.readableStreamToBuffer)({
46
- stream: fileStreamFactory(),
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
- return __awaiter(this, void 0, void 0, function* () {
57
- const nonce = (0, node_crypto_1.randomBytes)(16).toString('hex');
58
- const buffer = Buffer.from(nonce);
59
- // a bit hacky - but easiest way to solve web signing issues while still building for cjs
60
- const signature = yield arweave_1.default.default.crypto.sign(this.privateKey, buffer, {});
61
- return {
62
- 'x-public-key': this.privateKey.n,
63
- 'x-nonce': nonce,
64
- 'x-signature': (0, base64_js_1.toB64Url)(Buffer.from(signature)),
65
- };
66
- });
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
+ };
67
60
  }
68
61
  }
69
62
  exports.TurboWebArweaveSigner = TurboWebArweaveSigner;
@@ -0,0 +1,27 @@
1
+ export class ZeroDecimalCurrency {
2
+ constructor(amount, type) {
3
+ this.amount = amount;
4
+ this.type = type;
5
+ }
6
+ }
7
+ export class TwoDecimalCurrency {
8
+ constructor(a, type) {
9
+ this.a = a;
10
+ this.type = type;
11
+ }
12
+ get amount() {
13
+ return this.a * 100;
14
+ }
15
+ }
16
+ // Two decimal currencies that are supported by the Turbo API
17
+ export const USD = (usd) => new TwoDecimalCurrency(usd, 'usd');
18
+ export const EUR = (eur) => new TwoDecimalCurrency(eur, 'eur');
19
+ export const GBP = (gbp) => new TwoDecimalCurrency(gbp, 'gbp');
20
+ export const CAD = (cad) => new TwoDecimalCurrency(cad, 'cad');
21
+ export const AUD = (aud) => new TwoDecimalCurrency(aud, 'aud');
22
+ export const INR = (inr) => new TwoDecimalCurrency(inr, 'inr');
23
+ export const SGD = (sgd) => new TwoDecimalCurrency(sgd, 'sgd');
24
+ export const HKD = (hkd) => new TwoDecimalCurrency(hkd, 'hkd');
25
+ export const BRL = (brl) => new TwoDecimalCurrency(brl, 'brl');
26
+ // Zero decimal currencies that are supported by the Turbo API
27
+ export const JPY = (jpy) => new ZeroDecimalCurrency(jpy, 'jpy');
@@ -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(Object.assign({}, paymentServiceConfig));
7
- const uploadService = new TurboUnauthenticatedUploadService(Object.assign({}, uploadServiceConfig));
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,
@@ -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
- 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;
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
- 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;
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
  }
@@ -17,3 +17,4 @@
17
17
  export * from './upload.js';
18
18
  export * from './payment.js';
19
19
  export * from './turbo.js';
20
+ export * from './currency.js';
@@ -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, }) {
@@ -16,48 +7,59 @@ export class TurboUnauthenticatedPaymentService {
16
7
  });
17
8
  }
18
9
  getFiatRates() {
19
- return __awaiter(this, void 0, void 0, function* () {
20
- return this.httpService.get({
21
- endpoint: '/rates',
22
- });
10
+ return this.httpService.get({
11
+ endpoint: '/rates',
23
12
  });
24
13
  }
25
14
  getFiatToAR({ currency, }) {
26
- return __awaiter(this, void 0, void 0, function* () {
27
- return this.httpService.get({
28
- endpoint: `/rates/${currency}`,
29
- });
15
+ return this.httpService.get({
16
+ endpoint: `/rates/${currency}`,
30
17
  });
31
18
  }
32
19
  getSupportedCountries() {
33
- return __awaiter(this, void 0, void 0, function* () {
34
- return this.httpService.get({
35
- endpoint: '/countries',
36
- });
20
+ return this.httpService.get({
21
+ endpoint: '/countries',
37
22
  });
38
23
  }
39
24
  getSupportedCurrencies() {
40
- return __awaiter(this, void 0, void 0, function* () {
41
- return this.httpService.get({
42
- endpoint: '/currencies',
43
- });
25
+ return this.httpService.get({
26
+ endpoint: '/currencies',
44
27
  });
45
28
  }
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;
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;
35
+ }
36
+ getWincForFiat({ amount, }) {
37
+ const { amount: paymentAmount, type: currencyType } = amount;
38
+ return this.httpService.get({
39
+ endpoint: `/price/${currencyType}/${paymentAmount}`,
53
40
  });
54
41
  }
55
- getWincForFiat({ amount, currency }) {
56
- return __awaiter(this, void 0, void 0, function* () {
57
- return this.httpService.get({
58
- endpoint: `/price/${currency}/${amount}`,
59
- });
42
+ appendPromoCodesToQuery(promoCodes) {
43
+ const promoCodesQuery = promoCodes.join(',');
44
+ return promoCodesQuery ? `?promoCode=${promoCodesQuery}` : '';
45
+ }
46
+ async getCheckout({ amount, owner, promoCodes = [] }, headers) {
47
+ const { amount: paymentAmount, type: currencyType } = amount;
48
+ const endpoint = `/top-up/checkout-session/${owner}/${currencyType}/${paymentAmount}${this.appendPromoCodesToQuery(promoCodes)}`;
49
+ const { adjustments, paymentSession, topUpQuote } = await this.httpService.get({
50
+ endpoint,
51
+ headers,
60
52
  });
53
+ return {
54
+ winc: topUpQuote.winstonCreditAmount,
55
+ adjustments,
56
+ url: paymentSession.url,
57
+ paymentAmount: topUpQuote.paymentAmount,
58
+ quotedPaymentAmount: topUpQuote.quotedPaymentAmount,
59
+ };
60
+ }
61
+ createCheckoutSession(params) {
62
+ return this.getCheckout(params);
61
63
  }
62
64
  }
63
65
  // NOTE: to avoid redundancy, we use inheritance here - but generally prefer composition over inheritance
@@ -66,16 +68,23 @@ export class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymen
66
68
  super({ url, retryConfig });
67
69
  this.signer = signer;
68
70
  }
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' };
71
+ async getBalance() {
72
+ const headers = await this.signer.generateSignedRequestHeaders();
73
+ const balance = await this.httpService.get({
74
+ endpoint: '/balance',
75
+ headers,
76
+ allowedStatuses: [200, 404],
79
77
  });
78
+ // 404's don't return a balance, so default to 0
79
+ return balance.winc ? balance : { winc: '0' };
80
+ }
81
+ async getWincForFiat({ amount, promoCodes = [], }) {
82
+ return this.httpService.get({
83
+ endpoint: `/price/${amount.type}/${amount.amount}${this.appendPromoCodesToQuery(promoCodes)}`,
84
+ headers: await this.signer.generateSignedRequestHeaders(),
85
+ });
86
+ }
87
+ async createCheckoutSession(params) {
88
+ return this.getCheckout(params, await this.signer.generateSignedRequestHeaders());
80
89
  }
81
90
  }
@@ -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 {
@@ -18,9 +9,7 @@ export class TurboUnauthenticatedClient {
18
9
  * Returns the supported fiat currency conversion rate for 1AR based on current market prices.
19
10
  */
20
11
  getFiatToAR({ currency, }) {
21
- return __awaiter(this, void 0, void 0, function* () {
22
- return this.paymentService.getFiatToAR({ currency });
23
- });
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.
@@ -29,53 +18,48 @@ export class TurboUnauthenticatedClient {
29
18
  * cost in 'winc' for a given number of bytes, use getUploadCosts.
30
19
  */
31
20
  getFiatRates() {
32
- return __awaiter(this, void 0, void 0, function* () {
33
- return this.paymentService.getFiatRates();
34
- });
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
26
  getSupportedCountries() {
40
- return __awaiter(this, void 0, void 0, function* () {
41
- return this.paymentService.getSupportedCountries();
42
- });
27
+ return this.paymentService.getSupportedCountries();
43
28
  }
44
29
  /**
45
30
  * Returns a list of all supported fiat currencies.
46
31
  */
47
32
  getSupportedCurrencies() {
48
- return __awaiter(this, void 0, void 0, function* () {
49
- return this.paymentService.getSupportedCurrencies();
50
- });
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
38
  getUploadCosts({ bytes, }) {
56
- return __awaiter(this, void 0, void 0, function* () {
57
- return this.paymentService.getUploadCosts({ bytes });
58
- });
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 __awaiter(this, void 0, void 0, function* () {
65
- return this.paymentService.getWincForFiat({ amount, currency });
66
- });
44
+ getWincForFiat(params) {
45
+ return this.paymentService.getWincForFiat(params);
67
46
  }
68
47
  /**
69
48
  * Uploads a signed data item to the Turbo Upload Service.
70
49
  */
71
- uploadSignedDataItem({ dataItemStreamFactory, signal, }) {
72
- return __awaiter(this, void 0, void 0, function* () {
73
- return this.uploadService.uploadSignedDataItem({
74
- dataItemStreamFactory,
75
- signal,
76
- });
50
+ uploadSignedDataItem({ dataItemStreamFactory, dataItemSizeFactory, signal, }) {
51
+ return this.uploadService.uploadSignedDataItem({
52
+ dataItemStreamFactory,
53
+ dataItemSizeFactory,
54
+ signal,
77
55
  });
78
56
  }
57
+ /**
58
+ * Creates a Turbo Checkout Session for a given amount and currency.
59
+ */
60
+ createCheckoutSession(params) {
61
+ return this.paymentService.createCheckoutSession(params);
62
+ }
79
63
  }
80
64
  export class TurboAuthenticatedClient extends TurboUnauthenticatedClient {
81
65
  constructor({ paymentService, uploadService, }) {
@@ -85,16 +69,16 @@ export class TurboAuthenticatedClient extends TurboUnauthenticatedClient {
85
69
  * Returns the current balance of the user's wallet in 'winc'.
86
70
  */
87
71
  getBalance() {
88
- return __awaiter(this, void 0, void 0, function* () {
89
- return this.paymentService.getBalance();
90
- });
72
+ return this.paymentService.getBalance();
91
73
  }
92
74
  /**
93
75
  * Signs and uploads raw data to the Turbo Upload Service.
94
76
  */
95
- uploadFile({ fileStreamFactory, signal, }) {
96
- return __awaiter(this, void 0, void 0, function* () {
97
- return this.uploadService.uploadFile({ fileStreamFactory, signal });
77
+ uploadFile({ fileStreamFactory, fileSizeFactory, signal, }) {
78
+ return this.uploadService.uploadFile({
79
+ fileStreamFactory,
80
+ fileSizeFactory,
81
+ signal,
98
82
  });
99
83
  }
100
84
  }
@@ -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 TurboUnauthenticatedUploadService {
12
3
  constructor({ url = 'https://upload.ardrive.dev', retryConfig, }) {
@@ -15,17 +6,17 @@ export class TurboUnauthenticatedUploadService {
15
6
  retryConfig,
16
7
  });
17
8
  }
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
- });
9
+ async uploadSignedDataItem({ dataItemStreamFactory, dataItemSizeFactory, signal, }) {
10
+ const fileSize = dataItemSizeFactory();
11
+ // TODO: add p-limit constraint or replace with separate upload class
12
+ return this.httpService.post({
13
+ endpoint: `/tx`,
14
+ signal,
15
+ data: dataItemStreamFactory(),
16
+ headers: {
17
+ 'content-type': 'application/octet-stream',
18
+ 'content-length': `${fileSize}`,
19
+ },
29
20
  });
30
21
  }
31
22
  }
@@ -35,20 +26,22 @@ export class TurboAuthenticatedUploadService extends TurboUnauthenticatedUploadS
35
26
  super({ url, retryConfig });
36
27
  this.signer = signer;
37
28
  }
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
- });
29
+ async uploadFile({ fileStreamFactory, fileSizeFactory, signal, }) {
30
+ const { dataItemStreamFactory, dataItemSizeFactory } = await this.signer.signDataItem({
31
+ fileStreamFactory,
32
+ fileSizeFactory,
33
+ });
34
+ const signedDataItem = dataItemStreamFactory();
35
+ const fileSize = dataItemSizeFactory();
36
+ // TODO: add p-limit constraint or replace with separate upload class
37
+ return this.httpService.post({
38
+ endpoint: `/tx`,
39
+ signal,
40
+ data: signedDataItem,
41
+ headers: {
42
+ 'content-type': 'application/octet-stream',
43
+ 'content-length': `${fileSize}`,
44
+ },
52
45
  });
53
46
  }
54
47
  }
@@ -20,8 +20,14 @@ import { TurboNodeArweaveSigner } from './signer.js';
20
20
  export class TurboFactory extends TurboBaseFactory {
21
21
  static authenticated({ privateKey, paymentServiceConfig = {}, uploadServiceConfig = {}, }) {
22
22
  const signer = new TurboNodeArweaveSigner({ privateKey });
23
- const paymentService = new TurboAuthenticatedPaymentService(Object.assign(Object.assign({}, paymentServiceConfig), { signer }));
24
- const uploadService = new TurboAuthenticatedUploadService(Object.assign(Object.assign({}, uploadServiceConfig), { signer }));
23
+ const paymentService = new TurboAuthenticatedPaymentService({
24
+ ...paymentServiceConfig,
25
+ signer,
26
+ });
27
+ const uploadService = new TurboAuthenticatedUploadService({
28
+ ...uploadServiceConfig,
29
+ signer,
30
+ });
25
31
  return new TurboAuthenticatedClient({
26
32
  uploadService,
27
33
  paymentService,
@@ -16,5 +16,5 @@
16
16
  */
17
17
  export * from './factory.js';
18
18
  export * from './signer.js';
19
+ export * from '../types.js';
19
20
  export * from '../common/index.js';
20
- export * from '../types/index.js';
@@ -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
  /**
11
2
  * Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved.
12
3
  *
@@ -33,22 +24,51 @@ export class TurboNodeArweaveSigner {
33
24
  this.privateKey = privateKey;
34
25
  this.signer = new ArweaveSigner(this.privateKey);
35
26
  }
36
- signDataItem({ fileStreamFactory, }) {
27
+ async signDataItem({ fileStreamFactory, fileSizeFactory, }) {
37
28
  // TODO: replace with our own signer implementation
38
29
  const [stream1, stream2] = [fileStreamFactory(), fileStreamFactory()];
39
- return streamSigner(stream1, stream2, this.signer);
30
+ const signedDataItem = await streamSigner(stream1, stream2, this.signer);
31
+ // TODO: support target, anchor, and tags
32
+ const signedDataItemSize = this.calculateSignedDataHeadersSize({
33
+ dataSize: fileSizeFactory(),
34
+ });
35
+ return {
36
+ dataItemStreamFactory: () => signedDataItem,
37
+ dataItemSizeFactory: () => signedDataItemSize,
38
+ };
40
39
  }
41
40
  // 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
42
- generateSignedRequestHeaders() {
43
- return __awaiter(this, void 0, void 0, function* () {
44
- const nonce = randomBytes(16).toString('hex');
45
- const buffer = Buffer.from(nonce);
46
- const signature = yield Arweave.crypto.sign(this.privateKey, buffer);
47
- return {
48
- 'x-public-key': this.privateKey.n,
49
- 'x-nonce': nonce,
50
- 'x-signature': toB64Url(Buffer.from(signature)),
51
- };
52
- });
41
+ async generateSignedRequestHeaders() {
42
+ const nonce = randomBytes(16).toString('hex');
43
+ const buffer = Buffer.from(nonce);
44
+ const signature = await Arweave.crypto.sign(this.privateKey, buffer);
45
+ return {
46
+ 'x-public-key': this.privateKey.n,
47
+ 'x-nonce': nonce,
48
+ 'x-signature': toB64Url(Buffer.from(signature)),
49
+ };
50
+ }
51
+ // TODO: make dynamic that accepts anchor and target and tags to return the size of the headers + data
52
+ // reference https://github.com/ArweaveTeam/arweave-standards/blob/master/ans/ANS-104.md#13-dataitem-format
53
+ calculateSignedDataHeadersSize({ dataSize }) {
54
+ const anchor = 1; // + whatever they provide (max of 33)
55
+ const target = 1; // + whatever they provide (max of 33)
56
+ const tags = 0;
57
+ const signatureLength = 512;
58
+ const ownerLength = 512;
59
+ const signatureTypeLength = 2;
60
+ const numOfTagsLength = 8; // https://github.com/Bundlr-Network/arbundles/blob/master/src/tags.ts#L191-L198
61
+ const numOfTagsBytesLength = 8;
62
+ return [
63
+ anchor,
64
+ target,
65
+ tags,
66
+ signatureLength,
67
+ ownerLength,
68
+ signatureTypeLength,
69
+ numOfTagsLength,
70
+ numOfTagsBytesLength,
71
+ dataSize,
72
+ ].reduce((totalSize, currentSize) => (totalSize += currentSize));
53
73
  }
54
74
  }