Skip to content

Commit

Permalink
Merge pull request #52 from Kredeum/nftStorage
Browse files Browse the repository at this point in the history
NFT Storage on client
  • Loading branch information
zapaz authored May 20, 2021
2 parents 3b75ed9 + a0ede65 commit 19fecb4
Show file tree
Hide file tree
Showing 38 changed files with 1,211 additions and 299 deletions.
12 changes: 3 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@
"phpsab.standard": "WordPress",
"phpsab.snifferMode": "onType",
"phpsab.debug": true,
"phpsab.snifferArguments": [
"-n",
"--ignore=lib",
"--extensions=php"
],
"phpsab.fixerArguments": [
"--extensions=php"
],
"phpsab.snifferArguments": ["-n", "--ignore=lib", "--extensions=php"],
"phpsab.fixerArguments": ["--extensions=php"],
"phpsab.snifferShowSources": true
}
}
2 changes: 1 addition & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<link rel="icon" type="image/png" href="./klogo.png" />

<script defer src="./build/kredeum_nft.js"></script>
<script defer src="./build/kredeum-nft.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion app/metamask.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<link rel="icon" type="image/png" href="./favicon.png" />

<script defer src="./build/kredeum_metamask.js"></script>
<script defer src="./build/kredeum-metamask.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion app/mint.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<link rel="icon" type="image/png" href="./favicon.png" />

<script defer src="./build/kredeum_nft_mint.js"></script>
<script defer src="./build/kredeum-nft-mint.js"></script>
</head>

<body>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/networks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const networks = [
rpcUrls: ["https://rpc-mainnet.maticvigil.com/v1"],
nativeCurrency: { name: "Matic", symbol: "MATIC", decimals: 18 },
blockExplorerUrls: [
"https://polygon-explorer-mainnet.chainstacklabs.com/",
"https://polygon-explorer-mainnet.chainstacklabs.com",
"https://explorer-mainnet.maticvigil.com"
]
},
Expand Down
32 changes: 32 additions & 0 deletions lib/nft-storage.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pin from "./pin.mjs";

class nftStorage extends pin {
constructor(key) {
super(key);
}

async pin(buffer) {
let cid = "";

const data = await (
await fetch("https://api.nft.storage/upload", {
method: "POST",
body: buffer,
headers: {
Authorization: "Bearer " + super.key
}
})
).json();
// console.log(data);
if (data.ok) {
cid = data.value?.cid;
console.log(`https://ipfs.io/ipfs/${cid}`);
} else {
console.error(data.error);
}

return cid;
}
}

export default nftStorage;
2 changes: 1 addition & 1 deletion lib/nft.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from "ethers";

import libKRE from "../lib/kre1.mjs";
import networksKRE from "../lib/networks_kre.mjs";
import networksKRE from "../lib/networks-kre.mjs";
import axios from "axios";

const nft = {};
Expand Down
25 changes: 25 additions & 0 deletions lib/pin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class pin {
_key = null;

get key() {
return this._key;
}

constructor(key) {
this._key = key;
}

async pin(buffer, options) {
return "pin class is abstract";
}

async pinUrl(url, options) {
return await this.pin(await (await fetch(url)).blob(), options);
}

async pinJson(object, options) {
return await this.pin(JSON.stringify(object, null, 2), options);
}
}

export default pin;
File renamed without changes.
36 changes: 36 additions & 0 deletions old/pinata/lib/pinata.2.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pin from "./pin.mjs";
import axios from "axios";

class pinata extends pin {
constructor(key) {
super(key);
}

async pin(buffer) {
const pinataOptions = { cidVersion: 1 };
const data = new FormData();
data.append("file", buffer);
data.append("pinataOptions", JSON.stringify(pinataOptions));

let resp;
try {
resp = await axios({
method: "POST",
url: "https://api.pinata.cloud/pinning/pinFileToIPFS",
data: data,
maxBodyLength: "Infinity",
headers: {
pinata_api_key: "b253abf46663a21f0e40",
pinata_secret_api_key: "34b0951ff74f6bd1bd30463d50c4de61b8cb4183503fb26fd59595d5c78fced2"
}
});
console.log("pinata.call axios", config);
} catch (e) {
console.error("pinata.call ERROR", e);
}
//console.log('pinata.pinFileToIPFS', pinFileJson);
return resp.data;
}
}

export default pinata;
55 changes: 55 additions & 0 deletions old/pinata/lib/pinata.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pin from "./pin.mjs";
import axios from "axios";

class pinata extends pin {
constructor(key) {
super(key);
}

async pin(buffer) {
const formData = new FormData();
formData.append("file", buffer);
formData.append("pinataOptions", JSON.stringify({ cidVersion: 1 }));

console.log(formData.toString());

const options = {
method: "POST",
body: formData,
headers: {
withCredentials: true,
maxContentLength: "Infinity", //this is needed to prevent axios from erroring out with large files
maxBodyLength: "Infinity",
"Content-Type": `multipart/form-data; boundary=${formData.getBoundary()}`,
pinata_api_key: "b253abf46663a21f0e40",
pinata_secret_api_key: "34b0951ff74f6bd1bd30463d50c4de61b8cb4183503fb26fd59595d5c78fced2"
}
};
// console.log("options",options);
// const data = await (await fetch("https://api.pinata.cloud/pinning/pinFileToIPFS", options)).json();

const resp = await axios.post("https://api.pinata.cloud/pinning/pinFileToIPFS", formData, {
withCredentials: true,
maxContentLength: "Infinity",
maxBodyLength: "Infinity",
headers: {
"Content-type": `multipart/form-data; boundary= ${formData._boundary}`,
pinata_api_key: "b253abf46663a21f0e40",
pinata_secret_api_key: "34b0951ff74f6bd1bd30463d50c4de61b8cb4183503fb26fd59595d5c78fced2"
}
});

console.log("resp.data", resp.data);

if (data.ok) {
cid = data.value?.cid;
console.log(`https://ipfs.io/ipfs/${cid}`);
} else {
console.error(data.error);
}

return data;
}
}

export default pinata;
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions old/pinata/tests/pinata-ok.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fetch from "node-fetch";
import FormData from "form-data";
import fs from "fs";
import string2fileStream from "string-to-file-stream";

let data = new FormData();
// data.append("file", fs.createReadStream("./klogo.png"));
data.append("file", string2fileStream("Bonjour le monde"));
data.append("pinataOptions", JSON.stringify({ cidVersion: 1 }));

console.log(
await (
await fetch("https://api.pinata.cloud/pinning/pinFileToIPFS", {
method: "POST",
body: data,
headers: {
"Content-Type": `multipart/form-data; boundary=${data.getBoundary()}`,
pinata_api_key: "b253abf46663a21f0e40",
pinata_secret_api_key: "34b0951ff74f6bd1bd30463d50c4de61b8cb4183503fb26fd59595d5c78fced2"
}
})
).json()
);
58 changes: 58 additions & 0 deletions old/pinata/tests/pinata-ok2.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//imports needed for this function
import axios from "axios";
import fs from "fs";
import FormData from "form-data";

export const pinFileToIPFS = (pinataApiKey, pinataSecretApiKey) => {
const url = `https://api.pinata.cloud/pinning/pinFileToIPFS`;

//we gather a local file for this example, but any valid readStream source will work here.
let data = new FormData();
data.append("file", fs.createReadStream("./klogo.png"));

//You'll need to make sure that the metadata is in the form of a JSON object that's been convered to a string
//metadata is optional
const metadata = JSON.stringify({
name: "testname",
keyvalues: {
exampleKey: "exampleValue"
}
});
data.append("pinataMetadata", metadata);

//pinataOptions are optional
const pinataOptions = JSON.stringify({
cidVersion: 0,
customPinPolicy: {
regions: [
{
id: "FRA1",
desiredReplicationCount: 1
},
{
id: "NYC1",
desiredReplicationCount: 2
}
]
}
});
data.append("pinataOptions", pinataOptions);

return axios
.post(url, data, {
maxBodyLength: "Infinity", //this is needed to prevent axios from erroring out with large files
headers: {
"Content-Type": `multipart/form-data; boundary=${data._boundary}`,
pinata_api_key: pinataApiKey,
pinata_secret_api_key: pinataSecretApiKey
}
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.error(error);
});
};

pinFileToIPFS("b253abf46663a21f0e40", "34b0951ff74f6bd1bd30463d50c4de61b8cb4183503fb26fd59595d5c78fced2");
47 changes: 47 additions & 0 deletions old/pinata/tests/pinata.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// npx mocha tests/pinata.mjs
import { expect } from "chai";
import pinataPins from "../lib/pinata.mjs";
import fetch from "node-fetch";
import FormData from "form-data";
global.fetch = fetch;
global.FormData = FormData;

const txt = "Bonjour le monde";
const txtCID = "bafybeic2defbqe6j3swepixdfeyrmx6l3pk3fch3uqbkwtxl7gzs4pspwu";
const key = process.env.NFT_STORAGE_KEY;
const imgUrl = "https://www.kredeum.com/favicon.ico";
const imgCID = "bafybeid6lyb42fm2yabxi7ur2sea4fdokikuidkyqwy54lzcytfxn44yra";
const jsn = { json: "file" };
const jsnCID = "bafybeidyaf5zduqk2lsfiikpanmxmqs2imamfrz2kwnnovvmvv6pq3wcge";

describe("pinataPins Add Text", async function () {
this.timeout(10000);

it("Add text should return given CID", async function () {
const pinata = new pinataPins(key);
expect(await pinata.pin(txt)).to.be.equal(txtCID);
});

// it("Add text should fail without auth key", async function () {
// const pinata = new pinataPins();
// expect(await pinata.pin(txt)).to.be.equal("");
// });
});

// describe("pinataPins Add Image", async function () {
// this.timeout(10000);

// it("Add image should return given CID", async function () {
// const pinata = new pinataPins(key);
// expect(await pinata.pinUrl(imgUrl)).to.be.equal(imgCID);
// });
// });

// describe("pinataPins Add Json", async function () {
// this.timeout(10000);

// it("Add json should return given CID", async function () {
// const pinata = new pinataPins(key);
// expect(await pinata.pinJson(jsn)).to.be.equal(jsnCID);
// });
// });
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"smart-clean": "rm -rf solidity/build",
"smart-build": "npx hardhat compile",
"smart-check": "npx prettier-eslint solidity/**/*.sol scripts/*js scripts/*.sh tests/*js --write",
"smart-test": "npx hardhat test",
"smart-test": "npx mocha tests/nft-storage.mjs",
"smart-flatten": "npx hardhat flatten solidity/contracts/OpenNFTs.sol > solidity/flatten/OpenNFTs.sol",
"smart-deploy": "npx hardhat run scripts/deploy.mjs",
"smart-deploy-mumbai": "npx hardhat run scripts/deploy.mjs --network mumbai",
"smart-deploy-matic": "npx hardhat run scripts/deploy.mjs --netwok matic",
"smart-list": "npx hardhat run scripts/list.mjs",
"dapp-clean": "rm -rf app/build",
"dapp-build": "PROD=true rollup -c && cp wordpress/kredeum-nfts/lib/js/kredeum_nft.js app/build ",
"dapp-build": "PROD=true rollup -c && cp wordpress/kredeum-nfts/lib/js/kredeum-nft.js app/build ",
"dapp-build-dev": "rollup -c",
"dapp-check": "npx prettier-eslint **/*.svelte **/*js **/*.ts **/*.json **/*.html **/*.css **/*.yaml **/*.graphql **/*.md --write",
"dapp-test": "",
Expand Down Expand Up @@ -61,17 +61,21 @@
"eslint": "^7.26.0",
"ethereum-waffle": "^3.3.0",
"ethers": "^5.1.4",
"hardhat": "^2.2.1",
"form-data": "^4.0.0",
"hardhat": "^2.3.0",
"hardhat-abi-exporter": "^2.2.1",
"hbs-cli": "^1.4.0",
"mocha": "^8.4.0",
"multihashing-async": "^2.1.2",
"nft.storage": "^1.3.1",
"node-fetch": "^2.6.1",
"prettier": "^2.3.0",
"prettier-eslint": "^12.0.0",
"prettier-eslint-cli": "^5.0.1",
"prettier-plugin-sh": "^0.6.1",
"prettier-plugin-solidity": "^1.0.0-beta.10",
"prettier-plugin-svelte": "^2.3.0",
"rollup": "^2.47.0",
"rollup": "^2.48.0",
"rollup-plugin-css-only": "^3.1.0",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-node-builtins": "^2.1.2",
Expand All @@ -82,6 +86,7 @@
"solc": "^0.8.4",
"solhint": "^3.3.4",
"solhint-plugin-prettier": "^0.0.5",
"string-to-file-stream": "^1.3.0",
"svelte": "^3.38.2",
"web-encoding": "^1.1.5"
}
Expand Down
Loading

0 comments on commit 19fecb4

Please sign in to comment.