Skip to content

Commit

Permalink
Merge pull request #718 from magiclabs/ariflo-PDEEXP-492-Add-new-gene…
Browse files Browse the repository at this point in the history
…ral-announcement-type

Add new "Product Announcement" Relayer Message Type
  • Loading branch information
Ariflo authored Feb 27, 2024
2 parents f7d3de5 + 12b71cc commit 249f937
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 117 deletions.
6 changes: 1 addition & 5 deletions packages/@magic-sdk/provider/src/core/sdk-exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,8 @@ export function createDeprecationWarning(options: {
}) {
const { method, removalVersions, useInstead } = options;

const preventDisruptionLanguage = 'to prevent disruption of the wallet service';

const removalVersion = removalVersions[SDKEnvironment.sdkName];
const useInsteadSuffix = useInstead
? ` Use \`${useInstead}\` instead ${useInstead.includes('Dedicated Wallet') ? preventDisruptionLanguage : ''}.`
: '';
const useInsteadSuffix = useInstead ? ` Use \`${useInstead}\` instead.` : '';
const message = `\`${method}\` will be removed from \`${SDKEnvironment.sdkName}\` in version \`${removalVersion}\`.${useInsteadSuffix}`;

return new MagicSDKWarning(SDKWarningCode.DeprecationNotice, message);
Expand Down
20 changes: 5 additions & 15 deletions packages/@magic-sdk/provider/src/core/view-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import {
JsonRpcRequestPayload,
MagicMessageEvent,
MagicMessageRequest,
SDKWarningCode,
} from '@magic-sdk/types';
import { JsonRpcResponse } from './json-rpc';
import { createPromise } from '../util/promise-tools';
import { getItem, setItem } from '../util/storage';
import { createJwt } from '../util/web-crypto';
import { SDKEnvironment } from './sdk-environment';
import { createDeprecationWarning, createModalNotReadyError } from './sdk-exceptions';
import { MagicSDKWarning, createModalNotReadyError } from './sdk-exceptions';
import {
clearDeviceShares,
encryptAndPersistDeviceShare,
Expand All @@ -34,13 +35,6 @@ interface StandardizedMagicRequest {
deviceShare?: string;
}

export const UniversalWalletRemovalVersions = {
'magic-sdk': 'v23.0.0',
'@magic-sdk/react-native': 'v14.0.0',
'@magic-sdk/react-native-bare': 'v24.0.0',
'@magic-sdk/react-native-expo': 'v24.0.0',
};

/**
* Get the originating payload from a batch request using the specified `id`.
*/
Expand Down Expand Up @@ -267,13 +261,9 @@ export abstract class ViewController {
this.showOverlay();
});

this.on(MagicIncomingWindowMessage.MAGIC_SEND_PRODUCT_TYPE, (event: MagicMessageEvent) => {
if (event.data.response.result.product_type === 'connect') {
createDeprecationWarning({
method: 'Usage of Universal Wallet API Keys',
removalVersions: UniversalWalletRemovalVersions,
useInstead: 'Dedicated Wallet API Key',
}).log();
this.on(MagicIncomingWindowMessage.MAGIC_SEND_PRODUCT_ANNOUNCEMENT, (event: MagicMessageEvent) => {
if (event.data.response.result.product_announcement) {
new MagicSDKWarning(SDKWarningCode.ProductAnnouncement, event.data.response.result.product_announcement).log();
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@magic-sdk/provider/test/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export const MSG_TYPES = (parameters = ENCODED_QUERY_PARAMS) => ({
MAGIC_HIDE_OVERLAY: `MAGIC_HIDE_OVERLAY-${parameters}`,
MAGIC_HANDLE_REQUEST: `MAGIC_HANDLE_REQUEST-${parameters}`,
MAGIC_HANDLE_EVENT: `MAGIC_HANDLE_EVENT-${parameters}`,
MAGIC_SEND_PRODUCT_TYPE: `MAGIC_SEND_PRODUCT_TYPE-${parameters}`,
MAGIC_SEND_PRODUCT_ANNOUNCEMENT: `MAGIC_SEND_PRODUCT_ANNOUNCEMENT-${parameters}`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,6 @@ test('Creates a `DEPRECATION_NOTICE` warning with `useInstead` suffix', async ()
warningAssertions(
warning,
'DEPRECATION_NOTICE',
'`test()` will be removed from `magic-sdk` in version `v999`. Use `test2()` instead .',
);
});

test('Creates a `DEPRECATION_NOTICE` warning with `useInstead` containing "Dedicated Wallet" suffix', async () => {
mockSDKEnvironmentConstant({ sdkName: 'magic-sdk' });

const { createDeprecationWarning } = require('../../../../src/core/sdk-exceptions');
const warning = createDeprecationWarning({
method: 'test()',
removalVersions: {
'magic-sdk': 'v999',
'@magic-sdk/react-native-bare': 'v888',
'@magic-sdk/react-native-expo': 'v777',
},
useInstead: 'Dedicated Wallet',
});

warningAssertions(
warning,
'DEPRECATION_NOTICE',
'`test()` will be removed from `magic-sdk` in version `v999`. Use `Dedicated Wallet` instead to prevent disruption of the wallet service.',
'`test()` will be removed from `magic-sdk` in version `v999`. Use `test2()` instead.',
);
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import browserEnv from '@ikscodes/browser-env';
import { createViewController } from '../../../factories';
import { MSG_TYPES } from '../../../constants';
import { createDeprecationWarning } from '../../../../src/core/sdk-exceptions';
import { UniversalWalletRemovalVersions } from '../../../../src/core/view-controller';
import { MagicSDKWarning } from '../../../../src/core/sdk-exceptions';

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

jest.mock('../../../../src/core/sdk-exceptions', () => ({
...jest.requireActual('../../../../src/core/sdk-exceptions'),
createDeprecationWarning: jest.fn(),
}));

test('Receive MAGIC_HIDE_OVERLAY, call `hideOverlay`', (done) => {
const overlay = createViewController('');
const hideOverlayStub = jest.fn();
Expand All @@ -39,31 +33,25 @@ test('Receive MAGIC_SHOW_OVERLAY, call `showOverlay`', (done) => {
}, 0);
});

test('Receive MAGIC_SEND_PRODUCT_TYPE with product_type "connect", call `createDeprecationWarning`', (done) => {
test('Receive MAGIC_SEND_PRODUCT_ANNOUNCEMENT, log product announcement', (done) => {
const overlay = createViewController('');
const productAnnouncement = 'New feature available!';
const logSpy = jest.spyOn(MagicSDKWarning.prototype, 'log');

// Cast the imported function to its mocked version
const mockCreateDeprecationWarning = createDeprecationWarning as jest.Mock;

// Mock the return value
mockCreateDeprecationWarning.mockReturnValue({
log: jest.fn(),
});

// Simulate posting the MAGIC_SEND_PRODUCT_ANNOUNCEMENT message with a product announcement
window.postMessage(
{
msgType: MSG_TYPES().MAGIC_SEND_PRODUCT_TYPE,
response: { result: { product_type: 'connect' } },
msgType: MSG_TYPES().MAGIC_SEND_PRODUCT_ANNOUNCEMENT,
response: {
result: {
product_announcement: productAnnouncement,
},
},
},
'*',
);

setTimeout(() => {
expect(mockCreateDeprecationWarning).toBeCalledWith({
method: 'Usage of Universal Wallet API Keys',
removalVersions: UniversalWalletRemovalVersions,
useInstead: 'Dedicated Wallet API Key',
});
expect(logSpy).toHaveBeenCalled();
done();
}, 0);
});
1 change: 1 addition & 0 deletions packages/@magic-sdk/types/src/core/exception-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum SDKWarningCode {
DuplicateIframe = 'DUPLICATE_IFRAME',
ReactNativeEndpointConfiguration = 'REACT_NATIVE_ENDPOINT_CONFIGURATION',
DeprecationNotice = 'DEPRECATION_NOTICE',
ProductAnnouncement = 'ANNOUNCEMENT',
}

export enum RPCErrorCode {
Expand Down
2 changes: 1 addition & 1 deletion packages/@magic-sdk/types/src/core/json-rpc-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface JsonRpcResponsePayload<ResultType = any> {
id: string | number | null;
result?: ResultType | null;
error?: JsonRpcError | null;
product_type?: 'magic' | 'connect';
product_announcement?: string | null;
}

export interface UserInfo {
Expand Down
2 changes: 1 addition & 1 deletion packages/@magic-sdk/types/src/core/message-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export enum MagicIncomingWindowMessage {
MAGIC_HIDE_OVERLAY = 'MAGIC_HIDE_OVERLAY',
MAGIC_HANDLE_EVENT = 'MAGIC_HANDLE_EVENT',
MAGIC_MG_BOX_SEND_RECEIPT = 'MAGIC_MG_BOX_SEND_RECEIPT',
MAGIC_SEND_PRODUCT_TYPE = 'MAGIC_SEND_PRODUCT_TYPE',
MAGIC_SEND_PRODUCT_ANNOUNCEMENT = 'MAGIC_SEND_PRODUCT_ANNOUNCEMENT',
}

export enum MagicOutgoingWindowMessage {
Expand Down
Loading

0 comments on commit 249f937

Please sign in to comment.