Luffa钱包SDK
This commit is contained in:
@@ -5,6 +5,7 @@ import { logger } from "db://assets/Scripts/Main/Common/Logger";
|
|||||||
|
|
||||||
export class LuffaWalletSDK implements IWalletSDK, ISignCap, ITransactionCap {
|
export class LuffaWalletSDK implements IWalletSDK, ISignCap, ITransactionCap {
|
||||||
private address: string | null = null;
|
private address: string | null = null;
|
||||||
|
private luffaId: any = null;
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
init(cb?: Function): void {
|
init(cb?: Function): void {
|
||||||
@@ -15,8 +16,34 @@ export class LuffaWalletSDK implements IWalletSDK, ISignCap, ITransactionCap {
|
|||||||
// 连接钱包
|
// 连接钱包
|
||||||
connectWallet(cb: (address: string, publicKey?: string) => void): void {
|
connectWallet(cb: (address: string, publicKey?: string) => void): void {
|
||||||
logger.log("连接 Luffa 钱包");
|
logger.log("连接 Luffa 钱包");
|
||||||
this.address = "0x123456"; // 模拟地址
|
const opts = {
|
||||||
cb(this.address, "public-key");
|
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;
|
return this.address;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 签名消息
|
// 签名消息 (单签)
|
||||||
async signMessage(message: string, options?: any): Promise<{ signature: string; fullMessage?: string }> {
|
async signMessage(message: string, options?: any): Promise<{ signature: string; fullMessage?: string }> {
|
||||||
logger.log("调用 Luffa 单签签名");
|
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 }> {
|
async signTransaction(rawData: any): Promise<{ signedData: string }> {
|
||||||
logger.log("签名交易", rawData);
|
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 }> {
|
async sendTransaction(signedData: string): Promise<{ hash: string }> {
|
||||||
logger.log("发送交易", signedData);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.2.0",
|
||||||
|
"importer": "directory",
|
||||||
|
"imported": true,
|
||||||
|
"uuid": "d13d2f25-e50a-447a-a199-86b719e44093",
|
||||||
|
"files": [],
|
||||||
|
"subMetas": {},
|
||||||
|
"userData": {}
|
||||||
|
}
|
||||||
Vendored
+9
@@ -0,0 +1,9 @@
|
|||||||
|
declare global {
|
||||||
|
/**
|
||||||
|
* 微信小游戏运行时的全局对象
|
||||||
|
* 在 TypeScript 编译阶段不会报错
|
||||||
|
*/
|
||||||
|
const wx: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export {};
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"ver": "4.0.24",
|
||||||
|
"importer": "typescript",
|
||||||
|
"imported": true,
|
||||||
|
"uuid": "138d61f2-3332-43df-b6ca-147729b5604f",
|
||||||
|
"files": [],
|
||||||
|
"subMetas": {},
|
||||||
|
"userData": {}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user