Skip to content

Commit

Permalink
updated signBTCtx request
Browse files Browse the repository at this point in the history
- supports new keys to show more details of the tx in the keyguard
  • Loading branch information
onmax committed Feb 23, 2022
1 parent 0dfee63 commit 2dc9c6b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 8 deletions.
6 changes: 5 additions & 1 deletion demos/Demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
SetupSwapRequest,
} from '../src/lib/PublicRequestTypes';
import { RedirectRequestBehavior, PopupRequestBehavior } from '../client/RequestBehavior';
import { Utf8Tools } from '@nimiq/utils';
import { FiatApiSupportedFiatCurrency, Utf8Tools } from '@nimiq/utils';
import { WalletType } from '../src/lib/Constants';
import { WalletStore } from '../src/lib/WalletStore';

Expand Down Expand Up @@ -494,6 +494,10 @@ class Demo {
value: Math.round(0.009 * 1e8),
label: 'Paul McCartney',
},
delay: 12,
fiatCurrency: FiatApiSupportedFiatCurrency.EUR,
fiatRate: 38662.93,
feePerByte: 68,
// changeOutput: {
// keyPath: 'm/49\'/1\'/0\'/1/0',
// address: '',
Expand Down
7 changes: 7 additions & 0 deletions src/lib/PublicRequestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { BitcoinSpecifics } from './paymentOptions/BitcoinPaymentOptions';
import type { BitcoinDirectPaymentOptions } from './paymentOptions/BitcoinPaymentOptions';
import type { EtherSpecifics } from './paymentOptions/EtherPaymentOptions';
import type { EtherDirectPaymentOptions } from './paymentOptions/EtherPaymentOptions';
import { FiatApiSupportedFiatCurrency } from '@nimiq/utils';

export enum RequestType {
LIST = 'list',
Expand Down Expand Up @@ -522,6 +523,12 @@ export interface SignBtcTransactionRequest extends SimpleRequest {
value: number,
};
locktime?: number;

// Data needed for display in a standard BTC transaction
fiatCurrency?: FiatApiSupportedFiatCurrency;
fiatRate?: number;
delay?: number;
feePerByte?: number;
}

export interface SignedBtcTransaction {
Expand Down
32 changes: 32 additions & 0 deletions src/lib/RequestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,29 @@ export class RequestParser {
throw new Error('Invalid locktime');
}

const delay = signBtcTransactionRequest.delay;
if (!delay || delay < 1 || delay >= 100) {
throw new Error('Invalid delay');
}

const fiatRate = signBtcTransactionRequest.fiatRate;
if (!fiatRate || !Nimiq.NumberUtils.isUint32(fiatRate)) {
throw new Error('Invalid fiatRate');
}

const fiatCurrency = signBtcTransactionRequest.fiatCurrency;
if (!fiatCurrency) {
throw new Error('Invalid fiatCurrency');
} else if (typeof fiatCurrency !== 'string' || !/^[a-z]{3}$/i.test(fiatCurrency)) {
throw new Error(`Invalid fiatCurrency code ${fiatCurrency}`);
}

const feePerByte = signBtcTransactionRequest.feePerByte;
if (!feePerByte || !Nimiq.NumberUtils.isUint32(feePerByte)) {
throw new Error('Invalid feePerByte');
}


const parsedSignBtcTransactionRequest: ParsedSignBtcTransactionRequest = {
kind: RequestType.SIGN_BTC_TRANSACTION,
walletId: signBtcTransactionRequest.accountId,
Expand All @@ -508,6 +531,11 @@ export class RequestParser {
output,
changeOutput,
locktime,

delay,
fiatCurrency: fiatCurrency.toUpperCase(),
fiatRate,
feePerByte
};
return parsedSignBtcTransactionRequest;
case RequestType.SETUP_SWAP:
Expand Down Expand Up @@ -768,6 +796,10 @@ export class RequestParser {
output: signBtcTransactionRequest.output,
changeOutput: signBtcTransactionRequest.changeOutput,
locktime: signBtcTransactionRequest.locktime,
fiatCurrency: signBtcTransactionRequest.fiatCurrency,
fiatRate: signBtcTransactionRequest.fiatRate,
delay: signBtcTransactionRequest.delay,
feePerByte: signBtcTransactionRequest.feePerByte,
} as SignBtcTransactionRequest;
case RequestType.SETUP_SWAP:
const setupSwapRequest = request as ParsedSetupSwapRequest;
Expand Down
6 changes: 6 additions & 0 deletions src/lib/RequestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ export interface ParsedSignBtcTransactionRequest extends ParsedSimpleRequest {
value: number,
};
locktime?: number;

// Data needed for display
fiatCurrency: string;
fiatRate: number;
delay: number;
feePerByte: number;
}

export interface ParsedAddBtcAddressesRequest extends ParsedSimpleRequest {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/bitcoin/BitcoinLedgerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { loadBitcoinJS } from './BitcoinJSLoader';

// Import only types to avoid bundling
import type { Transaction as BitcoinJsTransaction } from 'bitcoinjs-lib';
import type { BitcoinTransactionInfo } from '../../views/SignBtcTransaction.vue';
import type { BitcoinTransactionInfo } from '@nimiq/keyguard-client';
import type { TransactionInfoBitcoin as LedgerBitcoinTransactionInfo } from '@nimiq/ledger-api';
import { BitcoinTransactionInputType } from '@nimiq/keyguard-client';

Expand Down
15 changes: 11 additions & 4 deletions src/views/SignBtcTransaction.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ import type {
SignBtcTransactionRequest as KeyguardSignBtcTransactionRequest,
BitcoinTransactionInput,
BitcoinTransactionChangeOutput,
BitcoinTransactionInfo as KeyguardBitcoinTransactionInfo,
BitcoinTransactionInfo,
SignBtcTransactionRequestStandard
} from '@nimiq/keyguard-client';
// BitcoinTransactionInfo with complete changeOutput
export type BitcoinTransactionInfo = Omit<KeyguardBitcoinTransactionInfo, 'changeOutput'> & {
// StandardBitcoinTransactionInfo with complete changeOutput
export type StandardBitcoinTransactionInfo = Pick<BitcoinTransactionInfo, 'inputs' | 'recipientOutput' | 'locktime'> &
// Data needed for display
Pick<SignBtcTransactionRequestStandard, 'fiatCurrency' | 'fiatRate' | 'delay' | 'feePerByte'> & {
changeOutput?: Required<BitcoinTransactionChangeOutput>,
};
Expand Down Expand Up @@ -108,10 +111,14 @@ export default class SignBtcTransaction extends BitcoinSyncBaseView {
changeOutput,
recipientOutput: this.request.output,
locktime: this.request.locktime,
delay: this.request.delay,
fiatCurrency: this.request.fiatCurrency,
fiatRate: this.request.fiatRate,
feePerByte: this.request.feePerByte,
}, walletInfo);
}
protected _signBtcTransaction(transactionInfo: BitcoinTransactionInfo, walletInfo: WalletInfo) {
protected _signBtcTransaction(transactionInfo: StandardBitcoinTransactionInfo, walletInfo: WalletInfo) {
// note that this method gets overwritten for SignBtcTransactionLedger
const request: KeyguardSignBtcTransactionRequest = {
Expand Down
4 changes: 2 additions & 2 deletions src/views/SignBtcTransactionLedger.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<script lang="ts">
import { Component } from 'vue-property-decorator';
import { SmallPage, PageHeader, Amount } from '@nimiq/vue-components';
import SignBtcTransaction, { BitcoinTransactionInfo } from './SignBtcTransaction.vue';
import SignBtcTransaction, { StandardBitcoinTransactionInfo } from './SignBtcTransaction.vue';
import StatusScreen from '../components/StatusScreen.vue';
import GlobalClose from '../components/GlobalClose.vue';
import LedgerUi from '../components/LedgerUi.vue';
Expand Down Expand Up @@ -119,7 +119,7 @@ export default class SignBtcTransactionLedger extends SignBtcTransaction {
}
}
protected async _signBtcTransaction(transactionInfo: BitcoinTransactionInfo, walletInfo: WalletInfo) {
protected async _signBtcTransaction(transactionInfo: StandardBitcoinTransactionInfo, walletInfo: WalletInfo) {
// If user left this view in the mean time, don't continue signing the transaction
if (this._isDestroyed) return;
Expand Down

0 comments on commit 2dc9c6b

Please sign in to comment.