From 619cf2c1498a4a7e0d809d5800a1fa089b14bf8e Mon Sep 17 00:00:00 2001 From: DFL Zeke Date: Thu, 27 Apr 2023 20:28:44 +0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20message=20sign=20to=20xNFT=20?= =?UTF-8?q?wallet=20adapter.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/Plugins/xNFT.jslib | 20 ++++++++++++++++++++ Runtime/codebase/XNFTWallet.cs | 22 ++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/Runtime/Plugins/xNFT.jslib b/Runtime/Plugins/xNFT.jslib index 06617461..ec2f04bb 100644 --- a/Runtime/Plugins/xNFT.jslib +++ b/Runtime/Plugins/xNFT.jslib @@ -35,4 +35,24 @@ mergeInto(LibraryManager.library, { console.error("Not running in Backpack wallet"); } }, + + ExternSignMessageXNFT: async function (message, callback) { + if ('xnft' in window && window.xnft != undefined && window.xnft.solana != undefined) { + try { + const messageBase64String = UTF8ToString(message); + const messageBytes = Uint8Array.from(atob(messageBase64String), (c) => c.charCodeAt(0)); + const signedMessage = await window.xnft.solana.signMessage(messageBytes); + console.log(signedMessage); + var sign = JSON.stringify(Array.from(signedMessage)); + var lenSign = lengthBytesUTF8(sign) + 1; + var strPtr = _malloc(lenSign); + stringToUTF8(sign, strPtr, lenSign); + Module.dynCall_vi(callback, strPtr); + } catch (err) { + console.error(err.message); + } + } else { + console.error("Not running in Backpack wallet"); + } + }, }); diff --git a/Runtime/codebase/XNFTWallet.cs b/Runtime/codebase/XNFTWallet.cs index 202c4bbf..efb909c4 100644 --- a/Runtime/codebase/XNFTWallet.cs +++ b/Runtime/codebase/XNFTWallet.cs @@ -16,6 +16,7 @@ public class XNFTWallet: WalletBase private static TaskCompletionSource _loginTaskCompletionSource; private static TaskCompletionSource _signedTransactionTaskCompletionSource; + private static TaskCompletionSource _signedMessageTaskCompletionSource; private static Transaction _currentTransaction; private static Account _account; @@ -56,7 +57,9 @@ protected override Task _SignAllTransactions(Transaction[] transa public override Task SignMessage(byte[] message) { - throw new NotImplementedException(); + _signedMessageTaskCompletionSource = new TaskCompletionSource(); + ExternSignMessageXNFT(Convert.ToBase64String(message), OnMessageSigned); + return _signedMessageTaskCompletionSource.Task; } protected override Task _CreateAccount(string mnemonic = null, string password = null) @@ -78,7 +81,7 @@ private static void OnXNFTConnected(string walletPubKey) } /// - /// Called from java script when the phantom wallet signed the transaction and return the signature + /// Called from java script when the xnft wallet signed the transaction and return the signature /// that we then need to put into the transaction before we send it out. /// [MonoPInvokeCallback(typeof(Action))] @@ -91,6 +94,17 @@ public static void OnTransactionSigned(string signature) }); _signedTransactionTaskCompletionSource.SetResult(_currentTransaction); } + + /// + /// Called from java script when the xnft wallet signed the message and return the signature. + /// + [MonoPInvokeCallback(typeof(Action))] + public static void OnMessageSigned(string signature) + { + var signatureArray = JsonUtility.FromJson(signature); + Debug.Log("SignatureArray: " + signatureArray); + _signedMessageTaskCompletionSource.SetResult(signatureArray); + } #endregion @@ -102,9 +116,13 @@ public static void OnTransactionSigned(string signature) [DllImport("__Internal")] private static extern void ExternSignTransactionXNFT(string transaction, Action callback); + [DllImport("__Internal")] + private static extern void ExternSignMessageXNFT(string message, Action callback); + #else private static void ExternConnectXNFT(Action callback){} private static void ExternSignTransactionXNFT(string transaction, Action callback){} + private static void ExternSignMessageXNFT(string message, Action callback){} #endif } } \ No newline at end of file