Skip to content

Commit 1ab7cee

Browse files
committed
refactor: implement wallet detection and information handling in NodeArweaveWallet
1 parent 2faf90b commit 1ab7cee

File tree

2 files changed

+79
-10
lines changed

2 files changed

+79
-10
lines changed

src/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export class NodeArweaveWallet {
5656
private readonly config: NodeArweaveWalletConfig
5757
private readonly pendingRequests = new Map<string, PendingRequest>()
5858
private sseClient: http.ServerResponse | null = null
59+
private _walletName: string = ''
60+
private _walletVersion: string = ''
5961

6062
private address: string | null = null
6163
private browserConnected = false
@@ -157,6 +159,14 @@ export class NodeArweaveWallet {
157159
})
158160
}
159161

162+
get walletName(): string {
163+
return this._walletName
164+
}
165+
166+
get walletVersion(): string {
167+
return this._walletVersion
168+
}
169+
160170
/**
161171
* Requests a connection to the user's Arweave wallet with specified permissions.
162172
* This prompts the user to approve the connection in their browser wallet extension.
@@ -792,6 +802,7 @@ export class NodeArweaveWallet {
792802
'GET /': () => this.handleGetRoot(res),
793803
'GET /events': () => this.handleSSE(res),
794804
'POST /response': () => this.handleResponse(req, res),
805+
'POST /wallet-info': () => this.handleWalletInfo(req, res),
795806
}
796807

797808
const key = `${req.method} ${req.url}`
@@ -905,6 +916,25 @@ export class NodeArweaveWallet {
905916
})
906917
}
907918

919+
private handleWalletInfo(req: http.IncomingMessage, res: http.ServerResponse): void {
920+
this.readRequestBody(req, body => {
921+
try {
922+
const info = JSON.parse(body)
923+
924+
if (!this._walletName && !this._walletVersion) {
925+
this._walletName = info.name || 'Unknown Wallet'
926+
this._walletVersion = info.version || '1.0.0'
927+
928+
console.log(`📱 Wallet detected: ${this._walletName} (v${this._walletVersion})`)
929+
}
930+
931+
this.sendJSON(res, 200, { success: true })
932+
} catch (error: any) {
933+
this.sendJSON(res, 400, { error: error.message })
934+
}
935+
})
936+
}
937+
908938
private readRequestBody(req: http.IncomingMessage, callback: (body: string) => void): void {
909939
let body = ''
910940
req.on('data', chunk => (body += chunk.toString()))

src/signer/signer.js

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ let currentState = States.DISCONNECTED
7878
let walletAddress = null
7979
let eventSource = null
8080
const requestQueue = new Map()
81+
let isWalletDetected = false
82+
let isWalletDetectionLogged = false
8183

8284
// eslint-disable-next-line no-undef
8385
const arweave = Arweave.init({
@@ -270,6 +272,18 @@ async function sendResponse(id, result, error = null) {
270272
})
271273
}
272274

275+
async function sendWalletInfo(walletName, walletVersion) {
276+
try {
277+
await fetch('/wallet-info', {
278+
method: 'POST',
279+
headers: { 'Content-Type': 'application/json' },
280+
body: JSON.stringify({ name: walletName, version: walletVersion }),
281+
})
282+
} catch (error) {
283+
console.error('Failed to send wallet info:', error)
284+
}
285+
}
286+
273287
/**
274288
* Check if a wallet API method is supported
275289
* @param {string} methodName - The name of the wallet API method to check
@@ -689,21 +703,46 @@ function startEventStream() {
689703
}
690704

691705
// ==================== Initialization ====================
692-
window.addEventListener('load', () => {
693-
cacheDOMElements()
694-
initTheme()
695-
startEventStream()
706+
async function handleWalletDetection() {
707+
if (isWalletDetected && isWalletDetectionLogged) return
696708

697-
setTimeout(() => {
698-
if (window.arweaveWallet) {
699-
log(`${window.arweaveWallet?.name || 'Arweave'} wallet extension detected. Waiting for connection request...`)
700-
setState(States.DISCONNECTED, '⏳ Waiting for connection request...')
701-
} else {
709+
if (!window.arweaveWallet) {
710+
if (dom.log && !isWalletDetectionLogged) {
702711
setState(
703712
States.ERROR,
704713
'No Arweave wallet extension detected<br><small>Please install Wander or any other compatible wallet and refresh this page</small>',
705714
)
706715
log('Please install Wander or any other compatible wallet extension', 'error')
716+
isWalletDetectionLogged = true
707717
}
708-
}, 500)
718+
return
719+
}
720+
721+
const name = window.arweaveWallet?.walletName || 'Unknown Wallet'
722+
const version = window.arweaveWallet?.walletVersion || '1.0.0'
723+
724+
if (dom.log && !isWalletDetectionLogged) {
725+
log(`${name} (v${version}) detected. Waiting for connection request...`)
726+
setState(States.DISCONNECTED, '⏳ Waiting for connection request...')
727+
isWalletDetectionLogged = true
728+
}
729+
730+
if (isWalletDetected) return
731+
isWalletDetected = true
732+
await sendWalletInfo(name, version)
733+
}
734+
735+
// Initialize on load
736+
window.addEventListener('load', () => {
737+
cacheDOMElements()
738+
initTheme()
739+
startEventStream()
740+
741+
if (window.arweaveWallet) {
742+
handleWalletDetection()
743+
} else {
744+
setTimeout(handleWalletDetection, 500)
745+
}
709746
})
747+
748+
window.addEventListener('arweaveWalletLoaded', handleWalletDetection)

0 commit comments

Comments
 (0)