-
Notifications
You must be signed in to change notification settings - Fork 26
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ | ||
LexLuthr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { | ||
|
@@ -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"> | ||
|
@@ -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)}" | ||
|
@@ -140,6 +144,7 @@ class AllowList extends LitElement { | |
> | ||
Remove | ||
</button> | ||
</div> | ||
</td> | ||
</tr> | ||
` | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.