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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mergeInto(LibraryManager.library, {
InitWalletAdapter: async function (callback) {

const isXnft = Boolean("xnft" in window && window.xnft != undefined && window.xnft.solana != undefined && window.xnft.solana.publicKey != undefined);
// Add UnityWalletAdapter from CDN
if(window.walletAdapterLib == undefined){
console.log("Adding WalletAdapterLib")
Expand All @@ -8,10 +10,11 @@
document.head.appendChild(script);
script.onload = function() {
console.log("WalletAdapterLib loaded");
Module.dynCall_vi(callback);
Module.dynCall_vi(callback, isXnft);
};
}else{
Module.dynCall_vi(callback, isXnft);
}
console.log(window.walletAdapterLib);
},
ExternGetWallets: async function(callback) {
try {
Expand All @@ -27,7 +30,12 @@
ExternConnectWallet: async function (walletNamePtr, callback) {
try {
const walletName = UTF8ToString(walletNamePtr)
var pubKey = await window.walletAdapterLib.connectWallet(walletName);
var pubKey;
if(walletName === 'XNFT'){
pubKey = window.xnft.solana.publicKey.toString();
} else {
pubKey = await window.walletAdapterLib.connectWallet(walletName);
}
var bufferSize = lengthBytesUTF8(pubKey) + 1;
var pubKeyPtr = _malloc(bufferSize);
stringToUTF8(pubKey, pubKeyPtr, bufferSize);
Expand All @@ -40,8 +48,14 @@
try {
const walletName = UTF8ToString(walletNamePtr)
var base64transaction = UTF8ToString(transactionPtr)
var signedTransaction = await window.walletAdapterLib.signTransaction(walletName, base64transaction);
var signature = signedTransaction.signature.toString('base64');
let signedTransaction;
if(walletName === 'XNFT'){
const transaction = window.walletAdapterLib.getTransactionFromStr(base64transaction);
signedTransaction = await window.xnft.solana.signTransaction(transaction);
} else {
signedTransaction = await window.walletAdapterLib.signTransaction(walletName, base64transaction);
}
let signature = signedTransaction.signature.toString('base64');
var bufferSize = lengthBytesUTF8(signature) + 1;
var signaturePtr = _malloc(bufferSize);
stringToUTF8(signature, signaturePtr, bufferSize);
Expand All @@ -54,8 +68,15 @@
try {
const walletName = UTF8ToString(walletNamePtr)
var base64Message = UTF8ToString(messagePtr)
var signature = await window.walletAdapterLib.signMessage(walletName, base64Message);
var signatureStr = signature.toString('base64');
let signatureStr;
if(walletName === 'XNFT'){
const messageBytes = Uint8Array.from(atob(base64Message), (c) => c.charCodeAt(0));
const signedMessage = await window.xnft.solana.signMessage(messageBytes);
signatureStr = JSON.stringify(Array.from(signedMessage));
} else {
var signature = await window.walletAdapterLib.signMessage(walletName, base64Message);
signatureStr = signature.toString('base64');
}
var bufferSize = lengthBytesUTF8(signatureStr) + 1;
var signaturePtr = _malloc(bufferSize);
stringToUTF8(signatureStr, signaturePtr, bufferSize);
Expand All @@ -69,7 +90,17 @@
const walletName = UTF8ToString(walletNamePtr)
var base64transactionsStr = UTF8ToString(transactionsPtr)
var base64transactions = base64transactionsStr.split(',');
var signedTransactions = await window.walletAdapterLib.signAllTransactions(walletName, base64transactions);
let signedTransactions;
if(walletName === 'XNFT'){
let transactions = [];
for(var i = 0; i < base64transactions.length; i++){
const transaction = window.walletAdapterLib.getTransactionFromStr(base64transactions[i]);
transactions.push(transaction);
}
signedTransactions = await window.xnft.solana.signAllTransactions(transactions);
} else {
signedTransactions = await window.walletAdapterLib.signAllTransactions(walletName, base64transactions);
}
var signatures = [];
for (var i = 0; i < signedTransactions.length; i++) {
var signedTransaction = signedTransactions[i];
Expand Down
58 changes: 0 additions & 58 deletions Runtime/Plugins/xNFT.jslib

This file was deleted.

32 changes: 0 additions & 32 deletions Runtime/Plugins/xNFT.jslib.meta

This file was deleted.

2 changes: 1 addition & 1 deletion Runtime/codebase/Exporters/WebGLTemplatesExporter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if UNITY_WEBGL && UNITY_EDITOR
#if UNITY_WEBGL && UNITY_EDITOR

using UnityEngine;
using UnityEditor;
Expand Down
14 changes: 3 additions & 11 deletions Runtime/codebase/Exporters/WebGLTemplatesExporter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class SolanaWalletAdapterWebGL: WalletBase
private static SolanaWalletAdapterWebGLOptions _walletOptions;
private static TaskCompletionSource<Account> _loginTaskCompletionSource;
private static TaskCompletionSource<string> _getWalletsTaskCompletionSource;
private static TaskCompletionSource<bool> _loadedScriptTaskCompletionSource;
private static TaskCompletionSource<bool> _walletsInitializedTaskCompletionSource;
private static TaskCompletionSource<Transaction> _signedTransactionTaskCompletionSource;
private static TaskCompletionSource<Transaction[]> _signedAllTransactionsTaskCompletionSource;
private static TaskCompletionSource<byte[]> _signedMessageTaskCompletionSource;
Expand Down Expand Up @@ -72,19 +72,34 @@ public SolanaWalletAdapterWebGL(
}

private static async Task InitWallets() {
if (Wallets == null){
_loadedScriptTaskCompletionSource = new TaskCompletionSource<bool>();
InitWalletAdapter(OnScriptLoaded);
await _loadedScriptTaskCompletionSource.Task;
_currentWallet = null;
_walletsInitializedTaskCompletionSource = new TaskCompletionSource<bool>();
InitWalletAdapter(OnWalletsInitialized);
bool isXnft = await _walletsInitializedTaskCompletionSource.Task;
if (isXnft){
_currentWallet = new WalletSpecs()
{
name = "XNFT",
icon = "",
installed = true
};
} else{
_getWalletsTaskCompletionSource = new TaskCompletionSource<string>();
ExternGetWallets(OnWalletsLoaded);
var walletsData = await _getWalletsTaskCompletionSource.Task;
Wallets = JsonUtility.FromJson<WalletSpecsObject>(walletsData).wallets;
}
_getWalletsTaskCompletionSource = new TaskCompletionSource<string>();
ExternGetWallets(OnWalletsLoaded);
var walletsData = await _getWalletsTaskCompletionSource.Task;
Wallets = JsonUtility.FromJson<WalletSpecsObject>(walletsData).wallets;

}




/// <summary>
/// Check whether it's an XNFT or not
/// </summary>
/// <returns> true if it's an XNFT, false otherwise</returns>
public static async Task<bool> IsXnft(){
await InitWallets();
return _currentWallet != null && _currentWallet.name == "XNFT";
}

protected override async Task<Account> _Login(string password = null)
{
Expand All @@ -99,29 +114,36 @@ protected override async Task<Account> _Login(string password = null)
Debug.LogError("WalletAdapter _Login -> Exception: " + e);
_loginTaskCompletionSource.SetResult(null);
}
WalletAdapterUI.SetActive(false);
if (WalletAdapterUI != null ){
WalletAdapterUI.SetActive(false);
}
return await _loginTaskCompletionSource.Task;
}

private static async Task SetCurrentWallet()
{
await InitWallets();
if (WalletAdapterUI == null)
if (_currentWallet == null)
{
WalletAdapterUI = GameObject.Instantiate(_walletOptions.walletAdapterUIPrefab);
if (WalletAdapterUI == null)
{
WalletAdapterUI = GameObject.Instantiate(_walletOptions.walletAdapterUIPrefab);
}

var waitForWalletSelectionTask = new TaskCompletionSource<string>();
var walletAdapterScreen =
WalletAdapterUI.transform.GetChild(0).gameObject.GetComponent<WalletAdapterScreen>();
walletAdapterScreen.viewPortContent = WalletAdapterUI.transform.GetChild(0).Find("Scroll View")
.Find("Viewport").Find("Content").GetComponent<RectTransform>();
walletAdapterScreen.buttonPrefab = _walletOptions.walletAdapterButtonPrefab;
walletAdapterScreen.OnSelectedAction = walletName =>
{
waitForWalletSelectionTask.SetResult(walletName);
};
WalletAdapterUI.SetActive(true);
var walletName = await waitForWalletSelectionTask.Task;
_currentWallet = Array.Find(Wallets, wallet => wallet.name == walletName);
}

var waitForWalletSelectionTask = new TaskCompletionSource<string>();
var walletAdapterScreen = WalletAdapterUI.transform.GetChild(0).gameObject.GetComponent<WalletAdapterScreen>();
walletAdapterScreen.viewPortContent = WalletAdapterUI.transform.GetChild(0).Find("Scroll View").Find("Viewport").Find("Content").GetComponent<RectTransform>();
walletAdapterScreen.buttonPrefab = _walletOptions.walletAdapterButtonPrefab;
walletAdapterScreen.OnSelectedAction = walletName =>
{
waitForWalletSelectionTask.SetResult(walletName);
};
WalletAdapterUI.SetActive(true);
var walletName = await waitForWalletSelectionTask.Task;
_currentWallet = Array.Find(Wallets, wallet => wallet.name == walletName);
}

protected override Task<Transaction> _SignTransaction(Transaction transaction)
Expand Down Expand Up @@ -219,12 +241,12 @@ public static void OnMessageSigned(string signature)
}

/// <summary>
/// Called from javascript when the wallet adapter script is loaded
/// Called from javascript when the wallet adapter script is loaded. Returns whether it's an XNFT or not.
/// </summary>
[MonoPInvokeCallback(typeof(Action<bool>))]
private static void OnScriptLoaded(bool success)
private static void OnWalletsInitialized(bool isXnft)
{
_loadedScriptTaskCompletionSource.SetResult(success);
_walletsInitializedTaskCompletionSource.SetResult(isXnft);
}

/// <summary>
Expand Down
29 changes: 15 additions & 14 deletions Runtime/codebase/Web3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,17 @@ private void Start()
if(w == null) return;
WalletBase = _web3AuthWallet;
};
// Try to login if running as an XNFT
LoginXNFT().AsUniTask().Forget();
}
catch (Exception e)
{
Debug.Log("We3Auth session not detected, " + e.Message);
}

#if UNITY_WEBGL
LoginXNFT().AsUniTask().Forget();
#endif


}

/// <summary>
Expand Down Expand Up @@ -191,21 +195,18 @@ public async Task<Account> LoginInWeb3Auth(Provider provider)
return acc;
}

/// <summary>
/// Login to backpack, if running as an xnft
/// </summary>
/// <returns></returns>

public async Task<Account> LoginXNFT()
{
var xnftWallet = new XNFTWallet(rpcCluster, customRpc, webSocketsRpc, false);
var acc = await xnftWallet.Login();
if (acc != null)
WalletBase = xnftWallet;
return acc;
var isXnft = await SolanaWalletAdapterWebGL.IsXnft();
if (isXnft)
{
Debug.Log("xNFT detected");
return await LoginWalletAdapter();
}
Debug.Log("xNFT not detected");
return null;
}



/// <summary>
/// Login using the solana wallet adapter
/// </summary>
Expand Down
Loading