diff --git a/assets/Scripts/Main/Channel/wallet/LuffaWalletSDK.ts b/assets/Scripts/Main/Channel/wallet/LuffaWalletSDK.ts index b27fa4b9..b2a1fc6d 100644 --- a/assets/Scripts/Main/Channel/wallet/LuffaWalletSDK.ts +++ b/assets/Scripts/Main/Channel/wallet/LuffaWalletSDK.ts @@ -5,6 +5,7 @@ import { logger } from "db://assets/Scripts/Main/Common/Logger"; export class LuffaWalletSDK implements IWalletSDK, ISignCap, ITransactionCap { private address: string | null = null; + private luffaId: any = null; // 初始化 init(cb?: Function): void { @@ -15,8 +16,34 @@ export class LuffaWalletSDK implements IWalletSDK, ISignCap, ITransactionCap { // 连接钱包 connectWallet(cb: (address: string, publicKey?: string) => void): void { logger.log("连接 Luffa 钱包"); - this.address = "0x123456"; // 模拟地址 - cb(this.address, "public-key"); + const opts = { + api_name: "luffaWebRequest", + data: { + uuid: this.create16String(), + methodName: "connect", + initData: { + network: "endless", // endless 为主网, ends 为测试网 + }, + metadata: { + superBox: true, + url: "https://your-game-domain.com", // 建议填自己游戏的唯一标识 + // icon: "https://your-game-domain.com/icon.png", + }, + from: "", + data: {}, + }, + success: (res) => { + logger.log("Luffa connect success:", res); + this.address = res.data?.address ?? null; + this.luffaId = res.data?.uid ?? null; + cb(this.address ?? "", res.data?.publicKey ?? ""); + }, + fail: (err) => { + logger.error("Luffa connect fail:", err); + cb("", ""); + }, + }; + wx.invokeNativePlugin(opts); } // 断开钱包连接 @@ -29,21 +56,107 @@ export class LuffaWalletSDK implements IWalletSDK, ISignCap, ITransactionCap { return this.address; } - // 签名消息 + // 签名消息 (单签) async signMessage(message: string, options?: any): Promise<{ signature: string; fullMessage?: string }> { logger.log("调用 Luffa 单签签名"); - return { signature: "0xsignedmsg", fullMessage: message }; + return new Promise((resolve, reject) => { + const opts = { + api_name: "luffaWebRequest", + data: { + uuid: this.create16String(), + methodName: "signMessageV2", + initData: { network: "endless" }, + from: this.address ?? "", + data: { + message, + nonce: options?.nonce ?? Date.now(), // 默认用时间戳 + address: options?.address ?? false, + application: options?.application ?? false, + chainId: options?.chainId ?? false, + }, + }, + success: (res) => { + logger.log("signMessageV2 success:", res); + resolve({ + signature: res.data?.signature, + fullMessage: res.data?.fullMessage, + }); + }, + fail: (err) => { + logger.error("signMessageV2 fail:", err); + reject(err); + }, + }; + wx.invokeNativePlugin(opts); + }); } // 签名交易 async signTransaction(rawData: any): Promise<{ signedData: string }> { logger.log("签名交易", rawData); - return { signedData: "0xsignedtx" }; + return new Promise((resolve, reject) => { + const opts = { + api_name: "luffaWebRequest", + data: { + uuid: this.create16String(), + from: this.address ?? "", + methodName: "packageTransactionV2", + initData: { network: "endless" }, + data: { + data: JSON.stringify(rawData), + }, + }, + success: (res) => { + logger.log("packageTransactionV2 success:", res); + resolve({ signedData: res.data?.rawData }); + }, + fail: (err) => { + logger.error("packageTransactionV2 fail:", err); + reject(err); + }, + }; + wx.invokeNativePlugin(opts); + }); } // 发送交易(广播到链上) async sendTransaction(signedData: string): Promise<{ hash: string }> { logger.log("发送交易", signedData); - return { hash: "0xhash" }; + return new Promise((resolve, reject) => { + const opts = { + api_name: "luffaWebRequest", + data: { + uuid: this.create16String(), + from: this.address ?? "", + methodName: "signAndSubmitTransaction", + initData: { network: "endless" }, + data: { + serializedTransaction: { + data: signedData, + }, + }, + }, + success: (res) => { + logger.log("sendTransaction success:", res); + resolve({ hash: res.data?.hash }); + }, + fail: (err) => { + logger.error("sendTransaction fail:", err); + reject(err); + }, + }; + wx.invokeNativePlugin(opts); + }); } + + // 工具:生成随机 16 长度字符串 + private create16String(): string { + const len = 16; + const strVals = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678"; + let randomStr = ""; + for (let i = 0; i < len; i++) { + randomStr += strVals.charAt(Math.floor(Math.random() * strVals.length)); + } + return randomStr; + } } diff --git a/assets/Scripts/typings.meta b/assets/Scripts/typings.meta new file mode 100644 index 00000000..e33a2f3b --- /dev/null +++ b/assets/Scripts/typings.meta @@ -0,0 +1,9 @@ +{ + "ver": "1.2.0", + "importer": "directory", + "imported": true, + "uuid": "d13d2f25-e50a-447a-a199-86b719e44093", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/typings/global.d.ts b/assets/Scripts/typings/global.d.ts new file mode 100644 index 00000000..c513e071 --- /dev/null +++ b/assets/Scripts/typings/global.d.ts @@ -0,0 +1,9 @@ +declare global { + /** + * 微信小游戏运行时的全局对象 + * 在 TypeScript 编译阶段不会报错 + */ + const wx: any; + } + + export {}; \ No newline at end of file diff --git a/assets/Scripts/typings/global.d.ts.meta b/assets/Scripts/typings/global.d.ts.meta new file mode 100644 index 00000000..6f595e7f --- /dev/null +++ b/assets/Scripts/typings/global.d.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "138d61f2-3332-43df-b6ca-147729b5604f", + "files": [], + "subMetas": {}, + "userData": {} +}