Skip to content

Commit

Permalink
Revert "fix: Ton related reports (#587)" (#588)
Browse files Browse the repository at this point in the history
This reverts commit 0a56387.
  • Loading branch information
y3fers0n authored Dec 13, 2024
1 parent 0a56387 commit fdcad03
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 164 deletions.
14 changes: 7 additions & 7 deletions android/lib/src/main/res/raw/trust_min.js

Large diffs are not rendered by default.

Binary file modified bun.lockb
Binary file not shown.
2 changes: 0 additions & 2 deletions packages/android-web3-provider/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import nodePolyfills from 'rollup-plugin-polyfill-node';
import inject from '@rollup/plugin-inject';
import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';

const input = './src/index.ts';
const plugins = [
json(),
nodePolyfills(),
resolve({ browser: true, preferBuiltins: false }),
commonjs(),
Expand Down
2 changes: 0 additions & 2 deletions packages/ios-web3-provider/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import commonjs from '@rollup/plugin-commonjs';
import { name, dependencies } from './package.json';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import inject from '@rollup/plugin-inject';
import json from '@rollup/plugin-json';

const input = './index.ts';
const plugins = [
json(),
nodeResolve({ preferBuiltins: false, browser: true }),
commonjs(),
inject({
Expand Down
4 changes: 2 additions & 2 deletions packages/ios-web3-provider/swift/trust-min.js
Git LFS file not shown
2 changes: 0 additions & 2 deletions packages/solana/SolanaProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'rpc-websockets/dist/lib/client';

import {
BaseProvider,
IRequestArguments,
Expand Down
3 changes: 1 addition & 2 deletions packages/solana/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"@wallet-standard/base": "1.1.0",
"@wallet-standard/features": "1.0.3",
"bs58": "4.0.1",
"@trustwallet/web3-provider-core": "workspace:*",
"rpc-websockets": "7.11.0"
"@trustwallet/web3-provider-core": "workspace:*"
},
"devDependencies": {
"@types/bs58": "^4.0.1"
Expand Down
132 changes: 11 additions & 121 deletions packages/ton/MobileAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Address, WalletContractV4 } from '@ton/ton';
import { TonConnectError } from './exceptions/TonConnectError';
import { TonProvider } from './TonProvider';
import {
ConnectItemReply,
Expand All @@ -9,21 +7,7 @@ import {

interface ITransaction {
valid_until: number;
messages: {
stateInit: string;
state_init: string;
address: string;
amount: string;
}[];
network: string;
from: string;
}

interface TransformedTransaction {
messages: ITransaction['messages'];
valid_until: number;
network: string;
from: string;
messages: { state_init: string; address: string }[];
}

/**
Expand All @@ -35,23 +19,21 @@ interface TransformedTransaction {
export class MobileAdapter {
provider: TonProvider;

private rawAddress: string | null = null;

constructor(provider: TonProvider) {
this.provider = provider;
}

static mapToCamelCase(transaction: ITransaction): TransformedTransaction {
static mapToCamelCase(transaction: ITransaction) {
return {
...transaction,
...(transaction?.messages
? {
messages: (transaction?.messages || []).map((message) => ({
...message,
...('state_init' in message || 'stateInit' in message
? { stateInit: message.state_init || message.stateInit }
: {}),
})),
messages: (transaction?.messages || []).map(
({ state_init, ...message }) => ({
...message,
stateInit: state_init,
}),
),
}
: {}),
};
Expand All @@ -78,16 +60,11 @@ export class MobileAdapter {
console.warn('type parameter removed from request');
}

this.rawAddress = rest.address;

return rest;
}

if (item.name === 'ton_proof') {
const { type, ...response } = item as TonProofItemReplySuccess & {
type?: string;
};

const response = item as TonProofItemReplySuccess;
return {
...response,
proof: {
Expand All @@ -106,35 +83,17 @@ export class MobileAdapter {
'tonConnect_reconnect',
params,
);

const parsedResponse = JSON.parse(res);

const { nonBounceable, type, ...rest } =
parsedResponse[0] as TonAddressItemReply & {
nonBounceable: string;
type?: string;
};

this.rawAddress = rest.address;

return [rest] as T;
return JSON.parse(res);
}

case 'ton_rawSign':
return this.provider.internalRequest<T>('signMessage', params);

case 'ton_sendTransaction':
case 'tonConnect_sendTransaction': {
const tx = (params as object[])[0] as ITransaction;

this.validateNetwork(tx);
this.validateMessagesAddresses(tx);
this.validateFromAddress(tx);
this.validateTransaction(MobileAdapter.mapToCamelCase(tx));

const res = await this.provider.internalRequest<string>(
'signTransaction',
MobileAdapter.mapToCamelCase(tx),
MobileAdapter.mapToCamelCase((params as object[])[0] as ITransaction),
);

const { nonce, hash } = JSON.parse(res);
Expand Down Expand Up @@ -173,73 +132,4 @@ export class MobileAdapter {
return this.provider.internalRequest(method, params);
}
}

validateTransaction(tx: TransformedTransaction) {
// throw error if there is a message with empty state init
if (
tx.messages.some(
(message) => 'stateInit' in message && message.stateInit.length === 0,
)
) {
console.error('Empty state init in message');
throw new TonConnectError('Bad request', 1);
}

// throw error if there is a message with amount not being a string
if (tx.messages.some((message) => typeof message.amount !== 'string')) {
console.error('Invalid amount type');
throw new TonConnectError('Bad request', 1);
}

// throw error if valid until is not a number
if (typeof tx.valid_until !== 'number') {
console.error('Invalid valid_until type');
throw new TonConnectError('Bad request', 1);
}
}

validateFromAddress(tx: ITransaction) {
if (!this.rawAddress) {
console.error('Trying to execute transaction with invalid address');
throw new TonConnectError('Bad request', 1);
}

const address = Address.parseRaw(this.rawAddress);

const collection = [
address.toRawString(),
address.toString({ bounceable: true }),
address.toString({ bounceable: false }),
];

console.log('collection', collection);

if (!collection.includes(tx.from)) {
console.error('from field does not match any user address');
throw new TonConnectError('Bad request', 1);
}
}

/**
* Validation on messages
* @param tx
*/
validateMessagesAddresses(tx: ITransaction) {
// Message addresses can not be raw
if (tx.messages.some((e) => e.address.includes(':'))) {
console.error('Bad request, message address is invalid');
throw new TonConnectError('Bad request', 1);
}
}

/**
* Enforce mainnet
* @param tx
*/
validateNetwork(tx: ITransaction) {
if (tx.network !== '-239') {
console.error('Bad request, network id is invalid');
throw new TonConnectError('Bad request', 1);
}
}
}
32 changes: 11 additions & 21 deletions packages/ton/TonBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,16 @@ export class TonBridge implements TonConnectBridge {
this.connectionAttempts += 1;

if ((items as any)?.event === 'connect_error') {
return this.emit({
...(items as any),
id: this.connectionAttempts.toString(),
});
return this.emit({ ...(items as any), id: this.connectionAttempts });
} else {
return this.emit({
id: this.connectionAttempts.toString(),
id: this.connectionAttempts,
event: 'connect',
payload: { items, device: this.deviceInfo },
});
}
} catch (e) {
return this.parseError(e, {
id: this.connectionAttempts.toString(),
}) as WalletResponseError;
return this.parseError(e, { id: this.connectionAttempts });
}
}

Expand Down Expand Up @@ -134,14 +129,11 @@ export class TonBridge implements TonConnectBridge {
this.connectionAttempts += 1;

if ((items as any)?.event === 'connect_error') {
return this.emit({
...(items as any),
id: this.connectionAttempts.toString(),
});
return this.emit({ ...(items as any), id: this.connectionAttempts });
}

return this.emit({
id: this.connectionAttempts.toString(),
id: this.connectionAttempts,
event: 'connect',
payload: {
items,
Expand Down Expand Up @@ -177,9 +169,7 @@ export class TonBridge implements TonConnectBridge {

return { result, id: message.id.toString() };
} catch (e) {
return this.parseError(e, {
id: message.id.toString(),
}) as WalletResponseError;
return this.parseError(e, { id: message.id });
}
}

Expand All @@ -203,7 +193,7 @@ export class TonBridge implements TonConnectBridge {
message: 'User declined the transaction',
code: 300,
},
id: String(message.id) ?? '0',
id: String(message.id) ?? 0,
};
}

Expand All @@ -214,7 +204,7 @@ export class TonBridge implements TonConnectBridge {
message: 'Bad request, a transaction is already pending',
code: 1,
},
id: String(message.id) ?? '0',
id: String(message.id) ?? 0,
};
}

Expand All @@ -228,13 +218,13 @@ export class TonBridge implements TonConnectBridge {
message: 'User declined the transaction',
code: 300,
},
id: String(message.id) ?? '0',
id: String(message.id) ?? 0,
};
}

return {
error: formatConnectEventError(e as TonConnectError),
id: String(message.id) ?? '0',
error: e as WalletResponseError['error'],
id: String(message.id) ?? 0,
};
}
}
2 changes: 0 additions & 2 deletions packages/ton/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
},
"peerDependencies": {},
"dependencies": {
"@ton/crypto": "3.3.0",
"@ton/ton": "15.1.0",
"@trustwallet/web3-provider-core": "workspace:*"
}
}
6 changes: 3 additions & 3 deletions packages/ton/types/TonBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export type ConnectItemReply = TonAddressItemReply | TonProofItemReply;

type ConnectEventSuccess = {
event: 'connect';
id?: string;
id?: number;
payload: {
items: ConnectItemReply[];
device: DeviceInfo;
Expand All @@ -85,7 +85,7 @@ type ConnectEventSuccess = {

export type ConnectEventError = {
event: 'connect_error';
id?: string;
id?: number;
payload: {
code: number;
message: string;
Expand All @@ -108,7 +108,7 @@ export interface WalletResponseError {

export interface WalletEvent {
event: WalletEventName;
id?: string;
id?: number;
payload: any;
}

Expand Down

0 comments on commit fdcad03

Please sign in to comment.