Skip to content

Commit

Permalink
Merge pull request #130 from VWBL/release-0.1.13
Browse files Browse the repository at this point in the history
Release 0.1.13
  • Loading branch information
Huzitatuguharu authored Feb 29, 2024
2 parents ce4ef7f + ba5e6be commit 4aea878
Show file tree
Hide file tree
Showing 33 changed files with 3,971 additions and 1,893 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ module.exports = {
"@typescript-eslint/no-unsafe-member-access": "warn",
"@typescript-eslint/no-unsafe-call": "warn",
"@typescript-eslint/no-unsafe-return": "warn",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-unused-vars": "warn",
"@typescript-eslint/await-thenable": "warn"
},
};
9 changes: 6 additions & 3 deletions .github/workflows/check-can-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
env:
# Check available versions below
# https://github.com/actions/node-versions/blob/main/versions-manifest.json
node-version: "16.x"
node-version: "20.x"

jobs:
check-can-publish:
Expand All @@ -26,5 +26,8 @@ jobs:
registry-url: "https://registry.npmjs.org"
always-auth: true

- name: npx can-npm-publish
run: npx can-npm-publish
- name: Check if package version can be published
uses: technote-space/package-version-check-action@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PACKAGE_MANAGER: yarn
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

strategy:
matrix:
node-version: [16.x]
node-version: [20.x]

steps:
- name: Checkout
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
env:
# Check available versions below
# https://github.com/actions/node-versions/blob/main/versions-manifest.json
node-version: "16.x"
node-version: "20.x"

jobs:
publish:
Expand Down Expand Up @@ -49,8 +49,11 @@ jobs:
# with:
# github_token: ${{ secrets.GITHUB_TOKEN }}

- name: npx can-npm-publish
run: npx can-npm-publish
- name: Check if package version can be published
uses: technote-space/package-version-check-action@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PACKAGE_MANAGER: yarn

- name: Get new package version
id: package-version
Expand Down
11 changes: 0 additions & 11 deletions .mocharc.json

This file was deleted.

6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: "ts-jest/presets/js-with-babel-esm",
testEnvironment: "node",
testMatch: ['**/unit/**/*.test.ts'],
transformIgnorePatterns: ['/node_modules']
};
34 changes: 21 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"name": "vwbl-sdk",
"description": "VWBL SDK for TypeScript",
"version": "0.1.12",
"version": "0.1.13",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"engines": {
"node": ">=20.0.0"
},
"files": [
"dist"
],
Expand All @@ -19,15 +22,17 @@
"build": "tsc",
"format": "eslint --fix --quiet './src/**/*.ts*'",
"lint": "eslint './src/**/*.ts*'",
"test": "mocha-parallel-tests -r ts-node/register test/unit/**/*.test.ts --exit",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"mocha": "mocha-parallel-tests -r ts-node/register --exit"
},
"devDependencies": {
"@babel/preset-typescript": "^7.23.3",
"@truffle/hdwallet-provider": "^2.1.11",
"@types/chai": "^4.3.0",
"@types/crypto-js": "^4.0.2",
"@types/jest": "^29.5.12",
"@types/mocha": "^9.1.0",
"@types/sinon": "^9.0.0",
"@types/sinon": "^17.0.3",
"@types/uuid": "^8.3.3",
"@typescript-eslint/eslint-plugin": "^5.13.0",
"@typescript-eslint/parser": "^5.13.0",
Expand All @@ -38,13 +43,15 @@
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"file-api": "^0.10.4",
"jest": "^29.7.0",
"mocha": "^7.1.2",
"mocha-parallel-tests": "^2.3.0",
"prettier": "^2.5.1",
"sinon": "4.1.2",
"ts-loader": "^9.4.2",
"ts-node": "^10.7.0",
"typescript": "^4.5.3"
"sinon": "^17.0.1",
"ts-jest": "^29.1.2",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.338.0",
Expand All @@ -53,20 +60,21 @@
"axios": "^0.27.2",
"crypto": "^1.0.1",
"crypto-js": "^4.1.1",
"ethers": "^5.7.2",
"filereader": "^0.10.3",
"ethers": "5.7.2",
"fetch-blob": "^4.0.0",
"form-data": "^4.0.0",
"jimp": "^0.16.1",
"mime": "^4.0.1",
"nft.storage": "^7.0.0",
"uuid": "^8.3.2",
"web3": "^1.8.0"
"web3": "^4.3.0"
},
"browser": {
"crypto": false,
"filereader": false,
"fs": false,
"path": false,
"os": false,
"filereader": false,
"crypto": false
"path": false
},
"react-native": {
"fs": false,
Expand Down
15 changes: 10 additions & 5 deletions src/storage/aws/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Upload } from "@aws-sdk/lib-storage";
import * as fs from "fs";
import * as Stream from "stream";

import { getMimeType, toArrayBuffer } from "../../util/fileHelper";
import { getMimeType } from "../../util";
import { PlainMetadata } from "../../vwbl/metadata";
import { EncryptLogic } from "../../vwbl/types";
import { EncryptLogic, FileOrPath } from "../../vwbl/types";
import { AWSConfig } from "./types";

export const uploadEncryptedFile = async (
Expand Down Expand Up @@ -45,7 +45,11 @@ export const uploadEncryptedFile = async (
return `${awsConfig.cloudFrontUrl.replace(/\/$/, "")}/${key}`;
};

export const uploadThumbnail = async (thumbnailImage: File, uuid: string, awsConfig?: AWSConfig): Promise<string> => {
export const uploadThumbnail = async (
thumbnailImage: FileOrPath,
uuid: string,
awsConfig?: AWSConfig
): Promise<string> => {
if (!awsConfig || !awsConfig.bucketName.content) {
throw new Error("bucket is not specified.");
}
Expand All @@ -60,14 +64,15 @@ export const uploadThumbnail = async (thumbnailImage: File, uuid: string, awsCon
})
: fromIni({ profile: awsConfig.profile });
const s3Client = new S3Client({ credentials });
const fileName = thumbnailImage instanceof File ? thumbnailImage.name : thumbnailImage.split("/").slice(-1)[0]; //ファイル名の取得だけのためにpathを使いたくなかった

const key = `data/${uuid}-${thumbnailImage.name}`;
const key = `data/${uuid}-${fileName}`;
const type = getMimeType(thumbnailImage);
const isRunningOnBrowser = typeof window !== "undefined";
const uploadCommand = new PutObjectCommand({
Bucket: awsConfig.bucketName.content,
Key: key,
Body: isRunningOnBrowser ? thumbnailImage : new Uint8Array(await toArrayBuffer(thumbnailImage)),
Body: thumbnailImage instanceof File ? thumbnailImage : await fs.promises.readFile(thumbnailImage),
ContentType: type,
ACL: "public-read",
});
Expand Down
8 changes: 4 additions & 4 deletions src/storage/ipfs/upload.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Blob, File, NFTStorage } from "nft.storage";
import { Blob, NFTStorage } from "nft.storage";

import { getMimeType } from "../../util/fileHelper";
import { getMimeType } from "../../util";
import { PlainMetadata } from "../../vwbl/metadata";
import { EncryptLogic } from "../../vwbl/types";
import { EncryptLogic, FileOrPath } from "../../vwbl/types";

export class UploadToIPFS {
private client: NFTStorage;
Expand All @@ -23,7 +23,7 @@ export class UploadToIPFS {
return `https://nftstorage.link/ipfs/${cid}`;
}

async uploadThumbnail(thumbnailImage: File): Promise<string> {
async uploadThumbnail(thumbnailImage: FileOrPath): Promise<string> {
const thumbnailFileType = getMimeType(thumbnailImage);
const thumbnailblob = new Blob([thumbnailImage], { type: thumbnailFileType });

Expand Down
72 changes: 36 additions & 36 deletions src/util/fileHelper.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const FileReaderNodeJs = require("filereader");
import mime from "mime";

import { FileOrPath } from "../vwbl/types";
const isRunningOnBrowser = typeof window !== "undefined";

export const toBase64FromBlob = async (blob: Blob): Promise<string> => {
return new Promise((resolve, reject) => {
const reader = switchReader();
reader.readAsDataURL(blob);
reader.onload = () => {
const result = reader.result;
if (!result || typeof result !== "string") {
reject("cannot convert to base64 string");
} else {
resolve(result);
}
};
reader.onerror = (error: any) => reject(error);
});
if (isRunningOnBrowser) {
return new Promise((resolve, reject) => {
const reader = new window.FileReader();
reader.readAsDataURL(blob);
reader.onload = () => {
const result = reader.result;
if (!result || typeof result !== "string") {
reject("cannot convert to base64 string");
} else {
resolve(result);
}
};
reader.onerror = (error: any) => reject(error);
});
}
const arrayBuffer = await blob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
return buffer.toString("base64");
};

export const getMimeType = (file: File): string => {
return file.type;
export const getMimeType = (file: FileOrPath): string => {
return file instanceof File ? file.type : mime.getType(file) || "";
};

export const toArrayBuffer = async (blob: Blob): Promise<ArrayBuffer> => {
return new Promise((resolve, reject) => {
const reader = switchReader();
reader.readAsArrayBuffer(blob);
reader.onload = () => {
const result = reader.result;
if (!result || !(result instanceof Uint8Array)) {
reject("cannot convert to ArrayBuffer");
} else {
resolve(result);
}
};
reader.onerror = (error: any) => reject(error);
});
};

const switchReader = (): any => {
if (isRunningOnBrowser) {
return new FileReader();
} else {
return new FileReaderNodeJs();
return new Promise((resolve, reject) => {
const reader = new window.FileReader();
reader.readAsArrayBuffer(blob);
reader.onload = () => {
const result = reader.result;
if (!result || !(result instanceof Uint8Array)) {
reject("cannot convert to ArrayBuffer");
} else {
resolve(result);
}
};
reader.onerror = (error: any) => reject(error);
});
}
return await blob.arrayBuffer();
};
2 changes: 1 addition & 1 deletion src/vwbl/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from "ethers";
import * as Stream from "stream";
import Web3 from "web3";
import { Web3 } from "web3";

import { UploadToIPFS } from "../storage/ipfs";
import {
Expand Down
2 changes: 1 addition & 1 deletion src/vwbl/blockchain/Sign.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from "ethers";
import Web3 from "web3";
import { Web3 } from "web3";

interface IEthersSigner {
signMessage(message: string | ethers.utils.Bytes): Promise<string>;
Expand Down
10 changes: 4 additions & 6 deletions src/vwbl/blockchain/erc1155/VWBLProtocol.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import Web3 from "web3";
import { Contract } from "web3-eth-contract";
import { AbiItem } from "web3-utils";
import { Web3 } from "web3";

import vwbl1155 from "../../../contract/VWBLERC1155ERC2981.json";
import vwbl1155IPFS from "../../../contract/VWBLERC1155ERC2981ForMetadata.json";
import { getFeeSettingsBasedOnEnvironment } from "../../../util/transactionHelper";
import { GasSettings } from "../../types";

export class VWBLERC1155Contract {
private contract: Contract;
private contract: any; // eslint-disable-line
private web3: Web3;

constructor(web3: Web3, address: string, isIpfs: boolean) {
this.web3 = web3;
this.contract = isIpfs
? new web3.eth.Contract(vwbl1155IPFS.abi as AbiItem[], address)
: new web3.eth.Contract(vwbl1155.abi as AbiItem[], address);
? new web3.eth.Contract(vwbl1155IPFS.abi, address)
: new web3.eth.Contract(vwbl1155.abi, address);
}

async mintToken(
Expand Down
11 changes: 3 additions & 8 deletions src/vwbl/blockchain/erc721/VWBLProtocol.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import Web3 from "web3";
import { Contract } from "web3-eth-contract";
import { AbiItem } from "web3-utils";
import { Web3 } from "web3";

import vwbl from "../../../contract/VWBLERC721ERC2981.json";
import vwblIPFS from "../../../contract/VWBLERC721ERC2981ForMetadata.json";
import { getFeeSettingsBasedOnEnvironment } from "../../../util/transactionHelper";
import { GasSettings } from "../../types";

export class VWBLNFT {
private contract: Contract;
private contract: any; // eslint-disable-line
private web3: Web3;

constructor(web3: Web3, address: string, isIpfs: boolean) {
this.web3 = web3;
this.contract = isIpfs
? new web3.eth.Contract(vwblIPFS.abi as AbiItem[], address)
: new web3.eth.Contract(vwbl.abi as AbiItem[], address);
this.contract = isIpfs ? new web3.eth.Contract(vwblIPFS.abi, address) : new web3.eth.Contract(vwbl.abi, address);
}

async mintToken(decryptUrl: string, feeNumerator: number, documentId: string, gasSettings?: GasSettings) {
Expand All @@ -42,7 +38,6 @@ export class VWBLNFT {
maxFeePerGas: _maxFeePerGas,
};
}

console.log("transaction start");
const receipt = await this.contract.methods.mint(decryptUrl, feeNumerator, documentId).send(txSettings);
console.log("transaction end");
Expand Down
Loading

0 comments on commit 4aea878

Please sign in to comment.