Skip to content

Commit

Permalink
fix: ton provider (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteZhang1024 authored Oct 31, 2024
1 parent d79843b commit 304d66e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/versionInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

const version = '2.1.10';
const version = '2.1.11';
const versionBuild = '2020-0101-1';

export default {
Expand Down
4 changes: 4 additions & 0 deletions packages/example/components/chains/ton/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export function Example() {
</div>
{userFriendlyAddress && <p>userFriendlyAddress: {userFriendlyAddress}</p>}
{rawAddress && <p>rawAddress: {rawAddress}</p>}
{wallet?.account && <p>Wallet Account PublicKey: {wallet?.account.publicKey}</p>}
{wallet?.account && <p>Wallet Account Chain: {wallet?.account.chain}</p>}
{wallet?.account && <p>Wallet Account WalletStateInit: {wallet?.account.walletStateInit}</p>}

{wallet?.device?.appName && <p>Wallet AppName: {wallet?.device?.appName}</p>}
{wallet?.device?.appVersion && <p>Wallet appVersion: {wallet?.device?.appVersion}</p>}
{wallet?.device?.platform && <p>Wallet platform: {wallet?.device?.platform}</p>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { ProviderSui, registerSuiWallet } from '@onekeyfe/onekey-sui-provider';
import { ProviderWebln } from '@onekeyfe/onekey-webln-provider';
import { ProviderScdo } from '@onekeyfe/onekey-scdo-provider';
import { ProviderTon } from '@onekeyfe/onekey-ton-provider';
import { createTonProviderOpenMask, ProviderTon } from '@onekeyfe/onekey-ton-provider';
import { ProviderNostr } from '@onekeyfe/onekey-nostr-provider';
import { ProviderBtc, ProviderBtcWallet } from '@onekeyfe/onekey-btc-provider';
import { ProviderAlgo } from '@onekeyfe/onekey-algo-provider';
Expand Down Expand Up @@ -211,7 +211,8 @@ function injectWeb3Provider(): unknown {
tonconnect,
});
defineWindowProperty('openmask', {
tonconnect,
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
tonconnect: createTonProviderOpenMask(tonconnect),
});
defineWindowProperty('unisat', btc);
defineWindowProperty('scdo', scdo);
Expand Down
99 changes: 68 additions & 31 deletions packages/providers/onekey-ton-provider/src/OnekeyTonProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
TonProofItem,
WalletEvent,
WalletResponse,
WalletResponseSuccess
WalletResponseSuccess,
} from '@tonconnect/protocol';
import {
AccountInfo,
Expand Down Expand Up @@ -45,7 +45,7 @@ export type TonRequest = {

type JsBridgeRequest = {
[K in keyof TonRequest]: (
params: Parameters<TonRequest[K]>
params: Parameters<TonRequest[K]>,
) => Promise<TypeUtils.WireStringified<TypeUtils.ResolvePromise<ReturnType<TonRequest[K]>>>>;
};

Expand Down Expand Up @@ -74,22 +74,54 @@ function isWalletEventMethodMatch({ method, name }: { method: string; name: stri
return method === `wallet_events_${name}`;
}

export function createTonProviderOpenMask(originalProvider: ProviderTon) {
return createTonProvider(
originalProvider,
{
appName: 'openmask',
appVersion: '0.21.1',
},
{
name: 'OpenMask',
image:
'https://raw.githubusercontent.com/OpenProduct/openmask-extension/main/public/openmask-logo-288.png',
about_url: 'https://www.openmask.app/',
},
);
}

export function createTonProvider(
originalProvider: ProviderTon,
customDeviceInfo: Partial<DeviceInfo>,
customWalletInfo: Partial<WalletInfo>,
): ProviderTon {
return new Proxy(originalProvider, {
get(target, prop, receiver) {
if (prop === 'deviceInfo') {
return { ...target.deviceInfo, ...customDeviceInfo };
}
if (prop === 'walletInfo') {
return { ...target.walletInfo, ...customWalletInfo };
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Reflect.get(target, prop, receiver);
},
});
}

export class ProviderTon extends ProviderTonBase implements IProviderTon {
private _accountInfo: AccountInfo | null = null;

deviceInfo: DeviceInfo = {
platform: this._getPlatform(),
appName: 'OneKey',
appName: 'onekey',
appVersion: '0.0.0',
maxProtocolVersion: 2,
features: [
{ name: 'SendTransaction', maxMessages: 4 },
{ name: 'SignData' },
],
features: [{ name: 'SendTransaction', maxMessages: 4 }, { name: 'SignData' }],
};
walletInfo?: WalletInfo = {
name: 'OneKey',
image: 'https://uni.onekey-asset.com/static/logo/onekey.png',
image: 'https://common.onekey-asset.com/logo/onekey-x288.png',
about_url: 'https://onekey.so',
};
protocolVersion = 2;
Expand All @@ -101,7 +133,7 @@ export class ProviderTon extends ProviderTonBase implements IProviderTon {
bridge: props.bridge || getOrCreateExtInjectedJsBridge({ timeout: props.timeout }),
});

void this._getDeviceInfo();
// void this._getDeviceInfo();

this._registerEvents();
}
Expand Down Expand Up @@ -209,34 +241,37 @@ export class ProviderTon extends ProviderTonBase implements IProviderTon {
private _id = 0;
async _connect(protocolVersion?: number, message?: ConnectRequest): Promise<ConnectEvent> {
const id = ++this._id;
const isGetTonAddr = !message || (message && message.items.some((item) => item.name === "ton_addr"));
const proofItem = message && message.items.find((item) => item.name === "ton_proof") as (TonProofItem | undefined);
const isGetTonAddr =
!message || (message && message.items.some((item) => item.name === 'ton_addr'));
const proofItem =
message &&
(message.items.find((item) => item.name === 'ton_proof') as TonProofItem | undefined);
const items = [] as ConnectItemReply[];

if (isGetTonAddr) {
if (this._accountInfo) {
items.push({
name: "ton_addr",
name: 'ton_addr',
...this._accountInfo,
});
} else {
const result = await this._callBridge({
method: 'connect',
params: (protocolVersion && message) ? [protocolVersion, message] : [],
params: protocolVersion && message ? [protocolVersion, message] : [],
});

if (!result) {
return {
event: "connect_error",
event: 'connect_error',
id,
payload: {
code: CONNECT_EVENT_ERROR_CODES.UNKNOWN_ERROR,
message: ConnectEventErrorMessage.UNKNOWN_ERROR
}
}
message: ConnectEventErrorMessage.UNKNOWN_ERROR,
},
};
}
items.push({
name: "ton_addr",
name: 'ton_addr',
...result,
});
this._handleConnected(result, { emit: true });
Expand All @@ -246,26 +281,28 @@ export class ProviderTon extends ProviderTonBase implements IProviderTon {
if (proofItem) {
const result = await this._callBridge({
method: 'signProof',
params: [{
payload: proofItem.payload,
}],
params: [
{
payload: proofItem.payload,
},
],
});
if (!result) {
return {
event: "connect_error",
event: 'connect_error',
id,
payload: {
code: CONNECT_EVENT_ERROR_CODES.UNKNOWN_ERROR,
message: ConnectEventErrorMessage.UNKNOWN_ERROR
}
}
message: ConnectEventErrorMessage.UNKNOWN_ERROR,
},
};
}
items.push({
name: "ton_proof",
name: 'ton_proof',
proof: {
...result,
payload: proofItem.payload,
}
},
});
}

Expand All @@ -289,7 +326,7 @@ export class ProviderTon extends ProviderTonBase implements IProviderTon {

async send<T extends RpcMethod>(message: AppRequest<T>): Promise<WalletResponse<T>> {
const id = message.id;

let res: unknown;
const params = message.params.map((p) => {
if (typeof p === 'string') {
Expand All @@ -311,7 +348,7 @@ export class ProviderTon extends ProviderTonBase implements IProviderTon {
code: SEND_TRANSACTION_ERROR_CODES.METHOD_NOT_SUPPORTED,
message: SendTransactionErrorMessage.METHOD_NOT_SUPPORTED,
},
}
};
}

if (res === undefined) {
Expand All @@ -321,7 +358,7 @@ export class ProviderTon extends ProviderTonBase implements IProviderTon {
code: SEND_TRANSACTION_ERROR_CODES.UNKNOWN_ERROR,
message: SendTransactionErrorMessage.UNKNOWN_ERROR,
},
}
};
}

return {
Expand Down

0 comments on commit 304d66e

Please sign in to comment.