Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat fleek api storage #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
module.exports = {
parser: "@typescript-eslint/parser",
parser: '@typescript-eslint/parser',
extends: [
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
parserOptions: {
ecmaVersion: 2018,
sourceType: "module"
sourceType: 'module',
},
rules: {
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-console": "error",
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'no-console': 'error',
/* More rule definitions in .prettierrc.js */
}
};
},
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ cache/
dist/
coverage
.nyc_output
.env
settings.json
yarn-error.log
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1hive/hardhat-aragon",
"version": "0.1.0",
"version": "0.1.1",
"description": "Aragon Hardhat plugin",
"author": "1hive <[email protected]>",
"license": "MIT",
Expand All @@ -23,17 +23,21 @@
"lint": "eslint",
"test": "mocha --exit --recursive 'test/**/*.test.ts'",
"build": "tsc",
"postbuild": "out='lib/node_modules' && root=$(npm root) && mkdir -p $out && npm ls --production --parseable | tail --lines=+2 | sed \"s%$root%%\" | cut --delimiter='/' --fields=2 | sort | uniq | xargs -I % cp -r \"$root/%\" $out",
"watch": "tsc -w",
"test:coverage": "nyc --all --reporter=lcov --reporter=text-summary --reporter=html yarn test",
"report-coverage": "codecov"
},
"dependencies": {
"@fleekhq/fleek-storage-js": "^1.0.23",
"@solidity-parser/parser": "^0.12.2",
"chalk": "^4.1.1",
"execa": "^5.0.0",
"fs-extra": "^8.1.0",
"ipfs-http-client": "^50.0.0",
"node-fetch": "^2.6.1"
"ipfs-only-hash": "^4.0.0",
"node-fetch": "^2.6.1",
"typestub-ipfs-only-hash": "^4.0.0"
},
"devDependencies": {
"@aragon/abis": "^1.1.0",
Expand Down Expand Up @@ -64,6 +68,7 @@
"prettier": "2.0.5",
"sinon": "^9.0.1",
"ts-node": "^8.1.0",
"tsconfig-paths": "^4.1.2",
"typescript": "^4.2.4"
},
"peerDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const DEFAULT_IPFS_API_ENDPOINT = 'http://localhost:5001/'
export const DEFAULT_IPFS_GATEWAY = 'https://ipfs.io/'

export const DEFAULT_PINATA_API_ENDPOINT = 'https://api.pinata.cloud/'
export const DEFAULT_FLEEK_API_ENDPOINT = 'https://storageapi2.fleek.co/'

export const DEFAULT_APP_SRC_PATH = 'app/'
export const DEFAULT_APP_BUILD_PATH = 'dist/'
Expand Down
22 changes: 18 additions & 4 deletions src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,21 @@ import {

// We ommit these imports beacuse they are peer dependencies and will be added
// by the plugin user. Otherwise naming conflicts may araise
// import '@nomiclabs/hardhat-ethers'
// import 'hardhat-deploy'
import '@nomiclabs/hardhat-ethers'
import 'hardhat-deploy'

// This import is needed to let the TypeScript compiler know that it should include your type
// extensions in your npm package's types file.
import './type-extensions'
import { fleekUploadContent } from './utils/fleek/fleek'

extendConfig(
(config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) => {
config.ipfs = {
url: userConfig.ipfs?.url ?? DEFAULT_IPFS_API_ENDPOINT,
gateway: userConfig.ipfs?.gateway ?? DEFAULT_IPFS_GATEWAY,
pinata: userConfig.ipfs?.pinata,
fleek: userConfig.ipfs?.fleek,
}

config.aragon = {
Expand Down Expand Up @@ -188,8 +190,11 @@ task(TASK_PUBLISH, 'Publish a new app version to Aragon Package Manager')
await apm.assertCanPublish(finalAppEnsName, owner.address, provider)

const ipfs = hre.ipfs
await assertIpfsApiIsAvailable(ipfs, hre.config.ipfs.url)

log(
`assertIpfsApiIsAvailable ${ipfs}, hre.config.ipfs.url: ${hre.config.ipfs.url}`
)
// await assertIpfsApiIsAvailable(ipfs, hre.config.ipfs.url)
// return
// Using let + if {} block instead of a ternary operator
// to assign value and log status to console
let contractAddress: string
Expand Down Expand Up @@ -266,6 +271,15 @@ task(TASK_PUBLISH, 'Publish a new app version to Aragon Package Manager')

await assertUploadContetResolve(contentHash, hre.config.ipfs.gateway)

if (hre.config.ipfs.fleek && hre.config.ipfs.fleek.key !== '') {
log('Storage Pinning content to Fleek...')
fleekUploadContent({
contentHash,
appEnsName: finalAppEnsName,
version: nextVersion,
hre,
})
}
if (hre.config.ipfs.pinata && hre.config.ipfs.pinata.key !== '') {
log('Pinning content to pinata...')
const response = await pinContent({
Expand Down
1 change: 1 addition & 0 deletions src/internal/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ declare module 'hardhat/types/config' {
declare module 'hardhat/types/runtime' {
export interface HardhatRuntimeEnvironment {
ipfs: any
fleek: any
}
}
141 changes: 141 additions & 0 deletions src/internal/utils/fleek/fleek.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/* eslint-disable @typescript-eslint/camelcase */
import fs from 'fs'
// import * as Hash from 'typestub-ipfs-only-hash'
// import entire SDK
// import AWS from 'aws-sdk';
// import AWS object without services
// import AWS from 'aws-sdk/global';
// import individual service
import fleekStorage from '@fleekhq/fleek-storage-js'
import { log } from '../../ui/logger'
// import { FleekConfig } from '~/src/types'
import { HardhatRuntimeEnvironment } from 'hardhat/types'
// const PIN_BY_HASH_API = 'pinByHash'

// export async function pinContent({
// contentHash,
// appEnsName,
// version,
// network,
// pinata,
// }: {
// contentHash: string
// appEnsName: string
// version: string
// network: string
// pinata: PinataConfig
// }): Promise<any | undefined> {
// const url = urlJoin(DEFAULT_PINATA_API_ENDPOINT, 'pinning', PIN_BY_HASH_API)

// const body = {
// hashToPin: contentHash,
// pinataMetadata: {
// name: `${network}:${appEnsName}@${version}`,
// },
// }

// try {
// const response = await fetch(url, {
// method: 'POST',
// body: JSON.stringify(body),
// headers: {
// 'Content-Type': 'application/json',
// pinata_api_key: pinata.key,
// pinata_secret_api_key: pinata.secret,
// },
// })

// const data = await response.json()

// return data
// } catch (error) {
// log(`Warning: Error while fetching pinata API with ${error}`)
// return undefined
// }
// }

export async function fleekUploadContent({
contentHash,
appEnsName,
version,
hre,
}: {
contentHash: string
appEnsName: string
version: string
hre: HardhatRuntimeEnvironment
}) {
const fleekConfig = hre.config.ipfs.fleek
if (!fleekConfig) {
log(`config.ipfs.fleek not defined or not found`)
return
}
const apiKey = fleekConfig.key
const apiSecret = fleekConfig.secret

log(`apiKey: ${apiKey} - apiSecret: ${apiSecret}`)

const network = hre.network
}
// export async function listBucket() {
// const buckets = await fleekStorage.listBuckets({
// apiKey,
// apiSecret,
// })
// // log(`listBuckets result: ${buckets}`)
// for (const buck of buckets) {
// log(`listBuckets result: ${buck.name}`)
// }

export async function listBucket({ hre }: { hre: HardhatRuntimeEnvironment }) {
const fleekConfig = hre.config.ipfs.fleek
if (!fleekConfig) {
log(`config.ipfs.fleek not defined or not found`)
return
}
const apiKey = fleekConfig.key
const apiSecret = fleekConfig.secret

const stream = fs.readFileSync('./test.txt')
// log(`listBuckets result: ${stream}`)
// const hash = await Hash.of(stream)
// log(`listBuckets hash: ${hash}`)
const uploadedFile = await fleekStorage.upload({
apiKey,
apiSecret,
key: 'test2.txt',
bucket: 'c3963ae5-3c0b-4752-b068-040f3f263566-bucket/folder',
data: stream,
})
log(
`listBuckets uploaded: ${uploadedFile.hash} ${uploadedFile.key} ${uploadedFile.hashV0} ${uploadedFile.publicUrl}`
)

const files = await fleekStorage.listFiles({
apiKey,
apiSecret,
getOptions: ['bucket', 'key', 'hash', 'publicUrl'],
})

// log(`listBuckets result: ${files}`)
for (const file of files) {
log(`listBuckets file: ${file.key} ${file.hash} ${file.publicUrl}`)
}

// const file = await fleekStorage.getFileFromHash({
// hash: 'bafybeid6x4kg4lkpooleyo7yxxoaodq3svdfklmld6mtfvcaykwlih55sy',
// })
// log(`listBuckets result: ${file.key} ${file.hash} ${file.publicUrl}`)

// return buckets
}
// export function listBucket(): void {
// const s3 = new S3({
// apiVersion: '2006-03-01',
// accessKeyId: apiKey,
// secretAccessKey: apiSecret,
// })
// s3.listBuckets((err, data) => {
// log(`listBuckets err: ${err} - data: ${data}`)
// })
// }
1 change: 1 addition & 0 deletions src/internal/utils/fleek/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './fleek'
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ export interface PinataConfig {
key: string
secret: string
}
export interface FleekConfig {
key: string
secret: string
}

export interface IpfsUserConfig {
url?: string
gateway?: string
pinata?: PinataConfig
fleek?: FleekConfig
}

export interface IpfsConfig {
url: string
gateway: string
pinata?: PinataConfig
fleek?: FleekConfig
}

export interface Dependencies {
Expand Down
Loading