@ardrive/turbo-sdk 1.7.1 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundles/web.bundle.min.js +68876 -50766
- package/lib/cjs/common/factory.js +98 -0
- package/lib/cjs/common/index.js +1 -1
- package/lib/cjs/common/payment.js +5 -12
- package/lib/cjs/common/signer.js +14 -0
- package/lib/cjs/common/{token.js → token/arweave.js} +13 -10
- package/lib/cjs/common/token/index.js +26 -0
- package/lib/cjs/common/token/solana.js +112 -0
- package/lib/cjs/node/factory.js +0 -49
- package/lib/cjs/types.js +4 -2
- package/lib/cjs/version.js +1 -1
- package/lib/cjs/web/factory.js +46 -13
- package/lib/esm/common/factory.js +101 -3
- package/lib/esm/common/index.js +1 -1
- package/lib/esm/common/payment.js +5 -12
- package/lib/esm/common/signer.js +11 -0
- package/lib/esm/common/{token.js → token/arweave.js} +13 -10
- package/lib/esm/common/token/index.js +9 -0
- package/lib/esm/common/token/solana.js +103 -0
- package/lib/esm/node/factory.js +0 -49
- package/lib/esm/types.js +2 -1
- package/lib/esm/version.js +1 -1
- package/lib/esm/web/factory.js +47 -14
- package/lib/types/common/factory.d.ts +5 -18
- package/lib/types/common/factory.d.ts.map +1 -1
- package/lib/types/common/index.d.ts +1 -1
- package/lib/types/common/index.d.ts.map +1 -1
- package/lib/types/common/payment.d.ts +3 -3
- package/lib/types/common/payment.d.ts.map +1 -1
- package/lib/types/common/signer.d.ts.map +1 -1
- package/lib/types/common/{token.d.ts → token/arweave.d.ts} +12 -15
- package/lib/types/common/token/arweave.d.ts.map +1 -0
- package/lib/types/common/token/index.d.ts +21 -0
- package/lib/types/common/token/index.d.ts.map +1 -0
- package/lib/types/common/token/solana.d.ts +37 -0
- package/lib/types/common/token/solana.d.ts.map +1 -0
- package/lib/types/node/factory.d.ts +16 -3
- package/lib/types/node/factory.d.ts.map +1 -1
- package/lib/types/types.d.ts +31 -17
- package/lib/types/types.d.ts.map +1 -1
- package/lib/types/version.d.ts +1 -1
- package/lib/types/version.d.ts.map +1 -1
- package/lib/types/web/factory.d.ts +4 -2
- package/lib/types/web/factory.d.ts.map +1 -1
- package/package.json +4 -2
- package/lib/types/common/token.d.ts.map +0 -1
@@ -1,7 +1,28 @@
|
|
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 { ArweaveSigner, EthereumSigner, HexSolanaSigner } from 'arbundles';
|
18
|
+
import { TurboNodeSigner } from '../node/signer.js';
|
19
|
+
import { isJWK, } from '../types.js';
|
20
|
+
import { TurboWebArweaveSigner } from '../web/signer.js';
|
1
21
|
import { TurboWinstonLogger } from './logger.js';
|
2
|
-
import { TurboUnauthenticatedPaymentService } from './payment.js';
|
3
|
-
import {
|
4
|
-
import {
|
22
|
+
import { TurboAuthenticatedPaymentService, TurboUnauthenticatedPaymentService, } from './payment.js';
|
23
|
+
import { defaultTokenMap } from './token/index.js';
|
24
|
+
import { TurboAuthenticatedClient, TurboUnauthenticatedClient, } from './turbo.js';
|
25
|
+
import { TurboAuthenticatedUploadService, TurboUnauthenticatedUploadService, } from './upload.js';
|
5
26
|
export class TurboBaseFactory {
|
6
27
|
static setLogLevel(level) {
|
7
28
|
this.logger.setLogLevel(level);
|
@@ -23,5 +44,82 @@ export class TurboBaseFactory {
|
|
23
44
|
paymentService,
|
24
45
|
});
|
25
46
|
}
|
47
|
+
static getSigner(providedSigner, providedPrivateKey, token) {
|
48
|
+
let signer;
|
49
|
+
if (providedSigner !== undefined) {
|
50
|
+
signer = providedSigner;
|
51
|
+
}
|
52
|
+
else if (providedPrivateKey !== undefined) {
|
53
|
+
if (token === 'solana') {
|
54
|
+
signer = new HexSolanaSigner(providedPrivateKey);
|
55
|
+
// TODO: else if (token === 'ethereum') {signer = new EthereumSigner(providedPrivateKey);}
|
56
|
+
}
|
57
|
+
else {
|
58
|
+
if (!isJWK(providedPrivateKey)) {
|
59
|
+
throw new Error('A JWK must be provided for ArweaveSigner.');
|
60
|
+
}
|
61
|
+
signer = new ArweaveSigner(providedPrivateKey);
|
62
|
+
}
|
63
|
+
}
|
64
|
+
else {
|
65
|
+
throw new Error('A privateKey or signer must be provided.');
|
66
|
+
}
|
67
|
+
if (typeof window !== 'undefined') {
|
68
|
+
return new TurboWebArweaveSigner({
|
69
|
+
signer,
|
70
|
+
logger: this.logger,
|
71
|
+
});
|
72
|
+
}
|
73
|
+
return new TurboNodeSigner({
|
74
|
+
signer,
|
75
|
+
logger: this.logger,
|
76
|
+
});
|
77
|
+
}
|
78
|
+
static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, tokenMap, gatewayUrl, tokenTools, }) {
|
79
|
+
if (!token) {
|
80
|
+
if (providedSigner) {
|
81
|
+
// Derive token from signer if not provided
|
82
|
+
if (providedSigner instanceof EthereumSigner) {
|
83
|
+
token = 'ethereum';
|
84
|
+
}
|
85
|
+
else if (providedSigner instanceof HexSolanaSigner) {
|
86
|
+
token = 'solana';
|
87
|
+
}
|
88
|
+
else {
|
89
|
+
token = 'arweave';
|
90
|
+
}
|
91
|
+
}
|
92
|
+
else {
|
93
|
+
token = 'arweave';
|
94
|
+
}
|
95
|
+
}
|
96
|
+
const turboSigner = this.getSigner(providedSigner, privateKey, token);
|
97
|
+
if (!tokenTools) {
|
98
|
+
if (tokenMap && token === 'arweave') {
|
99
|
+
tokenTools = tokenMap.arweave;
|
100
|
+
}
|
101
|
+
tokenTools = defaultTokenMap[token]?.({
|
102
|
+
gatewayUrl,
|
103
|
+
logger: this.logger,
|
104
|
+
});
|
105
|
+
}
|
106
|
+
const paymentService = new TurboAuthenticatedPaymentService({
|
107
|
+
...paymentServiceConfig,
|
108
|
+
signer: turboSigner,
|
109
|
+
logger: this.logger,
|
110
|
+
token,
|
111
|
+
tokenTools,
|
112
|
+
});
|
113
|
+
const uploadService = new TurboAuthenticatedUploadService({
|
114
|
+
...uploadServiceConfig,
|
115
|
+
signer: turboSigner,
|
116
|
+
logger: this.logger,
|
117
|
+
token,
|
118
|
+
});
|
119
|
+
return new TurboAuthenticatedClient({
|
120
|
+
uploadService,
|
121
|
+
paymentService,
|
122
|
+
});
|
123
|
+
}
|
26
124
|
}
|
27
125
|
TurboBaseFactory.logger = new TurboWinstonLogger();
|
package/lib/esm/common/index.js
CHANGED
@@ -17,7 +17,6 @@
|
|
17
17
|
import { BigNumber } from 'bignumber.js';
|
18
18
|
import { TurboHTTPService } from './http.js';
|
19
19
|
import { TurboWinstonLogger } from './logger.js';
|
20
|
-
import { ArweaveToken } from './token.js';
|
21
20
|
export const developmentPaymentServiceURL = 'https://payment.ardrive.dev';
|
22
21
|
export const defaultPaymentServiceURL = 'https://payment.ardrive.io';
|
23
22
|
export class TurboUnauthenticatedPaymentService {
|
@@ -130,14 +129,10 @@ export class TurboUnauthenticatedPaymentService {
|
|
130
129
|
}
|
131
130
|
// NOTE: to avoid redundancy, we use inheritance here - but generally prefer composition over inheritance
|
132
131
|
export class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymentService {
|
133
|
-
constructor({ url = defaultPaymentServiceURL, retryConfig, signer, logger = new TurboWinstonLogger(), token = 'arweave',
|
134
|
-
arweave: new ArweaveToken({
|
135
|
-
logger,
|
136
|
-
}),
|
137
|
-
}, }) {
|
132
|
+
constructor({ url = defaultPaymentServiceURL, retryConfig, signer, logger = new TurboWinstonLogger(), token = 'arweave', tokenTools, }) {
|
138
133
|
super({ url, retryConfig, logger, token });
|
139
134
|
this.signer = signer;
|
140
|
-
this.
|
135
|
+
this.tokenTools = tokenTools;
|
141
136
|
}
|
142
137
|
async getBalance() {
|
143
138
|
const headers = await this.signer.generateSignedRequestHeaders();
|
@@ -169,7 +164,7 @@ export class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymen
|
|
169
164
|
return walletAddress;
|
170
165
|
}
|
171
166
|
async topUpWithTokens({ feeMultiplier = 1, tokenAmount: tokenAmountV, }) {
|
172
|
-
if (!this.
|
167
|
+
if (!this.tokenTools) {
|
173
168
|
throw new Error(`Token type not supported for crypto fund ${this.token}`);
|
174
169
|
}
|
175
170
|
const tokenAmount = new BigNumber(tokenAmountV);
|
@@ -179,18 +174,16 @@ export class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymen
|
|
179
174
|
tokenAmount,
|
180
175
|
target,
|
181
176
|
});
|
182
|
-
const fundTx = await this.
|
177
|
+
const fundTx = await this.tokenTools.createAndSubmitTx({
|
183
178
|
target,
|
184
179
|
tokenAmount,
|
185
180
|
feeMultiplier,
|
186
181
|
signer: this.signer,
|
187
182
|
});
|
188
183
|
const txId = fundTx.id;
|
189
|
-
this.logger.debug('Submitting fund transaction...', { txId });
|
190
|
-
await this.tokenMap[this.token].submitTx({ tx: fundTx });
|
191
184
|
try {
|
192
185
|
// Let transaction settle some time
|
193
|
-
await this.
|
186
|
+
await this.tokenTools.pollForTxBeingAvailable({ txId });
|
194
187
|
return {
|
195
188
|
...(await this.submitFundTransaction({ txId })),
|
196
189
|
target: fundTx.target,
|
package/lib/esm/common/signer.js
CHANGED
@@ -16,6 +16,7 @@
|
|
16
16
|
*/
|
17
17
|
import { EthereumSigner, HexSolanaSigner } from 'arbundles';
|
18
18
|
import { randomBytes } from 'crypto';
|
19
|
+
import nacl from 'tweetnacl';
|
19
20
|
import { toB64Url } from '../utils/base64.js';
|
20
21
|
/**
|
21
22
|
* Abstract class for signing TurboDataItems.
|
@@ -59,6 +60,16 @@ export class TurboDataItemAbstractSigner {
|
|
59
60
|
return this.signer.publicKey;
|
60
61
|
}
|
61
62
|
async signData(dataToSign) {
|
63
|
+
if (this.signer instanceof HexSolanaSigner) {
|
64
|
+
const privateKey = this.signer.key;
|
65
|
+
const publicKey = Uint8Array.from(await this.getPublicKey());
|
66
|
+
// Concatenate the private and public keys correctly
|
67
|
+
const combinedKey = new Uint8Array(privateKey.length + publicKey.length);
|
68
|
+
combinedKey.set(privateKey);
|
69
|
+
combinedKey.set(publicKey, privateKey.length);
|
70
|
+
const signature = nacl.sign.detached(dataToSign, combinedKey);
|
71
|
+
return signature;
|
72
|
+
}
|
62
73
|
return this.signer.sign(dataToSign);
|
63
74
|
}
|
64
75
|
}
|
@@ -16,23 +16,23 @@
|
|
16
16
|
*/
|
17
17
|
import Arweave from '@irys/arweave';
|
18
18
|
import { BigNumber } from 'bignumber.js';
|
19
|
-
import { sha256B64Url, toB64Url } from '
|
20
|
-
import { sleep } from '
|
21
|
-
import { TurboWinstonLogger } from '
|
19
|
+
import { sha256B64Url, toB64Url } from '../../utils/base64.js';
|
20
|
+
import { sleep } from '../../utils/common.js';
|
21
|
+
import { TurboWinstonLogger } from '../logger.js';
|
22
22
|
export class ArweaveToken {
|
23
|
-
constructor({ arweave = Arweave.init({
|
24
|
-
url:
|
23
|
+
constructor({ gatewayUrl = 'https://arweave.net', arweave = Arweave.init({
|
24
|
+
url: gatewayUrl,
|
25
25
|
}), logger = new TurboWinstonLogger(), mintU = true, pollingOptions = {
|
26
26
|
maxAttempts: 10,
|
27
27
|
pollingIntervalMs: 3000,
|
28
28
|
initialBackoffMs: 7000,
|
29
|
-
}, }) {
|
29
|
+
}, } = {}) {
|
30
30
|
this.arweave = arweave;
|
31
31
|
this.logger = logger;
|
32
32
|
this.mintU = mintU;
|
33
33
|
this.pollingOptions = pollingOptions;
|
34
34
|
}
|
35
|
-
async
|
35
|
+
async createAndSubmitTx({ feeMultiplier, target, tokenAmount, signer, }) {
|
36
36
|
const tx = await this.arweave.createTransaction({
|
37
37
|
target,
|
38
38
|
quantity: tokenAmount.toString(),
|
@@ -60,7 +60,9 @@ export class ArweaveToken {
|
|
60
60
|
owner: publicKeyB64Url,
|
61
61
|
signature: toB64Url(signatureBuffer),
|
62
62
|
});
|
63
|
-
|
63
|
+
this.logger.debug('Submitting fund transaction...', { id });
|
64
|
+
await this.submitTx(tx);
|
65
|
+
return { id, target, reward: tx.reward };
|
64
66
|
}
|
65
67
|
async pollForTxBeingAvailable({ txId, }) {
|
66
68
|
const { maxAttempts, pollingIntervalMs, initialBackoffMs } = this.pollingOptions;
|
@@ -105,12 +107,13 @@ export class ArweaveToken {
|
|
105
107
|
}
|
106
108
|
throw new Error('Transaction not found after polling, transaction id: ' + txId);
|
107
109
|
}
|
108
|
-
|
110
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
111
|
+
async submitTx(tx) {
|
109
112
|
try {
|
110
113
|
const response = await this.arweave.transactions.post(tx);
|
111
114
|
if (response.status !== 200) {
|
112
115
|
throw new Error('Failed to post transaction -- ' +
|
113
|
-
`Status ${response.status}, ${response.statusText}`);
|
116
|
+
`Status ${response.status}, ${response.statusText}, ${response.data}`);
|
114
117
|
}
|
115
118
|
this.logger.debug('Successfully posted fund transaction...', { tx });
|
116
119
|
}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { ArweaveToken } from './arweave.js';
|
2
|
+
import { SolanaToken } from './solana.js';
|
3
|
+
export const defaultTokenMap = {
|
4
|
+
arweave: (config) => new ArweaveToken(config),
|
5
|
+
solana: (config) => new SolanaToken(config),
|
6
|
+
// ethereum: (config: TokenConfig) => new EthereumToken(config)
|
7
|
+
};
|
8
|
+
export * from './arweave.js';
|
9
|
+
export * from './solana.js';
|
@@ -0,0 +1,103 @@
|
|
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 { Connection, PublicKey, SystemProgram, Transaction, } from '@solana/web3.js';
|
18
|
+
import { BigNumber } from 'bignumber.js';
|
19
|
+
import bs58 from 'bs58';
|
20
|
+
import { sleep } from '../../utils/common.js';
|
21
|
+
import { TurboWinstonLogger } from '../logger.js';
|
22
|
+
export const lamportToTokenAmount = (winston) => winston;
|
23
|
+
export const SOLToTokenAmount = (sol) => new BigNumber(sol).times(1e9).valueOf();
|
24
|
+
export class SolanaToken {
|
25
|
+
constructor({ logger = new TurboWinstonLogger(), gatewayUrl = 'https://api.mainnet-beta.solana.com', pollingOptions = {
|
26
|
+
maxAttempts: 10,
|
27
|
+
pollingIntervalMs: 5000,
|
28
|
+
initialBackoffMs: 7000,
|
29
|
+
}, } = {}) {
|
30
|
+
this.logger = logger;
|
31
|
+
this.gatewayUrl = gatewayUrl;
|
32
|
+
this.connection = new Connection(gatewayUrl, 'confirmed');
|
33
|
+
this.pollingOptions = pollingOptions;
|
34
|
+
}
|
35
|
+
async createAndSubmitTx({ target, tokenAmount, signer, }) {
|
36
|
+
const publicKey = new PublicKey(bs58.encode(await signer.getPublicKey()));
|
37
|
+
const tx = new Transaction({
|
38
|
+
feePayer: publicKey,
|
39
|
+
...(await this.connection.getLatestBlockhash()),
|
40
|
+
});
|
41
|
+
tx.add(SystemProgram.transfer({
|
42
|
+
fromPubkey: publicKey,
|
43
|
+
toPubkey: new PublicKey(target),
|
44
|
+
lamports: +new BigNumber(tokenAmount),
|
45
|
+
}));
|
46
|
+
const serializedTx = tx.serializeMessage();
|
47
|
+
const signature = await signer.signData(serializedTx);
|
48
|
+
tx.addSignature(publicKey, Buffer.from(signature));
|
49
|
+
const id = bs58.encode(signature);
|
50
|
+
await this.submitTx(tx, id);
|
51
|
+
return { id, target };
|
52
|
+
}
|
53
|
+
async submitTx(tx, id) {
|
54
|
+
this.logger.debug('Submitting fund transaction...', { id });
|
55
|
+
await this.connection.sendRawTransaction(tx.serialize(), {
|
56
|
+
maxRetries: this.pollingOptions.maxAttempts,
|
57
|
+
});
|
58
|
+
if (tx.recentBlockhash === undefined ||
|
59
|
+
tx.lastValidBlockHeight === undefined) {
|
60
|
+
throw new Error('Failed to submit Transaction -- missing blockhash or lastValidBlockHeight from transaction creation. Solana Gateway Url:' +
|
61
|
+
this.gatewayUrl);
|
62
|
+
}
|
63
|
+
await this.connection.confirmTransaction({
|
64
|
+
signature: id,
|
65
|
+
blockhash: tx.recentBlockhash,
|
66
|
+
lastValidBlockHeight: tx.lastValidBlockHeight,
|
67
|
+
}, 'finalized');
|
68
|
+
}
|
69
|
+
async pollForTxBeingAvailable({ txId, }) {
|
70
|
+
const { maxAttempts, pollingIntervalMs, initialBackoffMs } = this.pollingOptions;
|
71
|
+
this.logger.debug('Polling for transaction...', {
|
72
|
+
txId,
|
73
|
+
pollingOptions: this.pollingOptions,
|
74
|
+
});
|
75
|
+
await sleep(initialBackoffMs);
|
76
|
+
let attempts = 0;
|
77
|
+
while (attempts < maxAttempts) {
|
78
|
+
let status = undefined;
|
79
|
+
attempts++;
|
80
|
+
try {
|
81
|
+
status = await this.connection.getSignatureStatus(txId);
|
82
|
+
}
|
83
|
+
catch (err) {
|
84
|
+
// Continue retries when request errors
|
85
|
+
this.logger.debug('Failed to poll for transaction...', { err });
|
86
|
+
}
|
87
|
+
if (status && status.value && status.value.err !== null) {
|
88
|
+
throw new Error(`Transaction failed: ${status.value.err}`);
|
89
|
+
}
|
90
|
+
if (status && status.value && status.value.slot !== null) {
|
91
|
+
return;
|
92
|
+
}
|
93
|
+
this.logger.debug('Transaction not found, polling...', {
|
94
|
+
txId,
|
95
|
+
attempts,
|
96
|
+
maxAttempts,
|
97
|
+
pollingIntervalMs,
|
98
|
+
});
|
99
|
+
await sleep(pollingIntervalMs);
|
100
|
+
}
|
101
|
+
throw new Error('Transaction not found after polling, transaction id: ' + txId);
|
102
|
+
}
|
103
|
+
}
|
package/lib/esm/node/factory.js
CHANGED
@@ -14,55 +14,6 @@
|
|
14
14
|
* You should have received a copy of the GNU Affero General Public License
|
15
15
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
16
|
*/
|
17
|
-
import { ArweaveSigner, EthereumSigner, HexSolanaSigner } from 'arbundles';
|
18
17
|
import { TurboBaseFactory } from '../common/factory.js';
|
19
|
-
import { TurboAuthenticatedClient, TurboAuthenticatedPaymentService, TurboAuthenticatedUploadService, } from '../common/index.js';
|
20
|
-
import { TurboWebArweaveSigner } from '../web/signer.js';
|
21
|
-
import { TurboNodeSigner } from './signer.js';
|
22
18
|
export class TurboFactory extends TurboBaseFactory {
|
23
|
-
static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, tokenMap, token, }) {
|
24
|
-
let signer;
|
25
|
-
if (providedSigner) {
|
26
|
-
signer = providedSigner;
|
27
|
-
if (!token) {
|
28
|
-
if (signer instanceof EthereumSigner) {
|
29
|
-
token = 'ethereum';
|
30
|
-
}
|
31
|
-
else if (signer instanceof HexSolanaSigner) {
|
32
|
-
token = 'solana';
|
33
|
-
}
|
34
|
-
}
|
35
|
-
}
|
36
|
-
else if (privateKey) {
|
37
|
-
signer = new ArweaveSigner(privateKey);
|
38
|
-
}
|
39
|
-
else {
|
40
|
-
throw new Error('A privateKey or signer must be provided.');
|
41
|
-
}
|
42
|
-
// when in browser, we use TurboWebArweaveSigner
|
43
|
-
const turboSigner = typeof window !== 'undefined'
|
44
|
-
? new TurboWebArweaveSigner({
|
45
|
-
signer,
|
46
|
-
logger: this.logger,
|
47
|
-
})
|
48
|
-
: new TurboNodeSigner({
|
49
|
-
signer,
|
50
|
-
logger: this.logger,
|
51
|
-
});
|
52
|
-
const paymentService = new TurboAuthenticatedPaymentService({
|
53
|
-
...paymentServiceConfig,
|
54
|
-
signer: turboSigner,
|
55
|
-
logger: this.logger,
|
56
|
-
tokenMap,
|
57
|
-
});
|
58
|
-
const uploadService = new TurboAuthenticatedUploadService({
|
59
|
-
...uploadServiceConfig,
|
60
|
-
signer: turboSigner,
|
61
|
-
logger: this.logger,
|
62
|
-
});
|
63
|
-
return new TurboAuthenticatedClient({
|
64
|
-
uploadService,
|
65
|
-
paymentService,
|
66
|
-
});
|
67
|
-
}
|
68
19
|
}
|
package/lib/esm/types.js
CHANGED
@@ -1,3 +1,4 @@
|
|
1
1
|
// TODO: Remove this var and Allow all tokens when crypto fund implemented for each PE-5993, PE-5992
|
2
2
|
export const allowedFiatTokens = ['arweave', 'solana', 'ethereum'];
|
3
|
-
export const tokenTypes = ['arweave'
|
3
|
+
export const tokenTypes = ['arweave', 'solana' /* 'ethereum'*/];
|
4
|
+
export const isJWK = (wallet) => wallet.kty !== undefined;
|
package/lib/esm/version.js
CHANGED
package/lib/esm/web/factory.js
CHANGED
@@ -16,43 +16,76 @@
|
|
16
16
|
*/
|
17
17
|
import { ArweaveSigner, EthereumSigner, HexSolanaSigner } from 'arbundles';
|
18
18
|
import { TurboBaseFactory } from '../common/factory.js';
|
19
|
-
import { TurboAuthenticatedClient, TurboAuthenticatedPaymentService, TurboAuthenticatedUploadService, } from '../common/index.js';
|
19
|
+
import { TurboAuthenticatedClient, TurboAuthenticatedPaymentService, TurboAuthenticatedUploadService, defaultTokenMap, } from '../common/index.js';
|
20
|
+
import { isJWK, } from '../types.js';
|
20
21
|
import { TurboWebArweaveSigner } from './signer.js';
|
21
22
|
export class TurboFactory extends TurboBaseFactory {
|
22
|
-
static
|
23
|
+
static getSigner(providedSigner, providedPrivateKey, token) {
|
23
24
|
let signer;
|
24
|
-
if (providedSigner) {
|
25
|
+
if (providedSigner !== undefined) {
|
25
26
|
signer = providedSigner;
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
else if (
|
31
|
-
|
27
|
+
}
|
28
|
+
else if (providedPrivateKey !== undefined) {
|
29
|
+
if (token === 'solana') {
|
30
|
+
signer = new HexSolanaSigner(providedPrivateKey);
|
31
|
+
// TODO: else if (token === 'ethereum') {signer = new EthereumSigner(providedPrivateKey);}
|
32
|
+
}
|
33
|
+
else {
|
34
|
+
if (!isJWK(providedPrivateKey)) {
|
35
|
+
throw new Error('A JWK must be provided for ArweaveSigner.');
|
32
36
|
}
|
37
|
+
signer = new ArweaveSigner(providedPrivateKey);
|
33
38
|
}
|
34
39
|
}
|
35
|
-
else if (privateKey) {
|
36
|
-
signer = new ArweaveSigner(privateKey);
|
37
|
-
}
|
38
40
|
else {
|
39
41
|
throw new Error('A privateKey or signer must be provided.');
|
40
42
|
}
|
41
|
-
|
43
|
+
return new TurboWebArweaveSigner({
|
42
44
|
signer,
|
43
45
|
logger: this.logger,
|
44
46
|
});
|
47
|
+
}
|
48
|
+
static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig = {}, uploadServiceConfig = {}, token, gatewayUrl, tokenMap, tokenTools, }) {
|
49
|
+
if (!token) {
|
50
|
+
if (providedSigner) {
|
51
|
+
// Derive token from signer if not provided
|
52
|
+
if (providedSigner instanceof EthereumSigner) {
|
53
|
+
token = 'ethereum';
|
54
|
+
}
|
55
|
+
else if (providedSigner instanceof HexSolanaSigner) {
|
56
|
+
token = 'solana';
|
57
|
+
}
|
58
|
+
else {
|
59
|
+
token = 'arweave';
|
60
|
+
}
|
61
|
+
}
|
62
|
+
else {
|
63
|
+
token = 'arweave';
|
64
|
+
}
|
65
|
+
}
|
66
|
+
const turboSigner = this.getSigner(providedSigner, privateKey, token);
|
67
|
+
token ??= 'arweave'; // default to arweave if token is not provided
|
68
|
+
if (!tokenTools) {
|
69
|
+
if (tokenMap && token === 'arweave') {
|
70
|
+
tokenTools = tokenMap.arweave;
|
71
|
+
}
|
72
|
+
tokenTools = defaultTokenMap[token]?.({
|
73
|
+
gatewayUrl,
|
74
|
+
logger: this.logger,
|
75
|
+
});
|
76
|
+
}
|
45
77
|
const paymentService = new TurboAuthenticatedPaymentService({
|
46
78
|
...paymentServiceConfig,
|
47
79
|
signer: turboSigner,
|
48
80
|
logger: this.logger,
|
49
|
-
tokenMap,
|
50
81
|
token,
|
82
|
+
tokenTools,
|
51
83
|
});
|
52
84
|
const uploadService = new TurboAuthenticatedUploadService({
|
53
85
|
...uploadServiceConfig,
|
54
86
|
signer: turboSigner,
|
55
87
|
logger: this.logger,
|
88
|
+
token,
|
56
89
|
});
|
57
90
|
return new TurboAuthenticatedClient({
|
58
91
|
uploadService,
|
@@ -1,26 +1,13 @@
|
|
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 { TurboUnauthenticatedConfiguration } from '../types.js';
|
1
|
+
import { CreditableTokenType, TurboAuthenticatedConfiguration, TurboSigner, TurboUnauthenticatedConfiguration, TurboWallet } from '../types.js';
|
18
2
|
import { TurboWinstonLogger } from './logger.js';
|
19
|
-
import {
|
3
|
+
import { TurboDataItemAbstractSigner } from './signer.js';
|
4
|
+
import { TurboAuthenticatedClient, TurboUnauthenticatedClient } from './turbo.js';
|
20
5
|
export declare class TurboBaseFactory {
|
21
6
|
protected static logger: TurboWinstonLogger;
|
22
7
|
static setLogLevel(level: string): void;
|
23
8
|
static setLogFormat(format: string): void;
|
24
9
|
static unauthenticated({ paymentServiceConfig, uploadServiceConfig, }?: TurboUnauthenticatedConfiguration): TurboUnauthenticatedClient;
|
10
|
+
protected static getSigner(providedSigner: TurboSigner | undefined, providedPrivateKey: TurboWallet | undefined, token: CreditableTokenType): TurboDataItemAbstractSigner;
|
11
|
+
static authenticated({ privateKey, signer: providedSigner, paymentServiceConfig, uploadServiceConfig, token, tokenMap, gatewayUrl, tokenTools, }: TurboAuthenticatedConfiguration): TurboAuthenticatedClient;
|
25
12
|
}
|
26
13
|
//# sourceMappingURL=factory.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../../src/common/factory.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../../src/common/factory.ts"],"names":[],"mappings":"AAmBA,OAAO,EACL,mBAAmB,EACnB,+BAA+B,EAC/B,WAAW,EACX,iCAAiC,EACjC,WAAW,EAEZ,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAKjD,OAAO,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC;AAE1D,OAAO,EACL,wBAAwB,EACxB,0BAA0B,EAC3B,MAAM,YAAY,CAAC;AAMpB,qBAAa,gBAAgB;IAC3B,SAAS,CAAC,MAAM,CAAC,MAAM,qBAA4B;IAEnD,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,MAAM;IAIhC,MAAM,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM;IAIlC,MAAM,CAAC,eAAe,CAAC,EACrB,oBAAyB,EACzB,mBAAwB,GACzB,GAAE,iCAAsC;IAezC,SAAS,CAAC,MAAM,CAAC,SAAS,CACxB,cAAc,EAAE,WAAW,GAAG,SAAS,EACvC,kBAAkB,EAAE,WAAW,GAAG,SAAS,EAC3C,KAAK,EAAE,mBAAmB,GACzB,2BAA2B;IA+B9B,MAAM,CAAC,aAAa,CAAC,EACnB,UAAU,EACV,MAAM,EAAE,cAAc,EACtB,oBAAyB,EACzB,mBAAwB,EACxB,KAAK,EACL,QAAQ,EACR,UAAU,EACV,UAAU,GACX,EAAE,+BAA+B;CA8CnC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/common/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/common/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC"}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { CreditableTokenType, Currency,
|
1
|
+
import { CreditableTokenType, Currency, TokenTools, TurboAuthenticatedPaymentServiceConfiguration, TurboAuthenticatedPaymentServiceInterface, TurboBalanceResponse, TurboCheckoutSessionParams, TurboCheckoutSessionResponse, TurboCountriesResponse, TurboCryptoFundResponse, TurboCurrenciesResponse, TurboDataItemSigner, TurboFiatToArResponse, TurboFundWithTokensParams, TurboLogger, TurboPriceResponse, TurboRatesResponse, TurboSignedRequestHeaders, TurboSubmitFundTxResponse, TurboUnauthenticatedPaymentServiceConfiguration, TurboUnauthenticatedPaymentServiceInterface, TurboWincForFiatParams, TurboWincForFiatResponse } from '../types.js';
|
2
2
|
import { TurboHTTPService } from './http.js';
|
3
3
|
export declare const developmentPaymentServiceURL = "https://payment.ardrive.dev";
|
4
4
|
export declare const defaultPaymentServiceURL = "https://payment.ardrive.io";
|
@@ -26,8 +26,8 @@ export declare class TurboUnauthenticatedPaymentService implements TurboUnauthen
|
|
26
26
|
}
|
27
27
|
export declare class TurboAuthenticatedPaymentService extends TurboUnauthenticatedPaymentService implements TurboAuthenticatedPaymentServiceInterface {
|
28
28
|
protected readonly signer: TurboDataItemSigner;
|
29
|
-
protected readonly
|
30
|
-
constructor({ url, retryConfig, signer, logger, token,
|
29
|
+
protected readonly tokenTools: TokenTools | undefined;
|
30
|
+
constructor({ url, retryConfig, signer, logger, token, tokenTools, }: TurboAuthenticatedPaymentServiceConfiguration);
|
31
31
|
getBalance(): Promise<TurboBalanceResponse>;
|
32
32
|
getWincForFiat({ amount, promoCodes, }: TurboWincForFiatParams): Promise<TurboWincForFiatResponse>;
|
33
33
|
createCheckoutSession(params: TurboCheckoutSessionParams): Promise<TurboCheckoutSessionResponse>;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"payment.d.ts","sourceRoot":"","sources":["../../../src/common/payment.ts"],"names":[],"mappings":"AAkBA,OAAO,EACL,mBAAmB,EACnB,QAAQ,EACR,
|
1
|
+
{"version":3,"file":"payment.d.ts","sourceRoot":"","sources":["../../../src/common/payment.ts"],"names":[],"mappings":"AAkBA,OAAO,EACL,mBAAmB,EACnB,QAAQ,EACR,UAAU,EAEV,6CAA6C,EAC7C,yCAAyC,EACzC,oBAAoB,EACpB,0BAA0B,EAC1B,4BAA4B,EAC5B,sBAAsB,EACtB,uBAAuB,EACvB,uBAAuB,EACvB,mBAAmB,EACnB,qBAAqB,EACrB,yBAAyB,EAEzB,WAAW,EAEX,kBAAkB,EAClB,kBAAkB,EAClB,yBAAyB,EACzB,yBAAyB,EACzB,+CAA+C,EAC/C,2CAA2C,EAC3C,sBAAsB,EACtB,wBAAwB,EACzB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAG7C,eAAO,MAAM,4BAA4B,gCAAgC,CAAC;AAC1E,eAAO,MAAM,wBAAwB,+BAA+B,CAAC;AAErE,qBAAa,kCACX,YAAW,2CAA2C;IAEtD,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,gBAAgB,CAAC;IACjD,SAAS,CAAC,MAAM,EAAE,WAAW,CAAC;IAC9B,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAAC;gBAElC,EACV,GAA8B,EAC9B,WAAW,EACX,MAAiC,EACjC,KAAiB,GAClB,EAAE,+CAA+C;IAU3C,YAAY,IAAI,OAAO,CAAC,kBAAkB,CAAC;IAM3C,WAAW,CAAC,EACjB,QAAQ,GACT,EAAE;QACD,QAAQ,EAAE,QAAQ,CAAC;KACpB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAM3B,qBAAqB,IAAI,OAAO,CAAC,sBAAsB,CAAC;IAMxD,sBAAsB,IAAI,OAAO,CAAC,uBAAuB,CAAC;IAMpD,cAAc,CAAC,EAC1B,KAAK,GACN,EAAE;QACD,KAAK,EAAE,MAAM,EAAE,CAAC;KACjB,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC;IAW1B,cAAc,CAAC,EACpB,MAAM,GACP,EAAE,sBAAsB,GAAG,OAAO,CAAC,wBAAwB,CAAC;IAO7D,SAAS,CAAC,uBAAuB,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,MAAM;cAK/C,WAAW,CACzB,EACE,MAAM,EACN,KAAK,EACL,UAAe,EACf,MAAiB,GAClB,EAAE,0BAA0B,EAC7B,OAAO,CAAC,EAAE,yBAAyB,GAClC,OAAO,CAAC,4BAA4B,CAAC;IA0BjC,qBAAqB,CAC1B,MAAM,EAAE,0BAA0B,GACjC,OAAO,CAAC,4BAA4B,CAAC;IAI3B,qBAAqB,CAAC,EACjC,IAAI,GACL,EAAE;QACD,IAAI,EAAE,MAAM,CAAC;KACd,GAAG,OAAO,CAAC,yBAAyB,CAAC;CAqCvC;AAED,qBAAa,gCACX,SAAQ,kCACR,YAAW,yCAAyC;IAEpD,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,mBAAmB,CAAC;IAC/C,SAAS,CAAC,QAAQ,CAAC,UAAU,EAAE,UAAU,GAAG,SAAS,CAAC;gBAE1C,EACV,GAA8B,EAC9B,WAAW,EACX,MAAM,EACN,MAAiC,EACjC,KAAiB,EACjB,UAAU,GACX,EAAE,6CAA6C;IAMnC,UAAU,IAAI,OAAO,CAAC,oBAAoB,CAAC;IAY3C,cAAc,CAAC,EAC1B,MAAM,EACN,UAAe,GAChB,EAAE,sBAAsB,GAAG,OAAO,CAAC,wBAAwB,CAAC;IAShD,qBAAqB,CAChC,MAAM,EAAE,0BAA0B,GACjC,OAAO,CAAC,4BAA4B,CAAC;YAO1B,sBAAsB;IAYvB,eAAe,CAAC,EAC3B,aAAiB,EACjB,WAAW,EAAE,YAAY,GAC1B,EAAE,yBAAyB,GAAG,OAAO,CAAC,uBAAuB,CAAC;CAwChE"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../../../src/common/signer.ts"],"names":[],"mappings":";
|
1
|
+
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../../../src/common/signer.ts"],"names":[],"mappings":";AAoBA,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,yBAAyB,EACzB,gBAAgB,EAChB,WAAW,EACX,0BAA0B,EAC1B,WAAW,EACZ,MAAM,aAAa,CAAC;AAGrB;;GAEG;AACH,8BAAsB,2BACpB,YAAW,mBAAmB;IAE9B,QAAQ,CAAC,YAAY,CAAC,EACpB,iBAAiB,EACjB,eAAe,EACf,YAAY,GACb,EAAE,gBAAgB,CAAC,iBAAiB,CAAC,GAAG,OAAO,CAAC,0BAA0B,CAAC;IAE5E,SAAS,CAAC,MAAM,EAAE,WAAW,CAAC;IAC9B,SAAS,CAAC,MAAM,EAAE,WAAW,CAAC;gBAElB,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,yBAAyB;IAKzD,SAAS,KAAK,SAAS,IAAI;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,eAAe,EAAE,MAAM,CAAA;KAAE,CAmB1E;IAEY,4BAA4B;;;;;IAY5B,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC;IAI/B,QAAQ,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;CAgBnE"}
|