Skip to content

Commit

Permalink
Add Public Network(Resolver) connection capability ++ new checks on a…
Browse files Browse the repository at this point in the history
…dd node menu
  • Loading branch information
KaffinPX committed Aug 1, 2024
1 parent bc8cdcc commit 0caa670
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/contexts/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ export const currencies = {
export const defaultSettings: ISettings = {
version: 4,
nodes: [{
name: "Kaspa-NG EU",
address: "wss://eu-1.kaspa-ng.io/mainnet",
name: "Public network",
address: "mainnet",
locked: true
}, {
name: "Public network",
address: "testnet-10",
locked: true
}, {
name: "Public network",
address: "testnet-11",
locked: true
}, {
name: "Subsecond.wtf",
Expand Down
19 changes: 14 additions & 5 deletions src/pages/Wallet/Settings/Wallet/Node.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react"
import { useMemo, useState } from "react"
import { i18n } from "webextension-polyfill"
import { PlusIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
Expand All @@ -16,10 +16,19 @@ import { Input } from "@/components/ui/input"
import useSettings from "@/hooks/useSettings"

export default function Export () {
const { settings, updateSetting } = useSettings()

const [ name, setName ] = useState("")
const [ address, setAddress ] = useState("")

const { settings, updateSetting } = useSettings()
const addressValid = useMemo(() => {
try {
const parsedUrl = new URL(address)
return parsedUrl.protocol === "ws:" || parsedUrl.protocol === "wss:"
} catch (e) {
return false
}
}, [ address ])

return (
<Dialog onOpenChange={() => {
Expand All @@ -46,9 +55,9 @@ export default function Export () {
<DialogClose asChild>
<Button
className={"flex gap-2"}
disabled={name === "" || address === ""}
onClick={async () => {
await updateSetting('nodes', [
disabled={name === "" || !addressValid}
onClick={() => {
updateSetting('nodes', [
...settings.nodes,
{
name,
Expand Down
10 changes: 8 additions & 2 deletions src/wallet/kaspa/node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from "events"
import { RpcClient, ConnectStrategy, Transaction } from "@/../wasm"
import { RpcClient, ConnectStrategy, Transaction, Resolver, NetworkId } from "@/../wasm"

export default class Node extends EventEmitter {
kaspa: RpcClient
Expand Down Expand Up @@ -32,9 +32,15 @@ export default class Node extends EventEmitter {

async reconnect (nodeAddress: string) {
await this.kaspa.disconnect()

if (!nodeAddress.startsWith('ws')) {
if (!this.kaspa.resolver) this.kaspa.setResolver(new Resolver())
this.kaspa.setNetworkId(new NetworkId(nodeAddress))
}

await this.kaspa.connect({
blockAsyncConnect: true,
url: nodeAddress,
url: nodeAddress.startsWith('ws') ? nodeAddress : undefined,
strategy: ConnectStrategy.Retry,
timeoutDuration: 2000,
retryInterval: 1000,
Expand Down

0 comments on commit 0caa670

Please sign in to comment.