Skip to content
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

Multi-Currency-Checkout: Types #346

Merged
merged 22 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
43a6eaa
request type update for MultiCurrencyCheckout
nibhar Sep 4, 2019
aad336b
Remove package currency-codes
danimoh Nov 14, 2019
940582e
PaymentOptions: add baseUnitAmount; remove dummy paymentLink stubs
danimoh Sep 18, 2019
34164bc
add csrf to MultiCurrencyCheckoutRequest
nibhar Sep 15, 2019
64cacbd
Time parsing, SimpleResult.success: boolean
nibhar Sep 15, 2019
e4e0456
Remove redundant undefined type.
sisou Sep 16, 2019
43c5b23
remove unnecessary isMilliseconds check
nibhar Sep 16, 2019
4f2006f
add csrf to RequestParsers.raw
nibhar Sep 16, 2019
ebd647b
Checkout: call Keyguard with new optional checkout options
danimoh Sep 25, 2019
470e07f
more robust parsing for MultiCurrencyCheckout
nibhar Sep 26, 2019
268c109
RequestParser,*Paymentoption: improve request parsing
danimoh Oct 3, 2019
09f20d8
RequestParser,NimiqPaymentOptions,BitcoinPaymentOptions: add feePerByte
danimoh Oct 4, 2019
04092c3
add functionality for exporting NimiqCheckoutRequests
nibhar Oct 16, 2019
55aafc6
*RequestTypes,*PaymentOptions: standalone protocolSpecific types
danimoh Nov 16, 2019
48db8e9
package.json: upgrade typescript to support built-in Omit
danimoh Nov 22, 2019
ca33b82
*RequestTypes,*PaymentOptions: unify update functionality
danimoh Nov 17, 2019
d840fa4
RequestTypes,*PaymentOptions: sanitize currency and type
danimoh Nov 24, 2019
eb06b9a
RequestTypes,*PaymentOptions: rename parameter option to options
danimoh Nov 17, 2019
417d13b
small code cleanup
nibhar Nov 26, 2019
cf662a5
move isDesktop and isMilliseconds to Helpers.ts
nibhar Nov 27, 2019
33e401f
PublicRequestTypes: rename PaymentMethod to PaymentType
danimoh Nov 30, 2019
1e6f51d
move ParsedPaymentOptions to own file
nibhar Jan 6, 2020
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
13 changes: 12 additions & 1 deletion client/HubApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
BehaviorType,
} from './RequestBehavior';
import { RedirectRpcClient } from '@nimiq/rpc';
import { RequestType } from '../src/lib/RequestTypes';
import {
RequestType,
BasicRequest,
SimpleRequest,
OnboardRequest,
Expand All @@ -23,13 +23,24 @@ import {
SimpleResult,
ExportResult,
SignedMessage,
Currency,
PaymentType,
} from '../src/lib/PublicRequestTypes';

export default class HubApi<DB extends BehaviorType = BehaviorType.POPUP> { // DB: Default Behavior
public static readonly RequestType = RequestType;
public static readonly RedirectRequestBehavior = RedirectRequestBehavior;
public static readonly Currency = Currency;
public static readonly PaymentType = PaymentType;
public static readonly MSG_PREFIX = '\x16Nimiq Signed Message:\n';

/** @deprecated */
public static get PaymentMethod() {
console.warn('PaymentMethod has been renamed to PaymentType. Access via HubApi.PaymentMethod will soon '
+ 'get disabled. Use HubApi.PaymentType instead.');
return PaymentType;
}

private static get DEFAULT_ENDPOINT() {
const originArray = location.origin.split('.');
originArray.shift();
Expand Down
3 changes: 1 addition & 2 deletions client/RequestBehavior.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { RequestType } from '../src/lib/RequestTypes';
import { PostMessageRpcClient, RedirectRpcClient } from '@nimiq/rpc';
import { ResultByRequestType } from '../src/lib/PublicRequestTypes';
import { ResultByRequestType, RequestType } from '../src/lib/PublicRequestTypes';

export abstract class RequestBehavior<B extends BehaviorType> {
public static getAllowedOrigin(endpoint: string) {
Expand Down
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"rollup-plugin-node-resolve": "^5.0.0",
"rollup-plugin-terser": "^4.0.4",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
"typescript": "^3.5.1"
},
"files": [
"dist",
Expand Down
8 changes: 4 additions & 4 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -998,10 +998,10 @@ tsutils@^2.29.0:
dependencies:
tslib "^1.8.1"

typescript@^3.4.5:
version "3.4.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.5.tgz#2d2618d10bb566572b8d7aad5180d84257d70a99"
integrity sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==
typescript@^3.5.1:
version "3.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.2.tgz#27e489b95fa5909445e9fef5ee48d81697ad18fb"
integrity sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==

union-value@^1.0.0:
version "1.0.1"
Expand Down
2 changes: 1 addition & 1 deletion demos/Demo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { State, PostMessageRpcClient } from '@nimiq/rpc';
import HubApi from '../client/HubApi';
import { RequestType } from '../src/lib/RequestTypes';
import {
SimpleRequest,
Account,
Expand All @@ -11,6 +10,7 @@ import {
SignMessageRequest,
ExportRequest,
RpcResult,
RequestType,
} from '../src/lib/PublicRequestTypes';
import { PopupRequestBehavior, RedirectRequestBehavior } from '../client/RequestBehavior';
import { Utf8Tools } from '@nimiq/utils';
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
"dependencies": {
"@nimiq/browser-warning": "^0.4.0",
"@nimiq/iqons": "^1.5.0",
"@nimiq/keyguard-client": "^1.0.0",
"@nimiq/keyguard-client": "^1.1.0",
"@nimiq/ledgerjs": "https://github.com/nimiq/ledger-api.git",
"@nimiq/network-client": "^0.3.1",
"@nimiq/rpc": "^0.3.0",
"@nimiq/style": "^0.7.2",
"@nimiq/utils": "^0.3.2",
"@nimiq/vue-components": "https://github.com/nimiq/vue-components.git#build/accounts",
"@nimiq/utils": "^0.4.2",
"@nimiq/vue-components": "https://github.com/nimiq/vue-components.git#sebastian/build/checkout/hub",
"big-integer": "^1.6.44",
"vue": "^2.6.6",
"vue-class-component": "^6.0.0",
"vue-property-decorator": "^7.0.0",
Expand All @@ -35,6 +36,7 @@
"@nimiq/core": "^1.5.3",
"@nimiq/core-web": "^1.5.3",
"@types/jest": "^24.0.6",
"@types/node": "^12.12.14",
"@vue/babel-preset-app": "^3.4.1",
"@vue/cli-plugin-typescript": "^3.4.1",
"@vue/cli-plugin-unit-jest": "^3.4.1",
Expand All @@ -45,7 +47,7 @@
"fake-indexeddb": "^2.0.5",
"jest": "^24.1.0",
"ts-jest": "^24.0.0",
"typescript": "^3.4.2",
"typescript": "^3.5.1",
"vue-template-compiler": "^2.6.6",
"write-file-webpack-plugin": "^4.5.0"
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/IdenticonSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import { PageHeader, LoadingSpinner, Identicon, AddressDisplay, CloseIcon } from '@nimiq/vue-components';
import { AccountInfo } from '@/lib/AccountInfo';
import { default as LabelInput } from './Input.vue';
import { isDesktop } from '../lib/Constants';
import { isDesktop } from '../lib/Helpers';

@Component({components: { PageHeader, Identicon, LoadingSpinner, AddressDisplay, LabelInput, CloseIcon }})
class IdenticonSelector extends Vue {
Expand Down
5 changes: 0 additions & 5 deletions src/lib/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,3 @@ export const ERROR_COOKIE_SPACE = 'Not enough cookie space';

// Input
export const MOBILE_MAX_WIDTH = 600; // px
export function isDesktop() {
return (window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth) > MOBILE_MAX_WIDTH;
}
15 changes: 15 additions & 0 deletions src/lib/Helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MOBILE_MAX_WIDTH } from './Constants';

export function isDesktop() {
return (window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth) > MOBILE_MAX_WIDTH;
}

export function isMilliseconds(time: number) {
/*
* 1568577148 = timestamp at time of writing
* 100000000000 ~ 11/16/5138
*/
return time > 100000000000;
}
106 changes: 104 additions & 2 deletions src/lib/PublicRequestTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { WalletType } from './WalletInfo';
import { RequestType } from './RequestTypes';
import { NimiqSpecifics, NimiqDirectPaymentOptions } from './paymentOptions/NimiqPaymentOptions';
import { EtherSpecifics, EtherDirectPaymentOptions } from './paymentOptions/EtherPaymentOptions';
import { BitcoinSpecifics, BitcoinDirectPaymentOptions } from './paymentOptions/BitcoinPaymentOptions';

export enum RequestType {
LIST = 'list',
MIGRATE = 'migrate',
CHECKOUT = 'checkout',
SIGN_MESSAGE = 'sign-message',
SIGN_TRANSACTION = 'sign-transaction',
ONBOARD = 'onboard',
SIGNUP = 'signup',
LOGIN = 'login',
EXPORT = 'export',
CHANGE_PASSWORD = 'change-password',
LOGOUT = 'logout',
ADD_ADDRESS = 'add-address',
RENAME = 'rename',
CHOOSE_ADDRESS = 'choose-address',
}

export interface BasicRequest {
appName: string;
Expand Down Expand Up @@ -28,7 +47,8 @@ export interface SignTransactionRequest extends BasicRequest {
validityStartHeight: number; // FIXME To be made optional when hub has its own network
}

export interface CheckoutRequest extends BasicRequest {
export interface NimiqCheckoutRequest extends BasicRequest {
version?: 1;
shopLogoUrl?: string;
sender?: string;
forceSender?: boolean;
Expand All @@ -41,6 +61,88 @@ export interface CheckoutRequest extends BasicRequest {
validityDuration?: number;
}

export enum PaymentType {
DIRECT,
OASIS,
}

export enum Currency {
NIM = 'nim',
BTC = 'btc',
ETH = 'eth',
}

export type ProtocolSpecificsForCurrency<C extends Currency> =
C extends Currency.NIM ? NimiqSpecifics
: C extends Currency.BTC ? BitcoinSpecifics
: C extends Currency.ETH ? EtherSpecifics
: undefined;

export interface PaymentOptions<C extends Currency, T extends PaymentType> {
type: T;
currency: C;
expires?: number;
/**
* Amount in the smallest unit of the currency specified as `currency`.
* i.e Luna for Currency.NIM and Satoshi for Currency.BTC
*/
amount: string;
protocolSpecific: ProtocolSpecificsForCurrency<C>;
}

export type AvailablePaymentOptions = NimiqDirectPaymentOptions
| EtherDirectPaymentOptions
| BitcoinDirectPaymentOptions;

export type PaymentOptionsForCurrencyAndType<C extends Currency, T extends PaymentType> =
T extends PaymentType.DIRECT ?
C extends Currency.NIM ? NimiqDirectPaymentOptions
: C extends Currency.BTC ? BitcoinDirectPaymentOptions
: C extends Currency.ETH ? EtherDirectPaymentOptions
: PaymentOptions<C, T>
: PaymentOptions<C, T>;

export interface MultiCurrencyCheckoutRequest extends BasicRequest {
version: 2;
/**
* Must be located on the same origin as the one the request is sent from.
*/
shopLogoUrl: string;
/**
* TODO description of the api the callback needs to provide.
* Input is {currency, type} alongside the order identifying parameters in the url.
* the called url must return a PaymentOptions<currency, type> object
*/
callbackUrl?: string;
/**
* A CSRF token, that will be transmitted in all requests to the callback url.
*/
csrf?: string;
/**
* The data to be included in the transaction. Ignored for `Currenct.BTC` and `Currency.ETH`.
*/
extraData?: Uint8Array | string;
/**
* Current time in seconds or milliseconds
*/
time: number;
/**
* ISO 4217 Code (three letters) of the fiat currency used on the calling site.
*/
fiatCurrency: string;
/**
* Amount in the currency specified by `fiatCurrency`
*/
fiatAmount: number;
/**
* Array of available payment options.
* Each currency can only be present once, and a Currency.NIM option must exist.
*/
paymentOptions: AvailablePaymentOptions[];
}

export type CheckoutRequest = NimiqCheckoutRequest | MultiCurrencyCheckoutRequest;

export interface SignedTransaction {
serializedTx: string; // HEX
hash: string; // HEX
Expand Down
Loading