Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions Runtime/Plugins/xNFT.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
},
});
22 changes: 20 additions & 2 deletions Runtime/codebase/XNFTWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class XNFTWallet: WalletBase

private static TaskCompletionSource<Account> _loginTaskCompletionSource;
private static TaskCompletionSource<Transaction> _signedTransactionTaskCompletionSource;
private static TaskCompletionSource<byte[]> _signedMessageTaskCompletionSource;
private static Transaction _currentTransaction;
private static Account _account;

Expand Down Expand Up @@ -56,7 +57,9 @@ protected override Task<Transaction[]> _SignAllTransactions(Transaction[] transa

public override Task<byte[]> SignMessage(byte[] message)
{
throw new NotImplementedException();
_signedMessageTaskCompletionSource = new TaskCompletionSource<byte[]>();
ExternSignMessageXNFT(Convert.ToBase64String(message), OnMessageSigned);
return _signedMessageTaskCompletionSource.Task;
}

protected override Task<Account> _CreateAccount(string mnemonic = null, string password = null)
Expand All @@ -78,7 +81,7 @@ private static void OnXNFTConnected(string walletPubKey)
}

/// <summary>
/// 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.
/// </summary>
[MonoPInvokeCallback(typeof(Action<string>))]
Expand All @@ -91,6 +94,17 @@ public static void OnTransactionSigned(string signature)
});
_signedTransactionTaskCompletionSource.SetResult(_currentTransaction);
}

/// <summary>
/// Called from java script when the xnft wallet signed the message and return the signature.
/// </summary>
[MonoPInvokeCallback(typeof(Action<string>))]
public static void OnMessageSigned(string signature)
{
var signatureArray = JsonUtility.FromJson<byte[]>(signature);
Debug.Log("SignatureArray: " + signatureArray);
_signedMessageTaskCompletionSource.SetResult(signatureArray);
}

#endregion

Expand All @@ -102,9 +116,13 @@ public static void OnTransactionSigned(string signature)
[DllImport("__Internal")]
private static extern void ExternSignTransactionXNFT(string transaction, Action<string> callback);

[DllImport("__Internal")]
private static extern void ExternSignMessageXNFT(string message, Action<string> callback);

#else
private static void ExternConnectXNFT(Action<string> callback){}
private static void ExternSignTransactionXNFT(string transaction, Action<string> callback){}
private static void ExternSignMessageXNFT(string message, Action<string> callback){}
#endif
}
}