Skip to content

Commit

Permalink
Merge pull request #666 from nevermined-io/fix/key_decrypt
Browse files Browse the repository at this point in the history
fix: fixing key decryption
  • Loading branch information
aaitor authored May 14, 2024
2 parents cdc9421 + bbd055c commit 3aa7a73
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 25 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 2 May 2022
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- Adding v0.19.21 Changelog updates [`c93cdc5`](https://github.com/nevermined-io/sdk-js/commit/c93cdc55f139a43db4130ccb0f80924d2645a931)

#### [v0.19.21](https://github.com/nevermined-io/sdk-js/compare/v0.19.20...v0.19.21)
Expand Down Expand Up @@ -2192,7 +2192,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

> 18 January 2022
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- [wip] Create agreement+pay in one transaction [`#183`](https://github.com/nevermined-io/sdk-js/pull/183)
- Adapting to contracts `v1.3.3` [`#177`](https://github.com/nevermined-io/sdk-js/pull/177)
- Adding v0.17.2 Changelog updates [`5eddda4`](https://github.com/nevermined-io/sdk-js/commit/5eddda43954e013e6e6f7344c9a877d801aacb5c)
Expand Down Expand Up @@ -2698,7 +2698,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- Feature/sync develop [`#268`](https://github.com/nevermined-io/sdk-js/pull/268)
- Quick fix for non-eip1559 networks [`#266`](https://github.com/nevermined-io/sdk-js/pull/266)
- integrate Permissions and refactor search query [`#264`](https://github.com/nevermined-io/sdk-js/pull/264)
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
- Get the keeper version from the artifacts instead of package version [`#244`](https://github.com/nevermined-io/sdk-js/pull/244)
- replace `metadata-api` url by `marketplace-api` and sort type [`#243`](https://github.com/nevermined-io/sdk-js/pull/243)
- fixing issues with BigNumbers [`#246`](https://github.com/nevermined-io/sdk-js/pull/246)
Expand Down Expand Up @@ -2737,7 +2737,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
- Feature/190 add did to sec markets [`#191`](https://github.com/nevermined-io/sdk-js/pull/191)
- Adding utility methods for getting nft token uri [`#189`](https://github.com/nevermined-io/sdk-js/pull/189)
- Lint ... [`#187`](https://github.com/nevermined-io/sdk-js/pull/187)
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
- [wip] Create agreement+pay in one transaction [`#183`](https://github.com/nevermined-io/sdk-js/pull/183)
- Adapting to contracts `v1.3.3` [`#177`](https://github.com/nevermined-io/sdk-js/pull/177)
- Correct typo in route [`#184`](https://github.com/nevermined-io/sdk-js/pull/184)
Expand Down
31 changes: 31 additions & 0 deletions integration/nevermined/NVMApiKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ describe('Nevermined API Key', () => {
providerAddress = provider.getId()
providerPublicKey = provider.getAccountSigner().publicKey
providerPrivateKey = provider.getAccountSigner().getHdKey().privateKey

// Uncomment to see the keys
// console.log('Provider Public Key:', providerPublicKey)
// console.log('Provider Private Key:', providerPrivateKey)
// console.log('Private Key encoded: ', Buffer.from(providerPrivateKey, 'hex'))
})

it('I can compress and decompress', async () => {
Expand Down Expand Up @@ -154,5 +159,31 @@ describe('Nevermined API Key', () => {
assert.isDefined(error)
}
})

it('As a user I can generate a NVM API Key for the NODE', async () => {
const address = '0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0'
const publicKey =
'0x04d793eb43ef7d191bf64f127c9f1a2c9037406d72706d3be7dc564fb9a9f08f21156b32d1ee3afbe64cc9f676f6facffac1377f7804daf932d3b8aa04fdeb0630'
const privateKey = '0x9bf5d7e4978ed5206f760e6daded34d657572bd49fa5b3fe885679329fb16b16'

encryptedNvmApiKey = await nevermined.utils.jwt.generateEncryptedNeverminedApiKey(
user,
zeroDevSessionKey,
marketplaceAuthToken,
address,
publicKey,
)
assert.isDefined(encryptedNvmApiKey)
console.log('NVM API Key valid for the Node:', encryptedNvmApiKey)
console.log('JWT Compressed size:', encryptedNvmApiKey.length)

const jwt = await nevermined.utils.jwt.decryptAndDecodeNeverminedApiKey(
encryptedNvmApiKey,
privateKey,
)
assert.isDefined(jwt)

assert.isTrue(nevermined.utils.jwt.isNeverminedApiKeyValid(jwt))
})
})
})
1 change: 1 addition & 0 deletions integration/nevermined/NVMAppAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ describe('NVM App API', () => {
signerAddress = defaultSigner.getAddress()
console.log(`Account address: ${signerAddress}`)
await nvmApp.connect(defaultSigner)
nvmApp.assetProviders.push(publisher.getId())

assert.isTrue(nvmApp.isWeb3Connected())

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nevermined-io/sdk",
"version": "3.0.7",
"version": "3.0.8",
"description": "Javascript SDK for connecting with Nevermined Data Platform ",
"main": "./dist/node/sdk.js",
"typings": "./dist/node/sdk.d.ts",
Expand Down
8 changes: 5 additions & 3 deletions src/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { URL } from 'whatwg-url'
import { v4 } from 'uuid'
import { SearchQuery } from '../types'
import { encrypt, decrypt } from 'eccrypto'
import { noZeroX } from '../utils/ConversionTypeHelpers'

export const buildQuery = (url: string, query?: SearchQuery) => {
const fullUrl = new URL(url)
Expand Down Expand Up @@ -71,15 +72,16 @@ export function urlSafeBase64Decode(input: string): Uint8Array {
}

export async function encryptMessage(message: string, receiverPublicKey: string) {
const publicKeyBuffer = Buffer.from(receiverPublicKey.slice(2), 'hex')
const publicKeyBuffer = Buffer.from(noZeroX(receiverPublicKey), 'hex')
const messageBuffer = Buffer.from(message)
const ecies = await encrypt(publicKeyBuffer, messageBuffer)
return serializeECIES(ecies)
}

export async function decryptMessage(encryptedMessage: string, privateKey: string) {
export async function decryptMessage(encryptedMessage: string, privateKey: string | any) {
const ecies = deserializeECIES(encryptedMessage)
const decrypted = await decrypt(Buffer.from(privateKey, 'hex'), ecies)
const pk = typeof privateKey === 'string' ? noZeroX(privateKey) : privateKey
const decrypted = await decrypt(Buffer.from(pk, 'hex'), ecies)
return Buffer.from(decrypted, 'hex').toString()
}

Expand Down
22 changes: 6 additions & 16 deletions src/nevermined/NvmApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class NvmApp {
private fullSDK: Nevermined | undefined
// private useZeroDevSigner: boolean = false
private zeroDevSignerAccount: SmartAccountSigner<'custom', `0x${string}`> | undefined
private assetProviders: string[] = []
public assetProviders: string[] = []
private loginCredentials: string | undefined
private subscriptionNFTContractAddress: string | undefined
private networkFeeReceiver: string | undefined
Expand Down Expand Up @@ -157,6 +157,11 @@ export class NvmApp {
this.fullSDK = await Nevermined.getInstance(config ? config : this.configNVM, ops)

if (config && config.zeroDevProjectId) {
// const signer = this.fullSDK.accounts.getAccount(account as string)
// const smartAccountSigner = await providerToSmartAccountSigner(config.web3Provider, {
// signerAddress: signer.getAddress(),
// })

const kernelClient = await createKernelClient(
account,
config.chainId!,
Expand Down Expand Up @@ -285,21 +290,6 @@ export class NvmApp {
return { receiver: this.networkFeeReceiver, fee: this.networkFee }
}

/**
* Gets the asset providers that are associated to the new assets registered.
* @returns An array of asset providers.
*/
public get getAssetProviders(): string[] {
return this.assetProviders
}

/**
* Sets the asset providers for the new assets registered.
*/
public set setAssetProviders(providers: string[]) {
this.assetProviders = providers
}

/**
* Creates a time-based subscription for a given asset.
*
Expand Down
7 changes: 6 additions & 1 deletion src/nevermined/utils/JwtUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ export class JwtUtils extends Instantiable {
const issuerAddress = getChecksumAddress(issuerAccount.getId())
const sub = getChecksumAddress(receiverAddress)

const eip712Data = {
message: 'Sign this message to generate the API Key',
chainId,
}

const params = {
iss: issuerAddress,
aud: chainId.toString(),
Expand All @@ -394,7 +399,7 @@ export class JwtUtils extends Instantiable {
.setProtectedHeader({ alg: 'ES256K' })
.setIssuedAt()
.setExpirationTime(expirationTime)
.ethSign(this.nevermined.utils.signature, issuerAccount)
.ethSign(this.nevermined.utils.signature, issuerAccount, eip712Data)
return JwtUtils.createCompressedJwt(signed)
}

Expand Down

0 comments on commit 3aa7a73

Please sign in to comment.