feat: add market stats endpoint, wallet integration, and browser wallet link

- Update devnet genesis timestamp to 1767000206
- Add market statistics endpoint with 24h volume, price change, and payment counts
- Add wallet balance and info API endpoints in exchange router
- Remove unused SessionDep dependencies from exchange endpoints
- Integrate real AITBC wallet extension connection in trade-exchange UI
- Add market data fetching with API fallback for price and volume display
- Add cache-busting query
This commit is contained in:
oib
2025-12-29 18:04:04 +01:00
parent b3fd0ea05c
commit 2cb2fbbeda
63 changed files with 4329 additions and 54 deletions

View File

@@ -0,0 +1,133 @@
# AITBC Wallet Extension for Firefox
A Firefox browser extension that provides AITBC wallet functionality for interacting with the AITBC Trade Exchange and other dApps.
## Differences from Chrome Version
This version is specifically built for Firefox with the following differences:
- Uses Manifest V2 (Firefox still requires V2 for full functionality)
- Uses `browser_action` instead of `action` (V2 syntax)
- Uses `chrome.runtime.connect()` for background script communication
- Background script uses persistent connections via ports
## Installation
### Development Installation
1. Clone this repository
2. Open Firefox and navigate to `about:debugging`
3. Click "This Firefox" in the left sidebar
4. Click "Load Temporary Add-on..."
5. Select the `manifest.json` file from the `aitbc-wallet-firefox` folder
### Production Installation
The extension will be published to the Firefox Add-on Store (AMO). Installation instructions will be available once published.
## Usage
The usage is identical to the Chrome version:
1. Install the AITBC Wallet extension
2. Navigate to https://aitbc.bubuit.net/Exchange
3. Toggle the switch from "Demo Mode" to "Real Mode"
4. Click "Connect AITBC Wallet"
5. Approve the connection request in the popup
## Features
- **Wallet Management**: Create new accounts or import existing private keys
- **Secure Storage**: Private keys are stored locally in Firefox's storage
- **dApp Integration**: Connect to AITBC Trade Exchange and other supported dApps
- **Transaction Signing**: Sign transactions and messages securely
- **Balance Tracking**: View your AITBC token balance
## API Reference
The extension injects a `window.aitbcWallet` object into supported dApps with the following methods:
### `aitbcWallet.connect()`
Connect the dApp to the wallet.
```javascript
const response = await aitbcWallet.connect();
console.log(response.address); // User's AITBC address
```
### `aitbcWallet.getAccount()`
Get the current account address.
```javascript
const address = await aitbcWallet.getAccount();
```
### `aitbcWallet.getBalance(address)`
Get the AITBC balance for an address.
```javascript
const balance = await aitbcWallet.getBalance('aitbc1...');
console.log(balance.amount); // Balance in AITBC
```
### `aitbcWallet.sendTransaction(to, amount, data)`
Send AITBC tokens to another address.
```javascript
const tx = await aitbcWallet.sendTransaction('aitbc1...', 100);
console.log(tx.hash); // Transaction hash
```
### `aitbcWallet.signMessage(message)`
Sign a message with the private key.
```javascript
const signature = await aitbcWallet.signMessage('Hello AITBC!');
```
## Security Considerations
- Private keys are stored locally in Firefox's storage
- Always verify you're on the correct domain before connecting
- Never share your private key with anyone
- Keep your browser and extension updated
## Development
To modify the extension:
1. Make changes to the source files
2. Go to `about:debugging` in Firefox
3. Find "AITBC Wallet" and click "Reload"
4. Test your changes
## File Structure
```
aitbc-wallet-firefox/
├── manifest.json # Extension configuration (Manifest V2)
├── background.js # Background script for wallet operations
├── content.js # Content script for dApp communication
├── injected.js # Script injected into dApps
├── popup.html # Extension popup UI
├── popup.js # Popup logic
├── icons/ # Extension icons
└── README.md # This file
```
## Firefox-Specific Notes
- Firefox requires Manifest V2 for extensions that use content scripts in this manner
- The `browser_action` API is used instead of the newer `action` API
- Background scripts use port-based communication for better performance
- Storage APIs use `chrome.storage` which is compatible with Firefox
## Troubleshooting
### Extension not loading
- Ensure you're loading the `manifest.json` file, not the folder
- Check the Browser Console for error messages (`Ctrl+Shift+J`)
### dApp connection not working
- Refresh the dApp page after installing/updating the extension
- Check that the site is in the `matches` pattern in manifest.json
- Look for errors in the Browser Console
### Permission errors
- Firefox may show additional permission prompts
- Make sure to allow all requested permissions when installing

View File

@@ -0,0 +1,153 @@
// Background script for Firefox extension
// Handles messages from content scripts and manages wallet state
let currentPort = null;
// Listen for connection from content script
chrome.runtime.onConnect.addListener(function(port) {
if (port.name === "aitbc-wallet") {
currentPort = port;
port.onMessage.addListener(function(request) {
handleWalletRequest(request, port);
});
port.onDisconnect.addListener(function() {
currentPort = null;
});
}
});
// Handle wallet requests from dApps
async function handleWalletRequest(request, port) {
const { method, params, id } = request;
try {
switch (method) {
case 'connect':
const response = await handleConnect();
port.postMessage({ id, result: response });
break;
case 'accounts':
const accounts = await getAccounts();
port.postMessage({ id, result: accounts });
break;
case 'getBalance':
const balance = await getBalance(params.address);
port.postMessage({ id, result: balance });
break;
case 'sendTransaction':
const txResult = await sendTransaction(params);
port.postMessage({ id, result: txResult });
break;
case 'signMessage':
const signature = await signMessage(params.message);
port.postMessage({ id, result: signature });
break;
default:
port.postMessage({ id, error: 'Unknown method: ' + method });
}
} catch (error) {
port.postMessage({ id, error: error.message });
}
}
// Handle connection request from dApp
async function handleConnect() {
// Get current account
const result = await chrome.storage.local.get(['currentAccount']);
if (!result.currentAccount) {
throw new Error('No account found. Please create or import an account first.');
}
// Show connection prompt (in a real implementation, this would show a proper UI)
const connected = confirm(`Allow this site to connect to your AITBC Wallet?\n\nAddress: ${result.currentAccount.address}`);
if (!connected) {
throw new Error('User rejected connection');
}
return {
success: true,
address: result.currentAccount.address
};
}
// Get all accounts
async function getAccounts() {
const result = await chrome.storage.local.get(['accounts']);
const accounts = result.accounts || [];
return accounts.map(acc => acc.address);
}
// Get balance for an address
async function getBalance(address) {
// In a real implementation, this would query the blockchain
// For demo, return stored balance
const result = await chrome.storage.local.get(['accounts']);
const accounts = result.accounts || [];
const account = accounts.find(acc => acc.address === address);
return {
address: address,
balance: account ? account.balance || 0 : 0,
symbol: 'AITBC'
};
}
// Send transaction
async function sendTransaction(params) {
// In a real implementation, this would create, sign, and broadcast a transaction
const { to, amount, data } = params;
// Get current account
const result = await chrome.storage.local.get(['currentAccount']);
const account = result.currentAccount;
if (!account) {
throw new Error('No account connected');
}
// Confirm transaction
const confirmed = confirm(`Send ${amount} AITBC to ${to}?\n\nFrom: ${account.address}`);
if (!confirmed) {
throw new Error('Transaction rejected');
}
// Return mock transaction hash
return {
hash: '0x' + Array.from(crypto.getRandomValues(new Uint8Array(32)), b => b.toString(16).padStart(2, '0')).join(''),
status: 'pending'
};
}
// Sign message
async function signMessage(message) {
// Get current account
const result = await chrome.storage.local.get(['currentAccount']);
const account = result.currentAccount;
if (!account) {
throw new Error('No account connected');
}
// Confirm signing
const confirmed = confirm(`Sign the following message?\n\n"${message}"\n\nAccount: ${account.address}`);
if (!confirmed) {
throw new Error('Message signing rejected');
}
// In a real implementation, this would sign with the private key
// For demo, return a mock signature
const encoder = new TextEncoder();
const data = encoder.encode(message + account.privateKey);
const hash = await crypto.subtle.digest('SHA-256', data);
return Array.from(new Uint8Array(hash), b => b.toString(16).padStart(2, '0')).join('');
}

View File

@@ -0,0 +1,35 @@
// Content script for AITBC Wallet Firefox extension
(function() {
// Inject the wallet API into the page
const script = document.createElement('script');
script.src = chrome.runtime.getURL('injected.js');
script.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(script);
// Create a port to background script
const port = chrome.runtime.connect({ name: "aitbc-wallet" });
// Listen for messages from the injected script
window.addEventListener('message', function(event) {
// Only accept messages from our own window
if (event.source !== window) return;
if (event.data.type && event.data.type === 'AITBC_WALLET_REQUEST') {
// Forward the request to the background script
port.postMessage(event.data);
}
});
// Listen for responses from background script
port.onMessage.addListener(function(response) {
// Send the response back to the page
window.postMessage({
type: 'AITBC_WALLET_RESPONSE',
id: response.id,
result: response.result,
error: response.error
}, '*');
});
})();

View File

@@ -0,0 +1,106 @@
// Injected script that provides the AITBC wallet API to the dApp
(function() {
// Create the wallet API object
const aitbcWallet = {
// Check if wallet is available
isAvailable: function() {
return true;
},
// Connect to wallet
connect: async function() {
return new Promise((resolve, reject) => {
const requestId = Date.now().toString();
// Send request to content script
window.postMessage({
type: 'AITBC_WALLET_REQUEST',
id: requestId,
method: 'connect'
}, '*');
// Listen for response
const messageHandler = function(event) {
if (event.data.type === 'AITBC_WALLET_RESPONSE' && event.data.id === requestId) {
window.removeEventListener('message', messageHandler);
if (event.data.response.error) {
reject(new Error(event.data.response.error));
} else {
resolve(event.data.response);
}
}
};
window.addEventListener('message', messageHandler);
// Timeout after 30 seconds
setTimeout(() => {
window.removeEventListener('message', messageHandler);
reject(new Error('Connection timeout'));
}, 30000);
});
},
// Get account address
getAccount: async function() {
const accounts = await this.request({ method: 'accounts' });
return accounts[0];
},
// Get balance
getBalance: async function(address) {
return this.request({ method: 'getBalance', params: { address } });
},
// Send transaction
sendTransaction: async function(to, amount, data = null) {
return this.request({
method: 'sendTransaction',
params: { to, amount, data }
});
},
// Sign message
signMessage: async function(message) {
return this.request({ method: 'signMessage', params: { message } });
},
// Generic request method
request: async function(payload) {
return new Promise((resolve, reject) => {
const requestId = Date.now().toString();
window.postMessage({
type: 'AITBC_WALLET_REQUEST',
id: requestId,
method: payload.method,
params: payload.params || {}
}, '*');
const messageHandler = function(event) {
if (event.data.type === 'AITBC_WALLET_RESPONSE' && event.data.id === requestId) {
window.removeEventListener('message', messageHandler);
if (event.data.response.error) {
reject(new Error(event.data.response.error));
} else {
resolve(event.data.response);
}
}
};
window.addEventListener('message', messageHandler);
setTimeout(() => {
window.removeEventListener('message', messageHandler);
reject(new Error('Request timeout'));
}, 30000);
});
}
};
// Inject the wallet API into the window object
window.aitbcWallet = aitbcWallet;
// Fire an event to notify the dApp that the wallet is ready
window.dispatchEvent(new Event('aitbcWalletReady'));
})();

View File

@@ -0,0 +1,46 @@
{
"manifest_version": 2,
"name": "AITBC Wallet",
"version": "1.0.0",
"description": "AITBC Browser Wallet for trading and managing AITBC tokens",
"permissions": [
"storage",
"activeTab"
],
"content_scripts": [
{
"matches": ["https://aitbc.bubuit.net/*", "http://localhost:3002/*"],
"js": ["content.js"],
"run_at": "document_start"
}
],
"browser_action": {
"default_popup": "popup.html",
"default_title": "AITBC Wallet",
"default_icon": {
"16": "icons/icon-16.png",
"32": "icons/icon-32.png",
"48": "icons/icon-48.png",
"128": "icons/icon-128.png"
}
},
"web_accessible_resources": [
["injected.js"]
],
"icons": {
"16": "icons/icon-16.png",
"32": "icons/icon-32.png",
"48": "icons/icon-48.png",
"128": "icons/icon-128.png"
},
"background": {
"scripts": ["background.js"],
"persistent": false
}
}

View File

@@ -0,0 +1,109 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
width: 350px;
padding: 20px;
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #f97316 0%, #ea580c 100%);
color: white;
}
.header {
display: flex;
align-items: center;
margin-bottom: 20px;
}
.logo {
width: 40px;
height: 40px;
margin-right: 10px;
background: white;
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
color: #f97316;
}
.wallet-info {
background: rgba(255, 255, 255, 0.1);
padding: 15px;
border-radius: 8px;
margin-bottom: 15px;
}
.address {
font-family: monospace;
font-size: 12px;
word-break: break-all;
margin: 5px 0;
}
.balance {
font-size: 24px;
font-weight: bold;
margin: 10px 0;
}
.actions {
display: flex;
flex-direction: column;
gap: 10px;
}
button {
background: white;
color: #f97316;
border: none;
padding: 10px;
border-radius: 6px;
font-weight: bold;
cursor: pointer;
transition: all 0.2s;
}
button:hover {
transform: translateY(-1px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.transactions {
margin-top: 20px;
max-height: 200px;
overflow-y: auto;
}
.tx-item {
background: rgba(255, 255, 255, 0.1);
padding: 8px;
border-radius: 4px;
margin-bottom: 5px;
font-size: 12px;
}
</style>
</head>
<body>
<div class="header">
<div class="logo">AITBC</div>
<h1>Wallet</h1>
</div>
<div class="wallet-info">
<div>Account Address:</div>
<div class="address" id="accountAddress">Not connected</div>
<div class="balance" id="balance">0 AITBC</div>
</div>
<div class="actions">
<button onclick="createAccount()">Create New Account</button>
<button onclick="importAccount()">Import Private Key</button>
<button onclick="sendTokens()">Send Tokens</button>
<button onclick="receiveTokens()">Receive Tokens</button>
<button onclick="viewOnExplorer()">View on Explorer</button>
</div>
<div class="transactions">
<h3>Recent Transactions</h3>
<div id="transactionList">
<div class="tx-item">No transactions yet</div>
</div>
</div>
<script src="popup.js"></script>
</body>
</html>

View File

@@ -0,0 +1,162 @@
// Popup script for AITBC Wallet extension
let currentAccount = null;
let accounts = [];
// Load wallet data on popup open
document.addEventListener('DOMContentLoaded', async function() {
await loadWalletData();
updateUI();
});
// Load wallet data from storage
async function loadWalletData() {
const result = await chrome.storage.local.get(['accounts', 'currentAccount']);
accounts = result.accounts || [];
currentAccount = result.currentAccount || null;
}
// Save wallet data to storage
async function saveWalletData() {
await chrome.storage.local.set({
accounts: accounts,
currentAccount: currentAccount
});
}
// Update UI with current wallet state
function updateUI() {
const addressEl = document.getElementById('accountAddress');
const balanceEl = document.getElementById('balance');
if (currentAccount) {
addressEl.textContent = currentAccount.address;
balanceEl.textContent = `${currentAccount.balance || 0} AITBC`;
} else {
addressEl.textContent = 'Not connected';
balanceEl.textContent = '0 AITBC';
}
}
// Create a new account
async function createAccount() {
// Generate a new private key and address
const privateKey = generatePrivateKey();
const address = await generateAddress(privateKey);
const newAccount = {
address: address,
privateKey: privateKey,
balance: 0,
created: new Date().toISOString()
};
accounts.push(newAccount);
currentAccount = newAccount;
await saveWalletData();
updateUI();
alert('New account created! Please save your private key securely.');
}
// Import account from private key
async function importAccount() {
const privateKey = prompt('Enter your private key:');
if (!privateKey) return;
try {
const address = await generateAddress(privateKey);
// Check if account already exists
const existing = accounts.find(a => a.address === address);
if (existing) {
currentAccount = existing;
} else {
currentAccount = {
address: address,
privateKey: privateKey,
balance: 0,
created: new Date().toISOString()
};
accounts.push(currentAccount);
}
await saveWalletData();
updateUI();
alert('Account imported successfully!');
} catch (error) {
alert('Invalid private key!');
}
}
// Send tokens
async function sendTokens() {
if (!currentAccount) {
alert('Please create or import an account first!');
return;
}
const to = prompt('Send to address:');
const amount = prompt('Amount:');
if (!to || !amount) return;
// In a real implementation, this would create and sign a transaction
alert(`Would send ${amount} AITBC to ${to}`);
}
// Receive tokens
function receiveTokens() {
if (!currentAccount) {
alert('Please create or import an account first!');
return;
}
alert(`Your receiving address:\n${currentAccount.address}`);
}
// View on explorer
function viewOnExplorer() {
if (!currentAccount) {
alert('Please create or import an account first!');
return;
}
chrome.tabs.create({ url: `https://aitbc.bubuit.net/explorer/address/${currentAccount.address}` });
}
// Generate a random private key (demo only)
function generatePrivateKey() {
const array = new Uint8Array(32);
crypto.getRandomValues(array);
return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
}
// Generate address from private key (demo only)
async function generateAddress(privateKey) {
// In a real implementation, this would derive the address from the private key
// using the appropriate cryptographic algorithm
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(privateKey));
return 'aitbc1' + Array.from(new Uint8Array(hash), b => b.toString(16).padStart(2, '0')).join('').substring(0, 40);
}
// Listen for connection requests from dApps
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.method === 'connect') {
// Show connection dialog
const connected = confirm(`Allow this site to connect to your AITBC Wallet?`);
if (connected && currentAccount) {
sendResponse({
success: true,
address: currentAccount.address
});
} else {
sendResponse({
success: false,
error: 'User rejected connection'
});
}
}
return true; // Keep the message channel open for async response
});