Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"babel-jest": "^29.7.0",
"brotli-size": "^4.0.0",
"chalk": "~4.1.2",
"ejs": "^3.1.10",
"enquirer": "^2.3.6",
"esbuild": "0.25.2",
"eslint": "^9.24.0",
Expand Down Expand Up @@ -73,8 +74,7 @@
"tsc-watch": "^4.2.9",
"tslib": "^2.3.1",
"typescript": "~5.8.2",
"wsrun": "^5.2.1",
"zombi": "^3.3.0"
"wsrun": "^5.2.1"
},
"workspaces": {
"packages": [
Expand Down
4 changes: 4 additions & 0 deletions packages/@magic-ext/aptos/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const config: Config.InitialOptions = {
'\\.(ts|tsx)$': 'ts-jest',
},
coveragePathIgnorePatterns: ['index.cdn.ts', 'index.native.ts'],
moduleNameMapper: {
'^@aptos-labs/wallet-adapter-core$': '<rootDir>/test/__mocks__/@aptos-labs/wallet-adapter-core.ts',
'^@aptos-labs/wallet-standard$': '<rootDir>/test/__mocks__/@aptos-labs/wallet-standard.ts',
},
};

export default config;
8 changes: 4 additions & 4 deletions packages/@magic-ext/aptos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
]
},
"devDependencies": {
"@aptos-labs/wallet-adapter-core": "^2.2.0",
"@aptos-labs/wallet-adapter-core": "^7.10.1",
"@magic-sdk/provider": "^31.2.0",
"aptos": "^1.8.5"
"aptos": "^1.22.1"
},
"peerDependencies": {
"@aptos-labs/wallet-adapter-core": "^2.2.0",
"aptos": "^1.8.5"
"@aptos-labs/wallet-adapter-core": "^7.10.1",
"aptos": "^1.22.1"
}
}
12 changes: 5 additions & 7 deletions packages/@magic-ext/aptos/src/MagicAptosWallet.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import {
AccountInfo,
AdapterPlugin,
NetworkInfo,
SignMessagePayload,
SignMessageResponse,
WalletInfo,
WalletReadyState,
} from '@aptos-labs/wallet-adapter-core';
import { AptosSignMessageInput, AptosSignMessageOutput, AptosWallet } from '@aptos-labs/wallet-standard';
import { TxnBuilderTypes, Types } from 'aptos';
import { InstanceWithExtensions, SDKBase } from '@magic-sdk/provider';
import { AptosExtension } from '.';
import { APTOS_NETWORKS, APTOS_NODE_URLS, APTOS_WALLET_NAME, ICON_BASE64 } from './constants';
import { MagicAptosWalletConfig } from './type';

export class MagicAptosWallet implements AdapterPlugin {
export class MagicAptosWallet implements Partial<AptosWallet> {
readonly name = APTOS_WALLET_NAME;
readonly url = 'https://magic.link/';
readonly icon = ICON_BASE64;
Expand All @@ -23,7 +21,7 @@ export class MagicAptosWallet implements AdapterPlugin {
provider: InstanceWithExtensions<SDKBase, [AptosExtension]> | undefined;
config?: MagicAptosWalletConfig;

readyState?: WalletReadyState = WalletReadyState.Loadable;
readyState?: WalletReadyState = WalletReadyState.Installed;

private accountInfo: AccountInfo | null;

Expand Down Expand Up @@ -106,7 +104,7 @@ export class MagicAptosWallet implements AdapterPlugin {
return this.provider.aptos.signAndSubmitBCSTransaction(accountInfo.address, transaction);
}

async signMessage(message: SignMessagePayload): Promise<SignMessageResponse> {
async signMessage(message: AptosSignMessageInput): Promise<AptosSignMessageOutput> {
if (!this.provider) {
throw new Error('Provider is not defined');
}
Expand All @@ -115,7 +113,7 @@ export class MagicAptosWallet implements AdapterPlugin {
return this.provider.aptos.signMessage(accountInfo.address, message);
}

async signMessageAndVerify(message: SignMessagePayload): Promise<boolean> {
async signMessageAndVerify(message: AptosSignMessageInput): Promise<boolean> {
if (!this.provider) {
throw new Error('Provider is not defined');
}
Expand Down
9 changes: 5 additions & 4 deletions packages/@magic-ext/aptos/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NetworkName, WalletName } from '@aptos-labs/wallet-adapter-core';
import { NetworkName } from '@aptos-labs/wallet-adapter-core';

export const APTOS_WALLET_NAME = 'Magic' as WalletName<'Magic'>;
export const APTOS_WALLET_NAME = 'Magic' as const;

export const APTOS_NODE_URLS = {
MAINNET: 'https://fullnode.mainnet.aptoslabs.com',
Expand All @@ -11,16 +11,17 @@ export const APTOS_NODE_URLS = {
export const APTOS_NETWORKS = {
[APTOS_NODE_URLS.MAINNET]: {
name: NetworkName.Mainnet,
chainId: '1',
chainId: 1,
url: APTOS_NODE_URLS.MAINNET,
},
[APTOS_NODE_URLS.TESTNET]: {
name: NetworkName.Testnet,
chainId: '2',
chainId: 2,
url: APTOS_NODE_URLS.TESTNET,
},
[APTOS_NODE_URLS.DEVNET]: {
name: NetworkName.Devnet,
chainId: 34,
url: APTOS_NODE_URLS.DEVNET,
},
};
Expand Down
10 changes: 5 additions & 5 deletions packages/@magic-ext/aptos/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MultichainExtension } from '@magic-sdk/provider';

// @ts-ignore
import { AptosClient, BCS, TxnBuilderTypes, Types, getAddressFromAccountOrAddress } from 'aptos';
import { AccountInfo, SignMessagePayload, SignMessageResponse } from '@aptos-labs/wallet-adapter-core';
import { AccountInfo } from '@aptos-labs/wallet-adapter-core';
import { AptosSignMessageInput, AptosSignMessageOutput } from '@aptos-labs/wallet-standard';
import { AptosConfig, AptosPayloadMethod } from './type';
import { APTOS_PAYLOAD_TYPE } from './constants';

Expand Down Expand Up @@ -106,8 +106,8 @@ export class AptosExtension extends MultichainExtension<'aptos'> {
);
};

signMessage = async (address: string, message: SignMessagePayload): Promise<SignMessageResponse> => {
return this.request<SignMessageResponse>(
signMessage = async (address: string, message: AptosSignMessageInput): Promise<AptosSignMessageOutput> => {
return this.request<AptosSignMessageOutput>(
this.utils.createJsonRpcRequestPayload(AptosPayloadMethod.AptosSignMessage, [
{
address,
Expand All @@ -117,7 +117,7 @@ export class AptosExtension extends MultichainExtension<'aptos'> {
);
};

signMessageAndVerify = async (address: string, message: SignMessagePayload): Promise<boolean> => {
signMessageAndVerify = async (address: string, message: AptosSignMessageInput): Promise<boolean> => {
return this.request<boolean>(
this.utils.createJsonRpcRequestPayload(AptosPayloadMethod.AptosSignMessageAndVerify, [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Mock for @aptos-labs/wallet-adapter-core
export enum NetworkName {
Mainnet = 'mainnet',
Testnet = 'testnet',
Devnet = 'devnet',
}

export enum WalletReadyState {
Installed = 'Installed',
NotDetected = 'NotDetected',
Loadable = 'Loadable',
Unsupported = 'Unsupported',
}

export interface AccountInfo {
address: string;
publicKey: string | string[];
minKeysRequired?: number;
ansName?: string | null;
}

export interface NetworkInfo {
name: NetworkName;
chainId?: number;
url?: string;
}

export interface WalletInfo {
name: string;
icon: string;
url: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Mock for @aptos-labs/wallet-standard
export interface AptosSignMessageInput {
address?: boolean;
application?: boolean;
chainId?: boolean;
message: string;
nonce: string;
}

export interface AptosSignMessageOutput {
address?: string;
application?: string;
chainId?: number;
fullMessage: string;
message: string;
nonce: string;
prefix: string;
signature: string;
bitmap?: Uint8Array;
}

export interface AptosWallet {
readonly name: string;
readonly url: string;
readonly icon: string;
}
34 changes: 34 additions & 0 deletions packages/@magic-ext/aptos/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
// NOTE: This module is automatically included at the top of each test file.
import { mockConsole } from '../../../../scripts/utils/mock-console';
import { TextEncoder, TextDecoder } from 'util';

// Polyfill TextEncoder/TextDecoder for @aptos-labs/ts-sdk
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder as typeof global.TextDecoder;

// Mock Aptos API responses
const mockAptosApiResponse = (url: string) => {
// Mock account response
if (url.includes('/accounts/')) {
return {
sequence_number: '0',
authentication_key: '0x8293d5e05544c6e53c47fc19ae071c26a60e0ccbd8a12eb5b2c9d348c85227b6',
};
}
// Mock modules response
if (url.includes('/modules')) {
return [];
}
// Default response
return {};
};

// Polyfill fetch for jsdom environment
global.fetch = jest.fn((url: string) =>
Promise.resolve({
ok: true,
status: 200,
headers: new Headers({ 'content-type': 'application/json' }),
json: () => Promise.resolve(mockAptosApiResponse(url)),
text: () => Promise.resolve(JSON.stringify(mockAptosApiResponse(url))),
} as Response),
);

beforeEach(() => {
mockConsole();
jest.clearAllMocks();
});
31 changes: 30 additions & 1 deletion packages/@magic-ext/aptos/test/spec/aptos-extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@ import { AptosExtension } from '../../src';
import { AptosPayloadMethod } from '../../src/type';
import { TextEncoder, TextDecoder } from 'util';

// Mock AptosClient to avoid network calls
const mockRawTransaction = new TxnBuilderTypes.RawTransaction(
TxnBuilderTypes.AccountAddress.fromHex('0x8293d5e05544c6e53c47fc19ae071c26a60e0ccbd8a12eb5b2c9d348c85227b6'),
BigInt(0),
new TxnBuilderTypes.TransactionPayloadEntryFunction(
TxnBuilderTypes.EntryFunction.natural(
'0x1::coin',
'transfer',
[TxnBuilderTypes.StructTag.fromString('0x1::aptos_coin::AptosCoin')],
[],
),
),
BigInt(2000),
BigInt(1),
BigInt(Math.floor(Date.now() / 1000) + 600),
new TxnBuilderTypes.ChainId(2),
);

jest.mock('aptos', () => {
const actual = jest.requireActual('aptos');
return {
...actual,
AptosClient: jest.fn().mockImplementation(() => ({
generateTransaction: jest.fn().mockResolvedValue(mockRawTransaction),
generateRawTransaction: jest.fn().mockResolvedValue(mockRawTransaction),
})),
};
});

const APTOS_NODE_URL = 'https://fullnode.testnet.aptoslabs.com';

const SAMPLE_ADDRESS = '0x8293d5e05544c6e53c47fc19ae071c26a60e0ccbd8a12eb5b2c9d348c85227b6';
Expand Down Expand Up @@ -36,7 +65,7 @@ beforeAll(() => {
})

beforeEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});

test('Construct GetAccount request with `aptos_getAccount`', async () => {
Expand Down
Loading