diff --git a/src/connector/TonhubLocalConnector.ts b/src/connector/TonhubLocalConnector.ts index 969ebc7..827095a 100644 --- a/src/connector/TonhubLocalConnector.ts +++ b/src/connector/TonhubLocalConnector.ts @@ -1,4 +1,5 @@ import * as t from 'io-ts'; +import { Cell, CommentMessage } from 'ton'; const configCodec = t.type({ version: t.literal(1), @@ -51,6 +52,18 @@ export type TonhubLocalTransactionResponse = { type: 'rejected' }; +export type TonhubLocalSignRequest = { + text?: string | null | undefined, + payload?: string | null | undefined +} + +export type TonhubLocalSignResponse = { + type: 'success', + signature: string +} | { + type: 'rejected' +}; + export class TonhubLocalConnector { static isAvailable() { @@ -138,6 +151,40 @@ export class TonhubLocalConnector { throw Error(res.message); } + async requestSign(request: TonhubLocalSignRequest): Promise { + + // Parse data + let data: Cell = new Cell(); + if (typeof request.payload === 'string') { + data = Cell.fromBoc(Buffer.from(request.payload, 'base64'))[0]; + } + + // Comment + let comment: string = ''; + if (typeof request.text === 'string') { + comment = request.text; + } + let commentCell = new Cell(); + new CommentMessage(comment).writeTo(commentCell); + + let res = await this.#doRequest('tx', { + network: this.network, + textCell: commentCell.toBoc({ idx: false }).toString('base64'), + payloadCell: data.toBoc({ idx: false }).toString('base64') + }); + if (res.type === 'ok') { + let d = res.data; + if (d.state === 'rejected') { + return { type: 'rejected' }; + } + if (d.state === 'sent') { + return { type: 'success', signature: d.result }; + } + throw Error('Unknown reponse'); + } + throw Error(res.message); + } + async #doRequest(name: string, args: any) { return await new Promise((resolve) => this.#provider(name, args, resolve)); }