Skip to content

Commit

Permalink
Merge pull request #465 from nimiq/usdc
Browse files Browse the repository at this point in the history
USDC-on-Polygon integration
  • Loading branch information
sisou authored Oct 22, 2023
2 parents 1131ac4 + 19f7b56 commit 8d2339a
Show file tree
Hide file tree
Showing 98 changed files with 9,261 additions and 815 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.min.*
src/lib/bitcoin/BitcoinJS.js
src/lib/polygon/OpenGSN.js
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ stages:

# include private configs for the remaining stages
include:
- project: "it/ci-config"
- project: "mirrors/ci-config"
file: "/testnet/deploy_webapp.yml"

variables:
Expand Down
11 changes: 6 additions & 5 deletions .tx/config
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[main]
host = https://www.transifex.com

[nimiq-keyguard.en-json]
file_filter = src/translations/<lang>.json
[o:nimiq-foundation:p:nimiq-keyguard:r:en-json]
file_filter = src/translations/<lang>.json
source_file = src/translations/en.json
source_lang = en
type = KEYVALUEJSON
minimum_perc = 50
source_file = src/translations/en.json
source_lang = en
type = KEYVALUEJSON

5 changes: 3 additions & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
"homepage": "https://github.com/nimiq/keyguard/client#readme",
"dependencies": {
"@nimiq/core-web": "1.5.8",
"@nimiq/rpc": "^0.3.0"
"@nimiq/rpc": "^0.3.0",
"@opengsn/common": "^2.2.5"
},
"devDependencies": {
"rollup": "^0.64.0",
"tslint": "^5.11.0",
"typescript": "^3.5.1"
"typescript": "^4.9.5"
}
}
10 changes: 10 additions & 0 deletions client/src/KeyguardClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
ResultByCommand,
SignBtcTransactionRequest,
DeriveBtcXPubRequest,
DerivePolygonAddressRequest,
SignPolygonTransactionRequest,
SignSwapRequest,
SignSwapTransactionsRequest,
SignSwapTransactionsResult,
Expand Down Expand Up @@ -134,6 +136,14 @@ export class KeyguardClient {
this._redirectRequest<DeriveBtcXPubRequest>(KeyguardCommand.DERIVE_BTC_XPUB, request);
}

public derivePolygonAddress(request: DerivePolygonAddressRequest) {
this._redirectRequest<DerivePolygonAddressRequest>(KeyguardCommand.DERIVE_POLYGON_ADDRESS, request);
}

public signPolygonTransaction(request: SignPolygonTransactionRequest) {
this._redirectRequest<SignPolygonTransactionRequest>(KeyguardCommand.SIGN_POLYGON_TRANSACTION, request);
}

public signSwap(request: SignSwapRequest) {
this._redirectRequest<SignSwapRequest>(KeyguardCommand.SIGN_SWAP, request);
}
Expand Down
4 changes: 4 additions & 0 deletions client/src/KeyguardCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export enum KeyguardCommand {
SIGN_BTC_TRANSACTION = 'sign-btc-transaction',
DERIVE_BTC_XPUB = 'derive-btc-xpub',

// Polygon
SIGN_POLYGON_TRANSACTION = 'sign-polygon-transaction',
DERIVE_POLYGON_ADDRESS = 'derive-polygon-address',

// Swap
SIGN_SWAP = 'sign-swap',

Expand Down
88 changes: 87 additions & 1 deletion client/src/PublicRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as Nimiq from '@nimiq/core-web';
import { ForwardRequest as OpenGsnForwardRequest } from '@opengsn/common/dist/EIP712/ForwardRequest';
import { RelayData as OpenGsnRelayData } from '@opengsn/common/dist/EIP712/RelayData';
import { KeyguardCommand } from './KeyguardCommand';

export {
OpenGsnForwardRequest,
OpenGsnRelayData,
};

export type ObjectType = {
[key: string]: any;
};
Expand All @@ -25,6 +32,10 @@ export type SingleKeyResult = {
fileExported: boolean;
wordsExported: boolean;
bitcoinXPub?: string;
polygonAddresses?: Array<{
address: string,
keyPath: string,
}>;
tmpCookieEncryptionKey?: Uint8Array;
};

Expand Down Expand Up @@ -90,6 +101,7 @@ export type CreateRequest = BasicRequest & {
defaultKeyPath: string,
enableBackArrow?: boolean,
bitcoinXPubPath: string,
polygonAccountPath: string,
};

export type DeriveAddressRequest = SimpleRequest & {
Expand All @@ -111,6 +123,7 @@ export type ImportRequest = BasicRequest & {
enableBackArrow?: boolean,
wordsOnly?: boolean,
bitcoinXPubPath: string,
polygonAccountPath: string,
};

export type ResetPasswordRequest = ImportRequest & {
Expand Down Expand Up @@ -194,6 +207,33 @@ export type SignBtcTransactionRequest
= SignBtcTransactionRequestStandard
| SignBtcTransactionRequestCheckout;

export type PolygonTransactionInfo = {
keyPath: string,

request: OpenGsnForwardRequest,
relayData: OpenGsnRelayData,

/**
* For refund and redeem transactions from HTLCs the amount is not part of the forward request / relay request and
* needs to be specified separately.
*/
amount?: number,

/**
* The sender's nonce in the token contract, required when calling the
* contract function `transferWithApproval`.
*/
approval?: {
tokenNonce: number,
},
};

export type SignPolygonTransactionRequest = Omit<SimpleRequest, 'keyLabel'> & PolygonTransactionInfo & {
keyLabel: string,
senderLabel?: string,
recipientLabel?: string,
};

export type MockSettlementInstruction = {
type: 'mock',
contractId: string,
Expand Down Expand Up @@ -236,6 +276,9 @@ export type SignSwapRequestCommon = SimpleRequest & {
>,
refundKeyPath: string, // To validate that we own the HTLC script's refund address
}>
) | (
{type: 'USDC'}
& Omit<PolygonTransactionInfo, 'amount'>
) | (
{type: 'EUR'}
& {
Expand Down Expand Up @@ -268,6 +311,12 @@ export type SignSwapRequestCommon = SimpleRequest & {
>,
output: BitcoinTransactionChangeOutput,
}
) | (
{type: 'USDC'}
& Omit<PolygonTransactionInfo, 'approval' | 'amount'>
& {
amount: number,
}
) | (
{type: 'EUR'}
& {
Expand Down Expand Up @@ -295,7 +344,7 @@ export type SignSwapRequestCommon = SimpleRequest & {
funding: number,
processing: number,
},
serviceSwapFee: number, // Luna, Sats or Cents, depending which one gets funded
serviceSwapFee: number, // Luna, Sats or USDC-units, depending which one gets funded

// Optional KYC info for swapping at higher limits.
// KYC-enabled swaps facilitated by S3/Fastspot require an s3GrantToken and swaps from or to Euro via OASIS
Expand All @@ -313,13 +362,18 @@ export type SignSwapRequestStandard = SignSwapRequestCommon & {

export type SignSwapRequestSlider = SignSwapRequestCommon & {
layout: 'slider',
direction: 'left-to-right' | 'right-to-left',
nimiqAddresses: Array<{
address: string,
balance: number, // Luna
}>,
bitcoinAccount: {
balance: number, // Sats
},
polygonAddresses: Array<{
address: string,
usdcBalance: number, // smallest unit of USDC (= 0.000001 USDC)
}>,
};

export type SignSwapRequest = SignSwapRequestStandard | SignSwapRequestSlider;
Expand All @@ -338,6 +392,9 @@ export type SignSwapTransactionsRequest = {
} | {
type: 'BTC',
htlcScript: Uint8Array,
} | {
type: 'USDC',
htlcData: string,
} | {
type: 'EUR',
hash: string,
Expand All @@ -353,6 +410,11 @@ export type SignSwapTransactionsRequest = {
htlcScript: Uint8Array,
transactionHash: string,
outputIndex: number;
} | {
type: 'USDC',
hash: string,
timeout: number,
htlcId: string,
} | {
type: 'EUR',
hash: string,
Expand All @@ -379,6 +441,17 @@ export type DeriveBtcXPubResult = {
bitcoinXPub: string,
};

export type DerivePolygonAddressRequest = SimpleRequest & {
polygonAccountPath: string,
};

export type DerivePolygonAddressResult = {
polygonAddresses: Array<{
address: string,
keyPath: string,
}>,
};

// Request unions

export type RedirectRequest
Expand All @@ -390,8 +463,10 @@ export type RedirectRequest
| SignMessageRequest
| SignTransactionRequest
| SignBtcTransactionRequest
| SignPolygonTransactionRequest
| SimpleRequest
| DeriveBtcXPubRequest
| DerivePolygonAddressRequest
| SignSwapRequest;

export type IFrameRequest
Expand Down Expand Up @@ -432,9 +507,14 @@ export type SignedBitcoinTransaction = {
transactionHash: string,
raw: string,
};
export type SignedPolygonTransaction = {
message: Record<string, any>,
signature: string,
};
export type SignSwapTransactionsResult = {
nim?: SignatureResult,
btc?: SignedBitcoinTransaction,
usdc?: SignedPolygonTransaction,
eur?: string, // When funding EUR: empty string, when redeeming EUR: JWS of the settlement instructions
refundTx?: string,
};
Expand All @@ -454,8 +534,10 @@ export type RedirectResult
| KeyResult
| SignTransactionResult
| SignedBitcoinTransaction
| SignedPolygonTransaction
| SimpleResult
| DeriveBtcXPubResult
| DerivePolygonAddressResult
| SignSwapResult;

export type Result = RedirectResult | IFrameResult;
Expand All @@ -470,6 +552,8 @@ export type ResultType<T extends RedirectRequest> =
T extends Is<T, RemoveKeyRequest> | Is<T, SimpleRequest> ? SimpleResult :
T extends Is<T, SignBtcTransactionRequest> ? SignedBitcoinTransaction :
T extends Is<T, DeriveBtcXPubRequest> ? DeriveBtcXPubResult :
T extends Is<T, DerivePolygonAddressRequest> ? DerivePolygonAddressResult :
T extends Is<T, SignPolygonTransactionRequest> ? SignedPolygonTransaction :
T extends Is<T, SignSwapRequest> ? SignSwapResult :
never;

Expand All @@ -481,6 +565,8 @@ export type ResultByCommand<T extends KeyguardCommand> =
T extends KeyguardCommand.REMOVE ? SimpleResult :
T extends KeyguardCommand.SIGN_BTC_TRANSACTION ? SignedBitcoinTransaction :
T extends KeyguardCommand.DERIVE_BTC_XPUB ? DeriveBtcXPubResult :
T extends KeyguardCommand.DERIVE_POLYGON_ADDRESS ? DerivePolygonAddressResult :
T extends KeyguardCommand.SIGN_POLYGON_TRANSACTION ? SignedPolygonTransaction :
T extends KeyguardCommand.SIGN_SWAP ? SignSwapResult :
never;

Expand Down
5 changes: 4 additions & 1 deletion client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"outDir": "./build/",
"target": "esnext",
"target": "es2021",
"module": "esnext",
"strict": true,
"jsx": "preserve",
Expand All @@ -13,6 +13,9 @@
"baseUrl": ".",
"declaration": true,
"declarationDir": "./dist/src/",
/* Skip lib check because our typescript version is too old to handle the types of ethers. */
/* TODO once typescript has been updated, this can be removed. */
"skipLibCheck": true,
"lib": [
"es2017",
"dom",
Expand Down
Loading

0 comments on commit 8d2339a

Please sign in to comment.