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

Add symmetric encryption api #520

Merged
merged 4 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions packages/@magic-sdk/provider/src/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ export class UserModule extends BaseModule {
return this.request<string | null>(requestPayload);
}

public encryptWithPrivateKey(message: string, opts = {}) {
const requestPayload = createJsonRpcRequestPayload('magic_auth_encrypt_v1', [{ message }]);
Ethella marked this conversation as resolved.
Show resolved Hide resolved
return this.request<string>(requestPayload);
}

public decryptWithPrivateKey(cipherText: string, opts = {}) {
const requestPayload = createJsonRpcRequestPayload('magic_auth_decrypt_v1', [{ cipherText }]);
Ethella marked this conversation as resolved.
Show resolved Hide resolved
return this.request<string>(requestPayload);
}

// Private members
private localForageKey = 'mc_active_wallet';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import browserEnv from '@ikscodes/browser-env';
import { createMagicSDK } from '../../../factories';

beforeEach(() => {
browserEnv.restore();
});

test('Encrypt `magic_auth_encrypt_v1`', async () => {
const magic = createMagicSDK();
magic.user.request = jest.fn();

const message = 'hello world';

magic.user.encryptWithPrivateKey(message);

const requestPayload = magic.user.request.mock.calls[0][0];
expect(requestPayload.method).toBe('magic_auth_encrypt_v1');
expect(requestPayload.params).toEqual([{ message }]);
});

test('Decrypt `magic_auth_decrypt_v1`', async () => {
const magic = createMagicSDK();
magic.user.request = jest.fn();

const cipherText = 'XJD/1238t';

magic.user.decryptWithPrivateKey(cipherText);

const requestPayload = magic.user.request.mock.calls[0][0];
expect(requestPayload.method).toBe('magic_auth_decrypt_v1');
expect(requestPayload.params).toEqual([{ cipherText }]);
});
88 changes: 44 additions & 44 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2731,15 +2731,15 @@ __metadata:
version: 0.0.0-use.local
resolution: "@magic-ext/algorand@workspace:packages/@magic-ext/algorand"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/aptos@workspace:packages/@magic-ext/aptos":
version: 0.0.0-use.local
resolution: "@magic-ext/aptos@workspace:packages/@magic-ext/aptos"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
aptos: ^1.8.5
peerDependencies:
aptos: ^1.8.5
Expand All @@ -2750,55 +2750,55 @@ __metadata:
version: 0.0.0-use.local
resolution: "@magic-ext/auth@workspace:packages/@magic-ext/auth"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/avalanche@workspace:packages/@magic-ext/avalanche":
version: 0.0.0-use.local
resolution: "@magic-ext/avalanche@workspace:packages/@magic-ext/avalanche"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/bitcoin@workspace:packages/@magic-ext/bitcoin":
version: 0.0.0-use.local
resolution: "@magic-ext/bitcoin@workspace:packages/@magic-ext/bitcoin"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/conflux@workspace:packages/@magic-ext/conflux":
version: 0.0.0-use.local
resolution: "@magic-ext/conflux@workspace:packages/@magic-ext/conflux"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/cosmos@workspace:packages/@magic-ext/cosmos":
version: 0.0.0-use.local
resolution: "@magic-ext/cosmos@workspace:packages/@magic-ext/cosmos"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/ed25519@workspace:packages/@magic-ext/ed25519":
version: 0.0.0-use.local
resolution: "@magic-ext/ed25519@workspace:packages/@magic-ext/ed25519"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/flow@workspace:packages/@magic-ext/flow":
version: 0.0.0-use.local
resolution: "@magic-ext/flow@workspace:packages/@magic-ext/flow"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
"@onflow/fcl": 0.0.41
"@onflow/types": 0.0.3
peerDependencies:
Expand All @@ -2811,34 +2811,34 @@ __metadata:
version: 0.0.0-use.local
resolution: "@magic-ext/harmony@workspace:packages/@magic-ext/harmony"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/icon@workspace:packages/@magic-ext/icon":
version: 0.0.0-use.local
resolution: "@magic-ext/icon@workspace:packages/@magic-ext/icon"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/near@workspace:packages/@magic-ext/near":
version: 0.0.0-use.local
resolution: "@magic-ext/near@workspace:packages/@magic-ext/near"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/oauth@^11.1.5, @magic-ext/oauth@workspace:packages/@magic-ext/oauth":
"@magic-ext/oauth@^11.1.6, @magic-ext/oauth@workspace:packages/@magic-ext/oauth":
version: 0.0.0-use.local
resolution: "@magic-ext/oauth@workspace:packages/@magic-ext/oauth"
dependencies:
"@magic-sdk/types": ^15.1.3
"@magic-sdk/types": ^15.1.4
"@types/crypto-js": ~3.1.47
crypto-js: ^3.3.0
magic-sdk: ^17.1.5
magic-sdk: ^17.1.6
languageName: unknown
linkType: soft

Expand All @@ -2854,15 +2854,15 @@ __metadata:
version: 0.0.0-use.local
resolution: "@magic-ext/polkadot@workspace:packages/@magic-ext/polkadot"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/react-native-bare-oauth@workspace:packages/@magic-ext/react-native-bare-oauth":
version: 0.0.0-use.local
resolution: "@magic-ext/react-native-bare-oauth@workspace:packages/@magic-ext/react-native-bare-oauth"
dependencies:
"@magic-sdk/react-native-bare": ^18.2.2
"@magic-sdk/react-native-bare": ^18.2.3
"@magic-sdk/types": ^10.0.1
"@types/crypto-js": ~3.1.47
crypto-js: ^3.3.0
Expand All @@ -2878,7 +2878,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@magic-ext/react-native-expo-oauth@workspace:packages/@magic-ext/react-native-expo-oauth"
dependencies:
"@magic-sdk/react-native-expo": ^18.2.2
"@magic-sdk/react-native-expo": ^18.2.3
"@magic-sdk/types": ^10.0.0
"@types/crypto-js": ~3.1.47
crypto-js: ^3.3.0
Expand All @@ -2893,56 +2893,56 @@ __metadata:
version: 0.0.0-use.local
resolution: "@magic-ext/solana@workspace:packages/@magic-ext/solana"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/taquito@workspace:packages/@magic-ext/taquito":
version: 0.0.0-use.local
resolution: "@magic-ext/taquito@workspace:packages/@magic-ext/taquito"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/terra@workspace:packages/@magic-ext/terra":
version: 0.0.0-use.local
resolution: "@magic-ext/terra@workspace:packages/@magic-ext/terra"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/tezos@workspace:packages/@magic-ext/tezos":
version: 0.0.0-use.local
resolution: "@magic-ext/tezos@workspace:packages/@magic-ext/tezos"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/webauthn@workspace:packages/@magic-ext/webauthn":
version: 0.0.0-use.local
resolution: "@magic-ext/webauthn@workspace:packages/@magic-ext/webauthn"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-ext/zilliqa@workspace:packages/@magic-ext/zilliqa":
version: 0.0.0-use.local
resolution: "@magic-ext/zilliqa@workspace:packages/@magic-ext/zilliqa"
dependencies:
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/commons": ^13.1.5
languageName: unknown
linkType: soft

"@magic-sdk/commons@^13.1.4, @magic-sdk/commons@workspace:packages/@magic-sdk/commons":
"@magic-sdk/commons@^13.1.5, @magic-sdk/commons@workspace:packages/@magic-sdk/commons":
version: 0.0.0-use.local
resolution: "@magic-sdk/commons@workspace:packages/@magic-sdk/commons"
dependencies:
"@magic-sdk/provider": ^17.1.4
"@magic-sdk/types": ^15.1.3
"@magic-sdk/provider": ^17.1.5
"@magic-sdk/types": ^15.1.4
peerDependencies:
"@magic-sdk/provider": ">=4.3.0"
"@magic-sdk/types": ">=3.1.1"
Expand All @@ -2966,17 +2966,17 @@ __metadata:
"@babel/core": ^7.9.6
"@babel/plugin-proposal-optional-chaining": ^7.9.0
"@babel/runtime": ^7.9.6
"@magic-ext/oauth": ^11.1.5
magic-sdk: ^17.1.5
"@magic-ext/oauth": ^11.1.6
magic-sdk: ^17.1.6
languageName: unknown
linkType: soft

"@magic-sdk/provider@^17.1.4, @magic-sdk/provider@workspace:packages/@magic-sdk/provider":
"@magic-sdk/provider@^17.1.5, @magic-sdk/provider@workspace:packages/@magic-sdk/provider":
version: 0.0.0-use.local
resolution: "@magic-sdk/provider@workspace:packages/@magic-sdk/provider"
dependencies:
"@babel/plugin-transform-modules-commonjs": ^7.9.6
"@magic-sdk/types": ^15.1.3
"@magic-sdk/types": ^15.1.4
"@peculiar/webcrypto": ^1.1.7
eventemitter3: ^4.0.4
localforage: ^1.7.4
Expand All @@ -3001,17 +3001,17 @@ __metadata:
languageName: node
linkType: hard

"@magic-sdk/react-native-bare@^18.2.2, @magic-sdk/react-native-bare@workspace:packages/@magic-sdk/react-native-bare":
"@magic-sdk/react-native-bare@^18.2.3, @magic-sdk/react-native-bare@workspace:packages/@magic-sdk/react-native-bare":
version: 0.0.0-use.local
resolution: "@magic-sdk/react-native-bare@workspace:packages/@magic-sdk/react-native-bare"
dependencies:
"@aveq-research/localforage-asyncstorage-driver": ^3.0.1
"@babel/core": ^7.15.0
"@babel/plugin-transform-flow-strip-types": ^7.14.5
"@babel/runtime": ~7.10.4
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/provider": ^17.1.4
"@magic-sdk/types": ^15.1.3
"@magic-sdk/commons": ^13.1.5
"@magic-sdk/provider": ^17.1.5
"@magic-sdk/types": ^15.1.4
"@react-native-async-storage/async-storage": ^1.15.5
"@types/lodash": ^4.14.158
buffer: ~5.6.0
Expand All @@ -3037,17 +3037,17 @@ __metadata:
languageName: unknown
linkType: soft

"@magic-sdk/react-native-expo@^18.2.2, @magic-sdk/react-native-expo@workspace:packages/@magic-sdk/react-native-expo":
"@magic-sdk/react-native-expo@^18.2.3, @magic-sdk/react-native-expo@workspace:packages/@magic-sdk/react-native-expo":
version: 0.0.0-use.local
resolution: "@magic-sdk/react-native-expo@workspace:packages/@magic-sdk/react-native-expo"
dependencies:
"@aveq-research/localforage-asyncstorage-driver": ^3.0.1
"@babel/core": ^7.15.0
"@babel/plugin-transform-flow-strip-types": ^7.14.5
"@babel/runtime": ~7.10.4
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/provider": ^17.1.4
"@magic-sdk/types": ^15.1.3
"@magic-sdk/commons": ^13.1.5
"@magic-sdk/provider": ^17.1.5
"@magic-sdk/types": ^15.1.4
"@react-native-async-storage/async-storage": ^1.15.5
"@types/lodash": ^4.14.158
buffer: ~5.6.0
Expand All @@ -3073,7 +3073,7 @@ __metadata:
languageName: unknown
linkType: soft

"@magic-sdk/types@^15.1.3, @magic-sdk/types@workspace:packages/@magic-sdk/types":
"@magic-sdk/types@^15.1.4, @magic-sdk/types@workspace:packages/@magic-sdk/types":
version: 0.0.0-use.local
resolution: "@magic-sdk/types@workspace:packages/@magic-sdk/types"
languageName: unknown
Expand Down Expand Up @@ -12402,16 +12402,16 @@ fsevents@^2.3.2:
languageName: unknown
linkType: soft

"magic-sdk@^17.1.5, magic-sdk@workspace:packages/magic-sdk":
"magic-sdk@^17.1.6, magic-sdk@workspace:packages/magic-sdk":
version: 0.0.0-use.local
resolution: "magic-sdk@workspace:packages/magic-sdk"
dependencies:
"@babel/core": ^7.9.6
"@babel/plugin-proposal-optional-chaining": ^7.9.0
"@babel/runtime": ^7.9.6
"@magic-sdk/commons": ^13.1.4
"@magic-sdk/provider": ^17.1.4
"@magic-sdk/types": ^15.1.3
"@magic-sdk/commons": ^13.1.5
"@magic-sdk/provider": ^17.1.5
"@magic-sdk/types": ^15.1.4
localforage: ^1.7.4
localforage-driver-memory: ^1.0.5
languageName: unknown
Expand Down