Skip to content

Commit

Permalink
Standardize providers (#20)
Browse files Browse the repository at this point in the history
* update packages and add TODO
* update signMessage method and add TODOs for next steps
  • Loading branch information
CiprianDraghici authored Sep 19, 2024
1 parent b89cc1e commit 80c0df2
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 194 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ out
npm-debug.log*
yarn-debug.log*
yarn-error.log*

.yarnrc
coverage
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"dependencies": {
"@lifeomic/axios-fetch": "3.0.1",
"@multiversx/sdk-extension-provider": "3.0.0",
"@multiversx/sdk-extension-provider": "4.0.0-alpha.0",
"@multiversx/sdk-hw-provider": "6.4.0",
"@multiversx/sdk-metamask-provider": "0.0.7",
"@multiversx/sdk-native-auth-client": "^1.0.8",
Expand All @@ -47,7 +47,7 @@
"peerDependencies": {
"@multiversx/sdk-core": ">= 13.0.0",
"@multiversx/sdk-dapp-utils": ">= 0.1.0",
"@multiversx/sdk-web-wallet-cross-window-provider": ">= 1.0.0",
"@multiversx/sdk-web-wallet-cross-window-provider": ">= 2.0.0-alpha.1",
"axios": ">=1.6.5",
"bignumber.js": "9.x"
},
Expand All @@ -58,7 +58,7 @@
"@types/lodash": "4.17.4",
"@multiversx/sdk-core": ">= 13.0.0",
"@multiversx/sdk-dapp-utils": ">= 0.1.0",
"@multiversx/sdk-web-wallet-cross-window-provider": ">= 1.0.0",
"@multiversx/sdk-web-wallet-cross-window-provider": ">= 2.0.0-alpha.1",
"@swc/core": "^1.4.17",
"@swc/jest": "^0.2.36",
"@types/node": "20.12.8",
Expand Down
1 change: 1 addition & 0 deletions src/core/methods/login/helpers/extractAccountFromToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function extractAccountFromToken({
}

if (accountDetails.account) {
// TODO remove this as is already done before this function is called
loginAction({
address: accountDetails.address,
providerType: provider.getType()
Expand Down
13 changes: 3 additions & 10 deletions src/core/methods/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import { registerWebsocketListener } from '../initApp/websocket/registerWebsocke

async function loginWithoutNativeToken(provider: IProvider) {
await provider.login?.({
// TODO remove callbackUrl when the provider will be standardized
callbackUrl: getCallbackUrl()
});

// TODO update this when the provider will be standardized
const address = await provider.getAddress?.();

if (!address) {
Expand All @@ -49,19 +51,10 @@ async function loginWithNativeToken(
noCache: true
});

const loginResult = await provider.login?.({
callbackUrl: getCallbackUrl(),
const { address, signature, ...loginResult } = await provider.login?.({
token: loginToken
});

const address = provider.getAddress
? // TODO check why on the second login the address is fetched asynchronously (looks like the crosswindow provider has getAddress as an async function)
await provider.getAddress()
: loginResult?.address;
const signature = provider.getTokenLoginSignature
? provider.getTokenLoginSignature()
: loginResult?.signature;

if (!address) {
console.warn('Login cancelled.');
return null;
Expand Down
16 changes: 9 additions & 7 deletions src/core/methods/signMessage/signMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SignableMessage, Address } from '@multiversx/sdk-core';
import { Message, Address } from '@multiversx/sdk-core';
import { getAccountProvider } from 'core/providers';
import { getProviderType } from 'core/providers/helpers/utils';
import { CrossWindowProvider } from 'lib/sdkWebWalletCrossWindowProvider';
Expand All @@ -8,7 +8,7 @@ import { addOriginToLocationPath } from 'utils/window/addOriginToLocationPath';
import { ProviderTypeEnum } from 'core/providers/types/providerFactory.types';

export interface SignMessageType {
message: string;
message: Message;
callbackRoute?: string;
options?: {
hasConsentPopup?: boolean;
Expand All @@ -20,15 +20,15 @@ export const signMessage = async ({
message,
callbackRoute,
options
}: SignMessageType): Promise<Nullable<SignableMessage>> => {
}: SignMessageType): Promise<Nullable<Message>> => {
const address = getAddress();
const provider = getAccountProvider();
const providerType = getProviderType(provider);

const callbackUrl = addOriginToLocationPath(callbackRoute);
const signableMessage = new SignableMessage({
const messageToSign = new Message({
address: new Address(address),
message: Buffer.from(message, 'ascii')
data: message.data
});

if (
Expand All @@ -40,9 +40,11 @@ export const signMessage = async ({
);
}

const signedMessage = await provider.signMessage(signableMessage, {
// TODO upgrade sdk-dapp-utils to use Message as input for signMessage method and remove the cast
const signedMessage = await provider.signMessage(messageToSign as any, {
callbackUrl: encodeURIComponent(callbackUrl)
});

return signedMessage;
// TODO upgrade sdk-dapp-utils to return Message instead of SignableMessage and remove the cast
return signedMessage as Nullable<Message>;
};
8 changes: 0 additions & 8 deletions src/core/providers/ProviderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ export class ProviderFactory {
const provider = await this.getExtensionProvider();
createdProvider = provider as unknown as IProvider;

createdProvider.getAddress = () => {
return Promise.resolve(provider.account.address);
};

createdProvider.getTokenLoginSignature = () => {
return provider.account.signature;
};

createdProvider.getType = () => {
return ProviderTypeEnum.extension;
};
Expand Down
2 changes: 1 addition & 1 deletion src/store/actions/transactions/transactionsActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const updateSignedTransactionStatus = (
return {
...(serverTransaction ?? {}),
...transaction,
status: status as TransactionServerStatusesEnum, // TODO: @CiprianDraghici is this correct?s
status: status as TransactionServerStatusesEnum,
errorMessage,
inTransit
};
Expand Down
Loading

0 comments on commit 80c0df2

Please sign in to comment.