Skip to content

Commit

Permalink
Merge pull request #28 from nevermined-io/fix/sdk_0190
Browse files Browse the repository at this point in the history
Format, linting and latest versions
  • Loading branch information
aaitor authored Feb 11, 2022
2 parents fc4e9c1 + fec56f7 commit 66241f6
Show file tree
Hide file tree
Showing 10 changed files with 387 additions and 76 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nevermined-io/cli",
"version": "0.2.2",
"version": "0.2.3",
"main": "index.js",
"repository": "[email protected]:nevermined-io/cli.git",
"author": "Nevermined",
Expand All @@ -20,7 +20,7 @@
"ncli": "./dist/src/index.js"
},
"dependencies": {
"@nevermined-io/nevermined-sdk-js": "^0.18.9",
"@nevermined-io/nevermined-sdk-js": "^0.19.0",
"@truffle/hdwallet-provider": "^1.4.1",
"chalk": "^4.1.2",
"cli-docs-generator": "^1.0.7",
Expand Down
41 changes: 22 additions & 19 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ import { ProvenanceMethods, StatusCodes } from './utils/enums'

const cmdHandler = async (cmd: Function, argv: any) => {
const { verbose, network } = argv

const config = getConfig(network as string)
const nvm = await loadNevermined(config, network, verbose)

if (verbose) {
if (verbose) {
logger.level = 'debug'
} else {
logger.level = 'info'
}
}

logger.debug(chalk.dim(`Debug mode: '${chalk.greenBright('on')}'\n`))
logger.debug(chalk.dim(`Using network: '${chalk.whiteBright(network)}'\n`))
Expand Down Expand Up @@ -317,14 +317,16 @@ y.command(
'import',
'Import an asset using the metadata in JSON format',
(yargs) =>
yargs.positional('metadata', {
describe: 'The metadata file',
type: 'string'
}).option('encrypt', {
type: 'boolean',
default: false,
description: 'Use encrypted service endpoint'
}),
yargs
.positional('metadata', {
describe: 'The metadata file',
type: 'string'
})
.option('encrypt', {
type: 'boolean',
default: false,
description: 'Use encrypted service endpoint'
}),
async (argv) => cmdHandler(registerAsset, argv)
)
.command(
Expand Down Expand Up @@ -374,14 +376,15 @@ y.command(
'order did',
'Order an asset given a DID',
(yargs) =>
yargs.positional('did', {
describe: 'The asset did',
type: 'string'
})
.option('password', {
type: 'string',
description: 'Password for receiving encrypted files'
}),
yargs
.positional('did', {
describe: 'The asset did',
type: 'string'
})
.option('password', {
type: 'string',
description: 'Password for receiving encrypted files'
}),
async (argv) => cmdHandler(orderAsset, argv)
)
.command(
Expand Down
9 changes: 2 additions & 7 deletions src/commands/assets/getAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,10 @@ export const getAsset = async (
)

if (password) {
const key = await nvm.assets.consumeProof(
agreementId,
did,
userAccount
)
const key = await nvm.assets.consumeProof(agreementId, did, userAccount)
console.log(`Got password ${Buffer.from(key as any, 'hex').toString()}`)

} else {
const path = await nvm.assets.consume(
const path = await nvm.assets.consume(
agreementId,
did,
userAccount,
Expand Down
5 changes: 3 additions & 2 deletions src/commands/assets/registerAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const registerAsset = async (
config: ConfigEntry,
logger: Logger
): Promise<number> => {
const { verbose, network, account, metadata, assetType, password, encrypt } = argv
const { verbose, network, account, metadata, assetType, password, encrypt } =
argv
const token = await loadToken(nvm, config, verbose)

if (verbose) {
Expand Down Expand Up @@ -87,7 +88,7 @@ export const registerAsset = async (
ddoMetadata.additionalInformation = {
poseidonHash: keytransfer.hashKey(Buffer.from(password)),
providerKey,
links: argv.urls.map((url:string) => ({name: 'public url', url}))
links: argv.urls.map((url: string) => ({ name: 'public url', url }))
}
}
if (assetType === 'algorithm') {
Expand Down
8 changes: 5 additions & 3 deletions src/commands/utils/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ export const decryptFile = async (
const { file, password } = argv
const encrypted = fs.readFileSync(file).toString('binary')

const salt = Buffer.from(encrypted.substring(8,16), 'binary')
const salt = Buffer.from(encrypted.substring(8, 16), 'binary')

const keydata = crypto.pbkdf2Sync(password, salt, 10000, 48, 'sha256').toString('binary')
const keydata = crypto
.pbkdf2Sync(password, salt, 10000, 48, 'sha256')
.toString('binary')
const key = Buffer.from(keydata.substring(0, 32), 'binary')
const iv = Buffer.from(keydata.substring(32, 48), 'binary')
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv)

let decrypted = decipher.update(encrypted.substring(16), 'binary', 'binary')
decrypted += decipher.final()

console.log(decrypted)

return 0
Expand Down
5 changes: 2 additions & 3 deletions src/commands/utils/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ export const uploadFile = async (
): Promise<number> => {
const { file, encrypt } = argv
const stream = fs.createReadStream(file)
const {url, password} = await nvm.files.uploadFilecoin(stream, encrypt)
const { url, password } = await nvm.files.uploadFilecoin(stream, encrypt)
console.log(`URL: ${url}`)
if (password) {
console.log(`Password: ${password}`)
console.log(`Password: ${password}`)
}
return 0
}

3 changes: 1 addition & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ export const config: CliConfig = {
spree: {
nvm: {
faucetUri: process.env.FAUCET_URL || 'http://localhost:3001',
metadataUri:
process.env.METADATA_URL || 'http://172.17.0.1:5000',
metadataUri: process.env.METADATA_URL || 'http://172.17.0.1:5000',
gatewayUri: process.env.GATEWAY_URL || 'http://localhost:8030',
gatewayAddress:
process.env.GATEWAY_ADDRESS ||
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/StdoutParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export const parseDIDFromNewAsset = (stdout: string): string => {
export const parseUrlAndPassword = (stdout: string): any => {
const parts = commandRegex.utils.upload.exec(stdout)
if (parts != null) {
return {url: parts[1], password: parts[2]}
return { url: parts[1], password: parts[2] }
}
return {url: '', password: ''}
return { url: '', password: '' }
}

export const parsePasswordFromOrder = (stdout: string): any => {
Expand Down
9 changes: 3 additions & 6 deletions test/integration-tests/AssetsEncrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,22 @@ describe('Assets e2e Testing', () => {

beforeAll(async () => {
console.log('pwd', execSync('pwd', execOpts).toString())

console.log(`Funding account: ${execOpts.accounts[0]}`)
const fundCommand = `${baseCommands.accounts.fund} "${execOpts.accounts[0]}" --token erc20`
console.debug(`COMMAND: ${fundCommand}`)

const stdout = execSync(fundCommand, execOpts)
console.log(stdout.toString())

})

test('Upload a file', async () => {
const uploadCommand = `${baseCommands.utils.upload} --encrypt --account "${execOpts.accounts[0]}" README.md`
console.debug(`COMMAND: ${uploadCommand}`)

const uploadStdout = execSync(uploadCommand, execOpts)
const res = parseUrlAndPassword(uploadStdout)
url = res.url
password = res.password

;({ url, password } = parseUrlAndPassword(uploadStdout))
})

test('Registering a new dataset and resolve the DID', async () => {
Expand Down Expand Up @@ -61,6 +59,5 @@ describe('Assets e2e Testing', () => {
const pass = parsePasswordFromOrder(getStdout)
console.log(`Password: ${pass}`)
expect(pass).toEqual(password)

})
})
Loading

0 comments on commit 66241f6

Please sign in to comment.