-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Kredeum/nftStorage
NFT Storage on client
- Loading branch information
Showing
38 changed files
with
1,211 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// }); | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.