Skip to content

Commit

Permalink
Merge pull request #61 from Akord-com/import-bundled-tx
Browse files Browse the repository at this point in the history
feat: support importing bundled txs
  • Loading branch information
wkolod authored Feb 6, 2023
2 parents 34c2994 + 6218d3a commit 16a95e9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 28 deletions.
71 changes: 71 additions & 0 deletions src/arweave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import axios from "axios";

const ARWEAVE_URL = "https://arweave.net/";

const getTxData = async (id: string) => {
const response = await fetch(ARWEAVE_URL + id);
if (response.status == 200 || response.status == 202) {
const body = await response.arrayBuffer();
return body;
} else {
throw new Error("Cannot fetch arweave transaction data: " + id);
}
};

const getTxMetadata = async (id: string) => {
try {
const result = await graphql(getTransaction, { id });
const txMetadata = result?.data?.transactions?.edges[0]?.node;
if (!txMetadata) {
throw new Error("Cannot fetch arweave transaction metadata: " + id);
}
return txMetadata;
} catch (error) {
throw new Error("Cannot fetch arweave transaction metadata: " + id);
}
};

const getTransaction = /* GraphQL */ `
query transactionsById($id: ID!) {
transactions(ids:[$id]) {
edges {
node {
id
owner {
address
}
data {
type
size
}
tags {
name
value
}
}
}
}
}
`;

const graphql = async (query: any, variables: any) => {
try {
const config = {
url: ARWEAVE_URL + "graphql",
method: <any>'post',
headers: {
'content-type': 'application/json'
},
data: JSON.stringify({ query, variables }),
};
const response = await axios(config);
return response.data;
} catch (error) {
throw new Error("Error while trying to make fetch request");
}
}

export {
getTxData,
getTxMetadata
}
32 changes: 4 additions & 28 deletions src/core/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Blob } from 'buffer';
import fs from "fs";
import { Tag, Tags } from "../types/contract";
import { BinaryLike } from "crypto";
import { getTxData, getTxMetadata } from "../arweave";

class FileService extends Service {
asyncUploadTreshold = 209715200;
Expand Down Expand Up @@ -118,10 +119,6 @@ class FileService extends Service {
}
}

base64ToString(base64: string) {
return Buffer.from(base64, "base64").toString();
};

public async import(
fileTxId: string,
name: string)
Expand All @@ -130,10 +127,9 @@ class FileService extends Service {
if (this.isPublic) {
tags.push(new Tag(fileTags.FILE_NAME, encodeURIComponent(name)))
}
const fileData = await this.downloadFile(fileTxId);
const fileMetadata = await this.downloadFileMetadata(fileTxId);
const contentTypeTag = fileMetadata.tags.find((tag: Tag) => this.base64ToString(tag.name) === "Content-Type");
const fileType = this.base64ToString(contentTypeTag.value);
const fileData = await getTxData(fileTxId);
const fileMetadata = await getTxMetadata(fileTxId);
const fileType = (fileMetadata.tags?.find((tag: Tag) => tag.name === "Content-Type"))?.value;
const file = await createFileLike([fileData], name, fileType);
tags.push(new Tag(smartweaveTags.CONTENT_TYPE, file.type));
tags.push(new Tag(fileTags.FILE_SIZE, file.size));
Expand All @@ -156,26 +152,6 @@ class FileService extends Service {
return { file, resourceHash, resourceUrl: resource.resourceUrl };
}

private async downloadFile(id: string) {
const response = await fetch("https://arweave.net/" + id);
if (response.status == 200 || response.status == 202) {
const body = await response.arrayBuffer();
return body;
} else {
throw new Error("Cannot fetch arweave transaction data: " + id);
}
};

private async downloadFileMetadata(id: string) {
const response = await fetch("https://arweave.net/tx/" + id);
if (response.status == 200 || response.status == 202) {
const body = await response.json();
return body;
} else {
throw new Error("Cannot fetch arweave transaction metadata: " + id);
}
};

public async stream(path: string, size?: number): Promise<fs.WriteStream | WritableStreamDefaultWriter> {
if (typeof window === 'undefined') {
return fs.createWriteStream(path);
Expand Down

0 comments on commit 16a95e9

Please sign in to comment.