Skip to content

Commit

Permalink
show to nickname in sign prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Thykof committed Oct 24, 2023
1 parent f1fa1fa commit 9762627
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 42 deletions.
64 changes: 37 additions & 27 deletions internal/handler/wallet/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,23 @@ const (
)

type PromptRequestSignData struct {
Description string
Fees string
OperationType string
OperationID uint64
Coins string
Address string
Function string
MaxCoins string
MaxGas string
Expiry uint64
WalletAddress string
Nickname string
RollCount uint64
RecipientAddress string
Amount string
PlainText string
Description string
Fees string
OperationType string
OperationID uint64
Coins string
Address string
Function string
MaxCoins string
MaxGas string
Expiry uint64
WalletAddress string
Nickname string
RollCount uint64
RecipientAddress string
RecipientNickname string
Amount string
PlainText string
}

// NewSign instantiates a sign Handler
Expand Down Expand Up @@ -258,8 +259,7 @@ func (w *walletSign) getPromptRequest(msgToSign string, acc *account.Account, de
if err != nil {
return nil, errors.Wrap(err, "failed to decode plainText message from b64")

Check warning on line 260 in internal/handler/wallet/sign.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/wallet/sign.go#L260

Added line #L260 was not covered by tests
}
promptRequest = w.prepareplainTextPromptRequest(string(decodedMsg), acc, address, description)

promptRequest = w.preparePlainTextPromptRequest(string(decodedMsg), acc, address, description)

Check warning on line 262 in internal/handler/wallet/sign.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/wallet/sign.go#L262

Added line #L262 was not covered by tests
}
}

Expand Down Expand Up @@ -356,23 +356,33 @@ func (w *walletSign) prepareTransactionPromptRequest(
fees uint64,
expiry uint64,
) prompt.PromptRequest {
var recipientNickname string

recipientAcc, err := w.prompterApp.App().Wallet.GetAccountFromAddress(msg.RecipientAddress)
if err != nil {
recipientNickname = ""
} else {
recipientNickname = recipientAcc.Nickname
}

Check warning on line 366 in internal/handler/wallet/sign.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/wallet/sign.go#L359-L366

Added lines #L359 - L366 were not covered by tests

return prompt.PromptRequest{
Action: walletapp.Sign,
Msg: fmt.Sprintf("Unprotect wallet %s", acc.Nickname),

Check warning on line 370 in internal/handler/wallet/sign.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/wallet/sign.go#L370

Added line #L370 was not covered by tests
Data: PromptRequestSignData{
Description: description,
Fees: strconv.FormatUint(fees, 10),
OperationType: "Transaction",
RecipientAddress: msg.RecipientAddress,
Amount: strconv.FormatUint(msg.Amount, 10),
Expiry: expiry,
WalletAddress: address,
Nickname: acc.Nickname,
Description: description,
Fees: strconv.FormatUint(fees, 10),
OperationType: "Transaction",
RecipientAddress: msg.RecipientAddress,
RecipientNickname: recipientNickname,
Amount: strconv.FormatUint(msg.Amount, 10),
Expiry: expiry,
WalletAddress: address,
Nickname: acc.Nickname,

Check warning on line 380 in internal/handler/wallet/sign.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/wallet/sign.go#L372-L380

Added lines #L372 - L380 were not covered by tests
},
}
}

func (s *walletSign) prepareplainTextPromptRequest(
func (s *walletSign) preparePlainTextPromptRequest(
plainText string,
acc *account.Account,
address string,
Expand Down
22 changes: 16 additions & 6 deletions internal/handler/wallet/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,26 @@ func (t *transferCoin) Handle(params operations.TransferCoinParams) middleware.R
}

Check warning on line 51 in internal/handler/wallet/transfer.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/wallet/transfer.go#L50-L51

Added lines #L50 - L51 were not covered by tests
address := string(addressBytes)

var recipientNickname string

recipientAcc, err := t.prompterApp.App().Wallet.GetAccountFromAddress(*params.Body.RecipientAddress)
if err != nil {
recipientNickname = ""
} else {
recipientNickname = recipientAcc.Nickname

Check warning on line 60 in internal/handler/wallet/transfer.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/wallet/transfer.go#L60

Added line #L60 was not covered by tests
}

promptRequest := prompt.PromptRequest{
Action: walletapp.Sign,
Msg: fmt.Sprintf("Unprotect wallet %s", acc.Nickname),
Data: PromptRequestSignData{
Fees: string(params.Body.Fee),
OperationType: "Transaction",
RecipientAddress: *params.Body.RecipientAddress,
Amount: string(params.Body.Amount),
WalletAddress: address,
Nickname: acc.Nickname,
Fees: string(params.Body.Fee),
OperationType: "Transaction",
RecipientAddress: *params.Body.RecipientAddress,
RecipientNickname: recipientNickname,
Amount: string(params.Body.Amount),
WalletAddress: address,
Nickname: acc.Nickname,
},
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ func (w *Wallet) GetAccount(nickname string) (*account.Account, error) {
return acc, nil
}

func (w *Wallet) GetAccountFromAddress(needle string) (*account.Account, error) {
for _, acc := range w.accounts {
address, err := acc.Address.MarshalText()
if err != nil {
return nil, err
}

Check warning on line 177 in pkg/wallet/wallet.go

View check run for this annotation

Codecov / codecov/patch

pkg/wallet/wallet.go#L172-L177

Added lines #L172 - L177 were not covered by tests

if needle == string(address) {
return acc, nil
}

Check warning on line 181 in pkg/wallet/wallet.go

View check run for this annotation

Codecov / codecov/patch

pkg/wallet/wallet.go#L179-L181

Added lines #L179 - L181 were not covered by tests
}

return nil, AccountNotFoundError

Check warning on line 184 in pkg/wallet/wallet.go

View check run for this annotation

Codecov / codecov/patch

pkg/wallet/wallet.go#L184

Added line #L184 was not covered by tests
}

// Delete an account from the wallet
func (w *Wallet) DeleteAccount(nickname string) error {
if w.accounts[nickname] == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { WindowSetSize } from '@wailsjs/runtime/runtime';

import { PromptRequestData } from '../Sign';
import Intl from '@/i18n/i18n';
import { formatStandard, masToken, maskAddress, Unit } from '@/utils';
import {
formatStandard,
masToken,
maskAddress,
Unit,
maskNickname,
} from '@/utils';

export function ExecuteSC(props: PromptRequestData) {
const {
Expand All @@ -22,7 +28,7 @@ export function ExecuteSC(props: PromptRequestData) {
<div className="flex w-full justify-between">
<p>{Intl.t('password-prompt.sign.from')}</p>
<div className="flex flex-col">
<p className="mas-menu-default">{Nickname}</p>
<p className="mas-menu-default">{maskNickname(Nickname)}</p>
<p className="mas-caption">{maskAddress(WalletAddress)}</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FiChevronDown, FiChevronUp, FiInfo } from 'react-icons/fi';

import { PromptRequestData } from '../Sign';
import Intl from '@/i18n/i18n';
import { maskAddress } from '@/utils';
import { maskAddress, maskNickname } from '@/utils';

export function PlainText(props: PromptRequestData) {
const { PlainText, DisplayData, WalletAddress, Description, Nickname } =
Expand All @@ -23,7 +23,7 @@ export function PlainText(props: PromptRequestData) {
<div className="flex w-full justify-between">
<p>{Intl.t('password-prompt.sign.from')}</p>
<div className="flex flex-col">
<p className="mas-menu-default">{Nickname}</p>
<p className="mas-menu-default">{maskNickname(Nickname)}</p>
<p className="mas-caption">{maskAddress(WalletAddress)}</p>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions wails-frontend/src/pages/PasswordPromptHandler/Sign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface PromptRequestData {
Expiry: number;
RollCount: number;
RecipientAddress: string;
RecipientNickname: string;
Amount: string;
PlainText: string;
DisplayData: boolean;
Expand Down
10 changes: 8 additions & 2 deletions wails-frontend/src/pages/PasswordPromptHandler/SignSC/CallSc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import {

import { PromptRequestData } from '../Sign';
import Intl from '@/i18n/i18n';
import { formatStandard, masToken, maskAddress, Unit } from '@/utils';
import {
formatStandard,
masToken,
maskAddress,
Unit,
maskNickname,
} from '@/utils';

export function CallSc(props: PromptRequestData) {
const {
Expand Down Expand Up @@ -40,7 +46,7 @@ export function CallSc(props: PromptRequestData) {
<p className="mas-menu-active">
{Intl.t('password-prompt.sign.from')}
</p>
<p className="mas-menu-default">{Nickname}</p>
<p className="mas-menu-default">{maskNickname(Nickname)}</p>
</div>
<p className="mas-caption">{maskAddress(WalletAddress)}</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import { FiArrowRight } from 'react-icons/fi';

import { PromptRequestData } from '../Sign';
import Intl from '@/i18n/i18n';
import { formatStandard, masToken, maskAddress, Unit } from '@/utils';
import {
formatStandard,
masToken,
maskAddress,
Unit,
maskNickname,
} from '@/utils';

export function Transaction(props: PromptRequestData) {
const {
WalletAddress,
RecipientAddress,
RecipientNickname,
OperationType,
Amount,
Fees,
Expand All @@ -26,15 +33,24 @@ export function Transaction(props: PromptRequestData) {
<p className="mas-menu-active">
{Intl.t('password-prompt.sign.from')}
</p>
<p className="mas-menu-default">{Nickname}</p>
<p className="mas-menu-default">{maskNickname(Nickname)}</p>
</div>
<p className="mas-caption">{maskAddress(WalletAddress)}</p>
</div>
<div className="h-8 w-8 rounded-full flex items-center justify-center bg-neutral">
<FiArrowRight size={24} className="text-primary" />
</div>
<div className="flex flex-col">
<p className="mas-menu-active">{Intl.t('password-prompt.sign.to')}</p>
<div className="flex gap-2">
<p className="mas-menu-active">
{Intl.t('password-prompt.sign.to')}
</p>
{RecipientAddress ? (
<p className="mas-menu-default">
{maskNickname(RecipientNickname)}
</p>
) : null}
</div>
<p className="mas-caption">{maskAddress(RecipientAddress)}</p>
</div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions wails-frontend/src/utils/massaFormat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,11 @@ export function maskAddress(str: string, length = 4, mask = '. . .'): string {

return str ? str?.substring(0, start) + mask + str?.substring(end) : '';
}

export function maskNickname(str: string, length = 10): string {
if (!str) return '';

if (str.length <= length) return str;

return str?.substring(0, length) + '...';
}

0 comments on commit 9762627

Please sign in to comment.