Skip to content

wallet friendly names #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2025
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
4 changes: 4 additions & 0 deletions harmony/harmonydb/sql/20241105-walletnames.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE wallet_names (
wallet VARCHAR PRIMARY KEY,
name VARCHAR(60) NOT NULL UNIQUE
);
20 changes: 3 additions & 17 deletions web/api/webrpc/market_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/builtin/v15/market"

"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/node/modules/dtypes"
)

Expand Down Expand Up @@ -112,11 +111,7 @@ func (a *WebRPC) SetClientFilters(ctx context.Context, name string, active bool,
if err != nil {
return xerrors.Errorf("invalid wallet address: %w", err)
}
client, err := a.deps.Chain.StateLookupID(ctx, w, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("wallet not found: %w", err)
}
clients = append(clients, client.String())
clients = append(clients, w.String())
}
}

Expand Down Expand Up @@ -258,11 +253,7 @@ func (a *WebRPC) AddClientFilters(ctx context.Context, name string, active bool,
if err != nil {
return xerrors.Errorf("invalid wallet address: %w", err)
}
client, err := a.deps.Chain.StateLookupID(ctx, w, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("wallet not found: %w", err)
}
clients = append(clients, client.String())
clients = append(clients, w.String())
}
}

Expand Down Expand Up @@ -382,12 +373,7 @@ func (a *WebRPC) AddAllowDenyList(ctx context.Context, wallet string, status boo
return xerrors.Errorf("invalid wallet address: %w", err)
}

client, err := a.deps.Chain.StateLookupID(ctx, w, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("wallet not found: %w", err)
}

n, err := a.deps.DB.Exec(ctx, "INSERT INTO market_allow_list (wallet, status) VALUES ($1, $2)", client.String(), status)
n, err := a.deps.DB.Exec(ctx, "INSERT INTO market_allow_list (wallet, status) VALUES ($1, $2)", w.String(), status)
if err != nil {
return err
}
Expand Down
101 changes: 101 additions & 0 deletions web/api/webrpc/wallet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package webrpc

import (
"context"
"errors"
"sync"

"github.com/filecoin-project/curio/harmony/harmonydb"
)

var walletOnce sync.Once
var walletFriendlyNames = map[string]string{}
var walletFriendlyNamesLock sync.Mutex

func (a *WebRPC) WalletName(ctx context.Context, id string) (string, error) {
walletOnce.Do(func() {
populateWalletFriendlyNames(a.deps.DB)
})
walletFriendlyNamesLock.Lock()
defer walletFriendlyNamesLock.Unlock()
name, ok := walletFriendlyNames[id]
if ok {
return name, nil
}
return id, nil
}

func (a *WebRPC) WalletNameChange(ctx context.Context, wallet, newName string) error {
if len(newName) == 0 {
return errors.New("name cannot be empty")
}
_, err := a.deps.DB.Exec(ctx, `UPDATE wallet_names SET name = $1 WHERE wallet = $2`, newName, wallet)
if err != nil {
log.Errorf("failed to set wallet name for %s: %s", wallet, err)
return err
}
walletFriendlyNamesLock.Lock()
defer walletFriendlyNamesLock.Unlock()
walletFriendlyNames[wallet] = newName
return nil
}

func populateWalletFriendlyNames(db *harmonydb.DB) {
// Get all wallet from DB
var idNames []struct {
Wallet string `db:"wallet"`
Name string `db:"name"`
}

err := db.Select(context.Background(), &idNames, `SELECT wallet, name FROM wallet_names`)
if err != nil {
log.Errorf("failed to get wallet names: %s", err)
return
}

walletFriendlyNamesLock.Lock()
defer walletFriendlyNamesLock.Unlock()
for _, idName := range idNames {
walletFriendlyNames[idName.Wallet] = idName.Name
}
}

func (a *WebRPC) WalletNames(ctx context.Context) (map[string]string, error) {
walletOnce.Do(func() {
populateWalletFriendlyNames(a.deps.DB)
})
walletFriendlyNamesLock.Lock()
defer walletFriendlyNamesLock.Unlock()
return walletFriendlyNames, nil
}

func (a *WebRPC) WalletAdd(ctx context.Context, wallet, name string) error {
if len(name) == 0 {
return errors.New("name cannot be empty")
}
if len(wallet) == 0 {
return errors.New("wallet cannot be empty")
}
_, err := a.deps.DB.Exec(ctx, `INSERT INTO wallet_names (wallet, name) VALUES ($1, $2)`, wallet, name)
if err != nil {
log.Errorf("failed to add wallet name for %s: %s", wallet, err)
return err
}

walletFriendlyNamesLock.Lock()
walletFriendlyNames[wallet] = name
defer walletFriendlyNamesLock.Unlock()
return nil
}

func (a *WebRPC) WalletRemove(ctx context.Context, wallet string) error {
_, err := a.deps.DB.Exec(ctx, `DELETE FROM wallet_names WHERE wallet = $1`, wallet)
if err != nil {
log.Errorf("failed to remove wallet name for %s: %s", wallet, err)
return err
}
walletFriendlyNamesLock.Lock()
defer walletFriendlyNamesLock.Unlock()
delete(walletFriendlyNames, wallet)
return nil
}
58 changes: 58 additions & 0 deletions web/static/lib/cu-wallet.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { LitElement, html } from 'https://cdn.jsdelivr.net/gh/lit/dist@3/all/lit-all.min.js';
import RPCCall from '/lib/jsonrpc.mjs';
import '/lib/clipboard-copy.mjs';

class CuWallet extends LitElement {
static properties = {
wallet_id: { type: String },
name: { state: true },
havername: { state: true }
};

constructor() {
super();
this.wallet_id = '';
this.name = '';
this.havename = false;
}

connectedCallback() {
super.connectedCallback();
this.loadWallet();
}

async loadWallet() {
if (!this.wallet_id) return;

try {
const result = await RPCCall('WalletName', [this.wallet_id]);
console.log('WalletName result:', result);
this.name = result || this.wallet_id;
this.havename = (this.name !== this.wallet_id);
} catch (err) {
console.error('Error during WalletName operation:', err);
this.name = this.wallet_id; // fallback
}
}

createRenderRoot() {
// Render in light DOM so the text can be styled normally and picked up by parent CSS
return this;
}

render() {
if (!this.havename){
const shortened = `${this.wallet_id.slice(0, 6)}...${this.wallet_id.slice(-6)}`;
return html`
<span title="${this.wallet_id}">${shortened}</span>
<clipboard-copy .text=${this.wallet_id}></clipboard-copy>
</span>
`;
}
return html`
<a href="/pages/wallet/?id=${this.wallet_id}"><span title="${this.wallet_id}">${this.name}</span></a>
`;
}
}

customElements.define('cu-wallet', CuWallet);
13 changes: 7 additions & 6 deletions web/static/pages/actor/actor-detail.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LitElement, html, css } from 'https://cdn.jsdelivr.net/gh/lit/dist@3/all/lit-all.min.js';
import '/actor-summary.mjs'; // <sector-expirations>
import RPCCall from '/lib/jsonrpc.mjs';
import '/lib/cu-wallet.mjs';

customElements.define('actor-detail', class Actor extends LitElement {
connectedCallback() {
Expand Down Expand Up @@ -138,19 +139,19 @@ customElements.define('actor-detail', class Actor extends LitElement {
</tr>
<tr>
<td>Owner Address:</td>
<td>${actorInfo.OwnerAddress}</td>
<td><cu-wallet wallet_id=${actorInfo.OwnerAddress}></cu-wallet></td>
</tr>
<tr>
<td>Beneficiary:</td>
<td>${actorInfo.Beneficiary}</td>
<td><cu-wallet wallet_id=${actorInfo.Beneficiary}></cu-wallet></td>
</tr>
<tr>
<td>Worker Address:</td>
<td>${actorInfo.WorkerAddress}</td>
<td><cu-wallet wallet_id=${actorInfo.WorkerAddress}></cu-wallet></td>
</tr>
<tr>
<td>Peer ID:</td>
<td>${actorInfo.PeerID}</td>
<td><cu-wallet wallet_id=${actorInfo.PeerID}></cu-wallet></td>
</tr>

<tr>
Expand Down Expand Up @@ -206,7 +207,7 @@ customElements.define('actor-detail', class Actor extends LitElement {
${actorInfo.PendingOwnerAddress ? html`
<tr>
<td>PendingOwnerAddress:</td>
<td>${actorInfo.PendingOwnerAddress}</td>
<td><cu-wallet wallet_id=${actorInfo.PendingOwnerAddress}></cu-wallet></td>
</tr>
`
: null
Expand Down Expand Up @@ -265,7 +266,7 @@ customElements.define('actor-detail', class Actor extends LitElement {
${actorInfo.Wallets.map(wallet => html`
<tr>
<td>${wallet.Type}</td>
<td>${wallet.Address}</td>
<td><cu-wallet wallet_id=${wallet.Address}></cu-wallet></td>
<td>${wallet.Balance}</td>
</tr>
`)
Expand Down
8 changes: 3 additions & 5 deletions web/static/pages/actor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<script type="module" src="/ux/curio-ux.mjs"></script>
<script type="module" src="/ux/components/Drawer.mjs"></script>
<script type="module" src="actor-detail.mjs"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw=="
crossorigin="anonymous" />
</head>
<body style="visibility:hidden" data-bs-theme="dark">
<curio-ux>
Expand All @@ -25,10 +28,5 @@ <h1>Miner Info</h1>
</section>
</div>
</curio-ux>
<!--<script>-->
<!-- let d = document.createElement('actor-detail');-->
<!-- d.id = new URLSearchParams(window.location.search).get('id');-->
<!-- document.querySelector('curio-ux').appendChild(d);-->
<!--</script>-->
</body>
</html>
2 changes: 1 addition & 1 deletion web/static/pages/ipni/ipni_status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class IpniStatus extends LitElement {
data-bs-parent="#ipniStatusAccordion"
>
<div class="accordion-body">
<p><strong>PeerID:</strong> ${provider.peer_id}</p>
<p><strong>PeerID:</strong>${provider.peer_id}></p>
<p><strong>Head:</strong> <a href="/pages/ipni/?ad_cid=${provider.head}">${provider.head}</a></p>
${provider.sync_status && provider.sync_status.length > 0 ? provider.sync_status.map((status) => html`
<div class="sync-status">
Expand Down
7 changes: 6 additions & 1 deletion web/static/pages/market-settings/allow-list.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LitElement, html, css } from 'https://cdn.jsdelivr.net/gh/lit/dist@3/all/lit-all.min.js';
import RPCCall from '/lib/jsonrpc.mjs';
import '/lib/cu-wallet.mjs';

class AllowList extends LitElement {
static properties = {
Expand Down Expand Up @@ -89,8 +90,10 @@ class AllowList extends LitElement {
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous"
/>
<link rel="stylesheet" href="/ux/main.css" onload="document.body.style.visibility = 'initial'" />
<div class="container">
<h2>Allow/Deny List
<button class="info-btn">
Expand Down Expand Up @@ -125,9 +128,10 @@ class AllowList extends LitElement {
${this.allowList.map(
(entry) => html`
<tr>
<td>${entry.wallet}</td>
<td><cu-wallet wallet_id=${entry.wallet}></cu-wallet></td>
<td>${entry.status ? 'Allow' : 'Deny'}</td>
<td>
<div class="d-flex gap-2">
<button
class="btn btn-secondary btn-sm"
@click="${() => this.editAllowListEntry(entry)}"
Expand All @@ -140,6 +144,7 @@ class AllowList extends LitElement {
>
Remove
</button>
</div>
</td>
</tr>
`
Expand Down
24 changes: 22 additions & 2 deletions web/static/pages/market-settings/client-filters.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LitElement, html, css } from 'https://cdn.jsdelivr.net/gh/lit/dist@3/all/lit-all.min.js';
import RPCCall from '/lib/jsonrpc.mjs';
import '/lib/cu-wallet.mjs';

class ClientFilters extends LitElement {
static properties = {
Expand Down Expand Up @@ -108,6 +109,7 @@ class ClientFilters extends LitElement {
rel="stylesheet"
crossorigin="anonymous"
/>
<link rel="stylesheet" href="/ux/main.css" onload="document.body.style.visibility = 'initial'" />
<div class="container">
<h2>Client Filters
<button class="info-btn">
Expand Down Expand Up @@ -150,13 +152,30 @@ class ClientFilters extends LitElement {
<tr>
<td>${filter.name}</td>
<td>${filter.active ? 'Yes' : 'No'}</td>
<td>${(filter.wallets || []).join(', ')}</td>
<td>${(filter.peers || []).join(', ')}</td>
<td>
${(filter.wallets || []).length === 0
? html`-`
: filter.wallets.map(
(w, i, arr) => html`
<cu-wallet wallet_id="${w}"></cu-wallet>${i < arr.length - 1 ? ', ' : ''}
`
)}
</td>
<td>
${(filter.peers || []).length === 0
? html`-`
: filter.peers.map(
(p, i, arr) => html`
<cu-wallet wallet_id="${p}"></cu-wallet>${i < arr.length - 1 ? ', ' : ''}
`
)}
</td>
<td>${(filter.pricing_filters || []).join(', ')}</td>
<td>${filter.max_deals_per_hour}</td>
<td>${this.formatBytes(filter.max_deal_size_per_hour)}</td>
<td>${filter.info || ''}</td>
<td>
<div class="d-flex gap-2">
<button
class="btn btn-secondary btn-sm"
@click="${() => this.editClientFilter(filter)}"
Expand All @@ -169,6 +188,7 @@ class ClientFilters extends LitElement {
>
Remove
</button>
</div>
</td>
</tr>
`
Expand Down
1 change: 1 addition & 0 deletions web/static/pages/market-settings/defaults.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class DefaultMarketFilters extends LitElement {
rel="stylesheet"
crossorigin="anonymous"
/>
<link rel="stylesheet" href="/ux/main.css" onload="document.body.style.visibility = 'initial'" />
<div class="container">
<h2>Filter Settings</h2>
<table class="table table-dark table-striped">
Expand Down
Loading