Skip to content

Commit

Permalink
removed top level await
Browse files Browse the repository at this point in the history
  • Loading branch information
ihsraham committed Oct 20, 2024
1 parent f467d39 commit 7a2f231
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { getHttpEndpoint } from "@orbs-network/ton-access";
import { useLaunchParams, isTMA } from "@telegram-apps/sdk-react";
import { mockTelegramEnvironment } from "./hooks/useMockTelegramInitData";
import TonRPC from "./tonRpc";
// import TonRPC from "./tonRpc";
import Loading from "./Loading";
import "./App.css";

Expand Down
26 changes: 12 additions & 14 deletions single-factor-auth-web/sfa-web-ton-telegram-example/src/tonRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import type { IProvider } from "@web3auth/base";
import { getHttpEndpoint } from "@orbs-network/ton-access";
import TonWeb from "tonweb";

const rpc = await getHttpEndpoint({
network: "testnet",
protocol: "json-rpc",
});

export default class TonRPC {
private provider: IProvider;
private tonweb: TonWeb;

constructor(provider: IProvider) {
this.provider = provider;
// Initialize tonweb in the constructor
this.initTonWeb();
}

private async initTonWeb() {
const rpc = await getHttpEndpoint({
network: "testnet",
protocol: "json-rpc",
});
this.tonweb = new TonWeb(new TonWeb.HttpProvider(rpc));
}

Expand All @@ -32,7 +36,7 @@ export default class TonRPC {
}
}

getChainId(): string {
getChainId(): string {
return "testnet";
}

Expand Down Expand Up @@ -77,7 +81,6 @@ export default class TonRPC {
const result = await transfer.send();

console.log(result);
// Return the full result for display in uiConsole
return result;
} catch (error) {
console.error("Error sending transaction:", error);
Expand All @@ -101,23 +104,19 @@ export default class TonRPC {
}
}

public getKeyPairFromPrivateKey(privateKey: string): { publicKey: Uint8Array; secretKey: Uint8Array } {
// Convert the hex string to a Uint8Array
private getKeyPairFromPrivateKey(privateKey: string): { publicKey: Uint8Array; secretKey: Uint8Array } {
const privateKeyBytes = new Uint8Array(privateKey.match(/.{1,2}/g)!.map(byte => parseInt(byte, 16)));

// Ensure the private key is 32 bytes (256 bits)
if (privateKeyBytes.length !== 32) {
// If it's shorter, pad it. If it's longer, truncate it.
const adjustedPrivateKey = new Uint8Array(32);
adjustedPrivateKey.set(privateKeyBytes.slice(0, 32));
return TonWeb.utils.nacl.sign.keyPair.fromSeed(adjustedPrivateKey);
}

// If it's already 32 bytes, use it directly
return TonWeb.utils.nacl.sign.keyPair.fromSeed(privateKeyBytes);
}

async getPrivateKey(): Promise<string> {
private async getPrivateKey(): Promise<string> {
try {
return await this.provider.request({
method: "private_key",
Expand All @@ -127,5 +126,4 @@ export default class TonRPC {
throw error;
}
}

}

0 comments on commit 7a2f231

Please sign in to comment.