Skip to content

Commit

Permalink
add standalone MS wallet support
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Jan 24, 2025
1 parent ce3016a commit 7cb15a0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
11 changes: 4 additions & 7 deletions src/massaStation/MassaStationAccount.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { getRequest, postRequest } from './RequestHandler';
import {
MASSA_STATION_URL,
MASSA_STATION_ACCOUNTS_URL,
} from './MassaStationWallet';
import { getMSWalletUrl, MASSA_STATION_URL } from './MassaStationWallet';
import {
argsToBase64,
base64ToByteArray,
Expand Down Expand Up @@ -117,7 +114,7 @@ export class MassaStationAccount implements Provider {
};

const res = await postRequest<MSAccountSignResp>(
`${MASSA_STATION_ACCOUNTS_URL}/${this.accountName}/signMessage`,
`${getMSWalletUrl()}accounts/${this.accountName}/signMessage`,
signData,
);

Expand Down Expand Up @@ -155,7 +152,7 @@ export class MassaStationAccount implements Provider {
};

const res = await postRequest<MSSendOperationResp>(
`${MASSA_STATION_ACCOUNTS_URL}/${this.accountName}/rolls`,
`${getMSWalletUrl()}accounts/${this.accountName}/rolls`,
body,
);

Expand Down Expand Up @@ -193,7 +190,7 @@ export class MassaStationAccount implements Provider {
};

const res = await postRequest<MSSendOperationResp>(
`${MASSA_STATION_ACCOUNTS_URL}/${this.accountName}/transfer`,
`${getMSWalletUrl()}accounts/${this.accountName}/transfer`,
body,
);

Expand Down
25 changes: 19 additions & 6 deletions src/massaStation/MassaStationWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ export const MASSA_STATION_URL = 'https://station.massa/';
/**
* The MassaStation accounts url
*/
export const MASSA_STATION_ACCOUNTS_URL = `${MASSA_STATION_URL}plugin/massa-labs/massa-wallet/api/accounts`;

export function getMSWalletUrl(): string {
try {
// This is a hack to detect that MS wallet is working in standalone mode
if (
typeof import.meta !== 'undefined' &&
(import.meta as any)?.env?.VITE_ENV

Check warning on line 30 in src/massaStation/MassaStationWallet.ts

View workflow job for this annotation

GitHub Actions / format

Unexpected any. Specify a different type

Check warning on line 30 in src/massaStation/MassaStationWallet.ts

View workflow job for this annotation

GitHub Actions / format / format

Unexpected any. Specify a different type
) {
return `http://localhost:8080${(import.meta as any).env.VITE_BASE_API}`;

Check warning on line 32 in src/massaStation/MassaStationWallet.ts

View workflow job for this annotation

GitHub Actions / format

Unexpected any. Specify a different type

Check warning on line 32 in src/massaStation/MassaStationWallet.ts

View workflow job for this annotation

GitHub Actions / format / format

Unexpected any. Specify a different type
}
} catch (e) {
// Ignore error and use default URL
}
return `${MASSA_STATION_URL}plugin/massa-labs/massa-wallet/api/`;
}
/**
* Events emitted by MassaStation
*/
Expand Down Expand Up @@ -51,7 +64,7 @@ export class MassaStationWallet implements Wallet {
}

public async accounts(): Promise<MassaStationAccount[]> {
const res = await getRequest<MSAccountsResp>(MASSA_STATION_ACCOUNTS_URL);
const res = await getRequest<MSAccountsResp>(getMSWalletUrl() + 'accounts');

if (res.isError) {
throw res.error;
Expand All @@ -69,7 +82,7 @@ export class MassaStationWallet implements Wallet {
publicKey: string,
privateKey: string,
): Promise<void> {
const res = await putRequest(MASSA_STATION_ACCOUNTS_URL, {
const res = await putRequest(getMSWalletUrl() + 'accounts', {
publicKey,
privateKey,
});
Expand All @@ -81,7 +94,7 @@ export class MassaStationWallet implements Wallet {
public async deleteAccount(address: string): Promise<void> {
// get all accounts and find the account to delete
const allAccounts = await getRequest<MSAccountsResp>(
MASSA_STATION_ACCOUNTS_URL,
getMSWalletUrl() + 'accounts',
);

if (allAccounts.isError) throw allAccounts.error;
Expand All @@ -95,7 +108,7 @@ export class MassaStationWallet implements Wallet {
}

const res = await deleteRequest<unknown>(
`${MASSA_STATION_ACCOUNTS_URL}/${accountToDelete.nickname}`,
`${getMSWalletUrl()}accounts/${accountToDelete.nickname}`,
);

if (res.isError) {
Expand All @@ -121,7 +134,7 @@ export class MassaStationWallet implements Wallet {
*/
public async generateNewAccount(name: string): Promise<MassaStationAccount> {
const response = await postRequest<MSAccount>(
MASSA_STATION_ACCOUNTS_URL + '/' + name,
getMSWalletUrl() + 'accounts/' + name,
{},
);

Expand Down

0 comments on commit 7cb15a0

Please sign in to comment.