Skip to content

Instantly share code, notes, and snippets.

@radio-alice
Created August 28, 2019 19:39
Show Gist options
  • Save radio-alice/d9341c3086e6ac0476abfb94a8eee47d to your computer and use it in GitHub Desktop.
Save radio-alice/d9341c3086e6ac0476abfb94a8eee47d to your computer and use it in GitHub Desktop.
const pinataAuthUrl = `https://api.pinata.cloud/data/testAuthentication`
const temporalAuthUrl = `https://api.temporal.cloud/v2/auth/login`
export const storeInCache = (wrapper, key, value) => {
return wrapper.cache.set(key, value)
}
export const getFromCache = (wrapper, key) => {
return wrapper.cache.observe(key)
}
export const createIpfsProvider = async (
provider,
providerId = '',
providerSecret = ''
) => {
switch (provider) {
case 'pinata':
return getPinataNode(providerId, providerSecret)
case 'infura':
return getInfuraNode()
case 'temporal':
return getTemporalNode(providerId, providerSecret)
}
}
const getPinataNode = async (key, secret) => {
await fetch(pinataAuthUrl, {
method: 'GET',
headers: {
pinata_api_key: key,
pinata_secret_api_key: secret,
},
})
return pinataNode(key, secret)
}
const pinataNode = (key, secret) => {
const pinataPutEndpoint = `https://api.pinata.cloud/pinning/pinJSONToIPFS`
const pinataDagGetEndpoint = `https://api.pinata.cloud/data/pinList`
return {
dagGet: async () => {
const response = await fetch(pinataDagGetEndpoint, {
method: 'GET',
headers: {
pinata_api_key: key,
pinata_secret_api_key: secret,
},
})
return response.json()
},
dagPut: async json => {
const response = await fetch(pinataPutEndpoint, {
method: 'POST',
headers: {
pinata_api_key: key,
pinata_secret_api_key: secret,
'Content-Type': 'application/json',
},
body: JSON.stringify({ pinataContent: json }),
})
return response.json()
},
}
}
const getInfuraNode = () => {
const getEndpoint = `https://ipfs.infura.io:5001/api/v0/dag/get?arg=`
const putEndpoint = `https://ipfs.infura.io:5001/api/v0/dag/put`
return {
dagGet: async hash => {
const url = `${getEndpoint}${hash}`
const response = await fetch(url, {
method: 'GET',
})
return response.json()
},
dagPut: async json => {
let data = new FormData()
data.append('v0', JSON.stringify(json))
const response = await fetch(putEndpoint, {
method: 'POST',
body: data,
})
return response.json()
},
}
}
const getTemporalNode = async (username, password) => {
const getEndpoint = `https://api.temporal.cloud/v2/ipfs/public/dag/`
const putEndpoint = `https://api.temporal.cloud/v2/ipfs/public/file/add`
const authData = await fetch(temporalAuthUrl, {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
},
body: JSON.stringify({
username: username,
password: password,
}),
})
const { token } = await authData.json()
console.log(token)
return {
dagGet: async hash => {
const url = `${getEndpoint}${hash}`
const response = await fetch(url, {
method: 'GET',
headers: {
Authorization: 'Bearer ' + token,
},
})
return response.json()
},
dagPut: async () => {
let key = Math.random().toString()
let val = Math.random().toString()
let blob = new Blob([{ [key]: val }], {
type: 'application/json',
})
let formData = new FormData()
formData.append('hold_time', '1')
formData.append('file', blob)
const response = await fetch(putEndpoint, {
method: 'POST',
headers: {
Authorization: 'Bearer ' + token,
},
body: formData,
})
return response.json()
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment