Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/embed params #208

Merged
merged 7 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
42 changes: 34 additions & 8 deletions src/controllers/TorusController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable no-underscore-dangle */
import { getHashedName, getNameAccountKey, getTwitterRegistry, NameRegistryState } from "@solana/spl-name-service";
import { ASSOCIATED_TOKEN_PROGRAM_ID, Token, TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { Connection, LAMPORTS_PER_SOL, PublicKey, Transaction } from "@solana/web3.js";
import { Connection, LAMPORTS_PER_SOL, Message, PublicKey, Transaction } from "@solana/web3.js";
import {
BaseConfig,
BaseController,
Expand Down Expand Up @@ -58,6 +58,9 @@ import {
KeyringController,
NetworkController,
PreferencesController,
SendTransactionParams,
SignAllTransactionParams,
SignTransactionParams,
SolanaToken,
TokenInfoController,
TokensTrackerController,
Expand Down Expand Up @@ -511,7 +514,7 @@ export default class TorusController extends BaseController<TorusControllerConfi
await this.txController.approveTransaction(txId, this.state.PreferencesControllerState.selectedAddress);
}

async transfer(tx: Transaction, req?: Ihandler<{ message: string }>): Promise<string> {
async transfer(tx: Transaction, req?: Ihandler<SendTransactionParams>): Promise<string> {
const signedTransaction = await this.txController.addSignTransaction(tx, req);
try {
await signedTransaction.result;
Expand Down Expand Up @@ -986,7 +989,10 @@ export default class TorusController extends BaseController<TorusControllerConfi
throw new Error("user denied provider change request");
}

async handleTransactionPopup(txId: string, req: Ihandler<{ message: string | string[] | undefined }>): Promise<boolean> {
async handleTransactionPopup(
txId: string,
req: Ihandler<SendTransactionParams | SignTransactionParams | SignAllTransactionParams>
): Promise<boolean> {
try {
const { windowId } = req;
const channelName = `${BROADCAST_CHANNELS.TRANSACTION_CHANNEL}_${windowId}`;
Expand All @@ -995,6 +1001,7 @@ export default class TorusController extends BaseController<TorusControllerConfi
const popupPayload: TransactionChannelDataType = {
type: req.method,
message: req.params?.message || "",
messageOnly: (req.params as SignTransactionParams)?.messageOnly || false,
signer: this.selectedAddress,
// txParams: JSON.parse(JSON.stringify(this.txController.getTransaction(txId))),
origin: this.preferencesController.iframeOrigin,
Expand Down Expand Up @@ -1204,7 +1211,7 @@ export default class TorusController extends BaseController<TorusControllerConfi
return allTransactions;
}

async providerSignAllTransaction(req: Ihandler<{ message: string[] | undefined }>) {
async providerSignAllTransaction(req: Ihandler<SignAllTransactionParams>) {
if (!this.selectedAddress) throw new Error("Not logged in");

const approved = await this.handleTransactionPopup("", req);
Expand All @@ -1214,6 +1221,11 @@ export default class TorusController extends BaseController<TorusControllerConfi

// sign all transaction
const allTransactions = req.params?.message?.map((msg) => {
if (req.params?.messageOnly) {
let tx = Transaction.populate(Message.from(Buffer.from(msg, "hex")));
ieow marked this conversation as resolved.
Show resolved Hide resolved
tx = this.keyringController.signTransaction(tx, this.selectedAddress);
return JSON.stringify({ publicKey: this.selectedAddress, signature: tx.signature?.toString("hex") });
}
let tx = Transaction.from(Buffer.from(msg, "hex"));
tx = this.keyringController.signTransaction(tx, this.selectedAddress);
const signedMessage = tx.serialize({ requireAllSignatures: false }).toString("hex");
Expand Down Expand Up @@ -1243,13 +1255,18 @@ export default class TorusController extends BaseController<TorusControllerConfi
return this.preferencesController.state.selectedAddress ? [this.preferencesController.state.selectedAddress] : [];
}

private async providerSignTransaction(req: JRPCRequest<any>) {
private async providerSignTransaction(req: JRPCRequest<SignTransactionParams>) {
if (!this.selectedAddress) throw new Error("Not logged in");

const message = req.params?.message;
if (!message) throw new Error("empty error message");

const tx = Transaction.from(Buffer.from(message, "hex"));
let tx: Transaction;
if (req.params?.messageOnly) {
tx = Transaction.populate(Message.from(Buffer.from(message, "hex")));
} else {
tx = Transaction.from(Buffer.from(message, "hex"));
}

const ret_signed = await this.txController.addSignTransaction(tx, req);
try {
Expand All @@ -1263,16 +1280,25 @@ export default class TorusController extends BaseController<TorusControllerConfi
if (gaslessHost) {
signed_tx = await getRelaySigned(gaslessHost, signed_tx, tx.recentBlockhash || "");
}

if (req.params?.messageOnly) {
return JSON.stringify({
publicKey: this.selectedAddress,
signature: ret_signed.transactionMeta.transaction.signature?.toString("hex"),
});
}
return signed_tx;
}

private async sendTransaction(req: JRPCRequest<any>) {
private async sendTransaction(req: JRPCRequest<SendTransactionParams>) {
if (!this.selectedAddress) throw new Error("Not logged in");

const message = req.params?.message;
if (!message) throw new Error("empty error message");

const tx = Transaction.from(Buffer.from(message, "hex"));
let tx: Transaction;
if (req.params?.messageOnly) tx = Transaction.populate(Message.from(Buffer.from(message, "hex")));
else tx = Transaction.from(Buffer.from(message, "hex"));

return this.transfer(tx, req);
}
Expand Down
16 changes: 13 additions & 3 deletions src/pages/Confirm.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { LAMPORTS_PER_SOL, SystemInstruction, SystemProgram, Transaction } from "@solana/web3.js";
import { LAMPORTS_PER_SOL, Message, SystemInstruction, SystemProgram, Transaction } from "@solana/web3.js";
import { addressSlicer, BROADCAST_CHANNELS, BroadcastChannelHandler, broadcastChannelOptions, POPUP_RESULT } from "@toruslabs/base-controllers";
import { BigNumber } from "bignumber.js";
import { BroadcastChannel } from "broadcast-channel";
Expand Down Expand Up @@ -74,7 +74,12 @@ onMounted(async () => {
if (txData.type === "sign_all_transactions") {
const decoded: DecodedDataType[] = [];
(txData.message as string[]).forEach((msg) => {
const tx2 = Transaction.from(Buffer.from(msg, "hex"));
let tx2: Transaction;
if (txData.messageOnly) {
tx2 = Transaction.populate(Message.from(Buffer.from(msg, "hex")));
} else {
tx2 = Transaction.from(Buffer.from(msg, "hex"));
}
tx2.instructions.forEach((inst) => {
decoded.push(decodeInstruction(inst));
});
Expand All @@ -83,7 +88,12 @@ onMounted(async () => {
return;
}

tx.value = Transaction.from(Buffer.from(txData.message as string, "hex"));
if (txData.messageOnly) {
tx.value = Transaction.populate(Message.from(Buffer.from(txData.message as string, "hex")));
} else {
tx.value = Transaction.from(Buffer.from(txData.message as string, "hex"));
}

const block = await ControllerModule.torus.connection.getRecentBlockhash("finalized");

const isGasless = tx.value.feePayer?.toBase58() !== txData.signer;
Expand Down
1 change: 1 addition & 0 deletions src/utils/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export interface EmbedInitParams {
export type TransactionChannelDataType = {
type: string;
message: string | string[];
messageOnly?: boolean;
origin: string;
signer: string;
balance: string;
Expand Down