Skip to content

Commit

Permalink
feat(upload): allow eth and sol signing for upload PE-5941
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Apr 24, 2024
1 parent 15d3056 commit bfcdc4b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
22 changes: 22 additions & 0 deletions src/common/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { EthereumSigner, HexSolanaSigner } from 'arbundles';
import { randomBytes } from 'crypto';

import {
Expand Down Expand Up @@ -47,6 +48,27 @@ export abstract class TurboDataItemAbstractSigner
this.signer = signer;
}

protected get sigConfig(): { ownerLength: number; signatureLength: number } {
if (this.signer instanceof EthereumSigner) {
return {
signatureLength: 65,
ownerLength: 65,
};
}
if (this.signer instanceof HexSolanaSigner) {
return {
signatureLength: 64,
ownerLength: 32,
};
}

// base case, arweave/arconnect signer
return {
ownerLength: 512,
signatureLength: 512,
};
}

public async generateSignedRequestHeaders() {
const nonce = randomBytes(16).toString('hex');
const buffer = Buffer.from(nonce);
Expand Down
11 changes: 8 additions & 3 deletions src/common/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import {
CreditableTokenType,
TurboAbortSignal,
TurboAuthenticatedUploadServiceConfiguration,
TurboAuthenticatedUploadServiceInterface,
Expand All @@ -37,12 +38,15 @@ export class TurboUnauthenticatedUploadService
{
protected httpService: TurboHTTPService;
protected logger: TurboLogger;
protected token: CreditableTokenType;

constructor({
url = defaultUploadServiceURL,
retryConfig,
logger = new TurboWinstonLogger(),
token = 'arweave',
}: TurboUnauthenticatedUploadServiceConfiguration) {
this.token = token;
this.logger = logger;
this.httpService = new TurboHTTPService({
url: `${url}/v1`,
Expand All @@ -61,7 +65,7 @@ export class TurboUnauthenticatedUploadService
this.logger.debug('Uploading signed data item...');
// TODO: add p-limit constraint or replace with separate upload class
return this.httpService.post<TurboUploadDataItemResponse>({
endpoint: `/tx`,
endpoint: `/tx/${this.token}`,
signal,
data: dataItemStreamFactory(),
headers: {
Expand All @@ -84,8 +88,9 @@ export class TurboAuthenticatedUploadService
retryConfig,
signer,
logger,
token,
}: TurboAuthenticatedUploadServiceConfiguration) {
super({ url, retryConfig, logger });
super({ url, retryConfig, logger, token });
this.signer = signer;
}

Expand All @@ -107,7 +112,7 @@ export class TurboAuthenticatedUploadService
this.logger.debug('Uploading signed data item...');
// TODO: add p-limit constraint or replace with separate upload class
return this.httpService.post<TurboUploadDataItemResponse>({
endpoint: `/tx`,
endpoint: `/tx/${this.token}`,
signal,
data: signedDataItem,
headers: {
Expand Down
14 changes: 11 additions & 3 deletions src/node/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ArweaveSigner } from 'arbundles';
import { ArweaveSigner, EthereumSigner, HexSolanaSigner } from 'arbundles';

import { TurboBaseFactory } from '../common/factory.js';
import {
Expand All @@ -23,7 +23,7 @@ import {
TurboAuthenticatedUploadService,
} from '../common/index.js';
import { TurboAuthenticatedConfiguration, TurboSigner } from '../types.js';
import { TurboNodeArweaveSigner } from './signer.js';
import { TurboNodeSigner } from './signer.js';

export class TurboFactory extends TurboBaseFactory {
static authenticated({
Expand All @@ -32,18 +32,26 @@ export class TurboFactory extends TurboBaseFactory {
paymentServiceConfig = {},
uploadServiceConfig = {},
tokenMap,
token,
}: TurboAuthenticatedConfiguration) {
let signer: TurboSigner;

if (providedSigner) {
signer = providedSigner;
if (!token) {
if (signer instanceof EthereumSigner) {
token = 'ethereum';
} else if (signer instanceof HexSolanaSigner) {
token = 'solana';
}
}
} else if (privateKey) {
signer = new ArweaveSigner(privateKey);
} else {
throw new Error('A privateKey or signer must be provided.');
}

const turboSigner = new TurboNodeArweaveSigner({
const turboSigner = new TurboNodeSigner({
signer,
logger: this.logger,
});
Expand Down
14 changes: 9 additions & 5 deletions src/node/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
import {
ArconnectSigner,
ArweaveSigner,
EthereumSigner,
HexSolanaSigner,
serializeTags,
streamSigner,
} from 'arbundles';
import { Readable } from 'node:stream';

import { TurboDataItemAbstractSigner } from '../common/signer.js';
import {
CreditableTokenType,
DataItemOptions,
StreamSizeFactory,
TurboDataItemSignerParams,
Expand All @@ -33,12 +36,14 @@ import { fromB64Url } from '../utils/base64.js';
/**
* Utility exports to avoid clients having to install arbundles
*/
export { ArconnectSigner, ArweaveSigner };
export { ArconnectSigner, ArweaveSigner, EthereumSigner, HexSolanaSigner };

/**
* Node implementation of TurboDataItemSigner.
*/
export class TurboNodeArweaveSigner extends TurboDataItemAbstractSigner {
export class TurboNodeSigner extends TurboDataItemAbstractSigner {
token: CreditableTokenType;

constructor(p: TurboDataItemSignerParams) {
super(p);
}
Expand Down Expand Up @@ -98,9 +103,8 @@ export class TurboNodeArweaveSigner extends TurboDataItemAbstractSigner {
const serializedTags = tags && tags.length > 0 ? serializeTags(tags) : null;
const tagsLength = 16 + (serializedTags ? serializedTags.byteLength : 0);

// Arweave sig and owner length is 512 bytes
const signatureLength = 512;
const ownerLength = 512;
const { ownerLength, signatureLength } = this.sigConfig;

const signatureTypeLength = 2;

return [
Expand Down
9 changes: 8 additions & 1 deletion src/web/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ArweaveSigner } from 'arbundles';
import { ArweaveSigner, EthereumSigner, HexSolanaSigner } from 'arbundles';

import { TurboBaseFactory } from '../common/factory.js';
import {
Expand All @@ -38,6 +38,13 @@ export class TurboFactory extends TurboBaseFactory {

if (providedSigner) {
signer = providedSigner;
if (!token) {
if (signer instanceof EthereumSigner) {
token = 'ethereum';
} else if (signer instanceof HexSolanaSigner) {
token = 'solana';
}
}
} else if (privateKey) {
signer = new ArweaveSigner(privateKey);
} else {
Expand Down
10 changes: 8 additions & 2 deletions src/web/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ArconnectSigner, ArweaveSigner, createData } from 'arbundles';
import {
ArconnectSigner,
ArweaveSigner,
EthereumSigner,
HexSolanaSigner,
createData,
} from 'arbundles';

import { TurboDataItemAbstractSigner } from '../common/signer.js';
import {
Expand All @@ -28,7 +34,7 @@ import { readableStreamToBuffer } from '../utils/readableStream.js';
/**
* Utility exports to avoid clients having to install arbundles
*/
export { ArconnectSigner, ArweaveSigner };
export { ArconnectSigner, ArweaveSigner, EthereumSigner, HexSolanaSigner };

/**
* Web implementation of TurboDataItemSigner.
Expand Down

0 comments on commit bfcdc4b

Please sign in to comment.