Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Nov 13, 2024
1 parent 6b1f6cf commit 094ec38
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 264 deletions.
17 changes: 9 additions & 8 deletions client/PublicRequestTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type * as Nimiq from '@nimiq/albatross-wasm';
import type { RelayRequest } from '@opengsn/common/dist/EIP712/RelayRequest';

import {
Expand Down Expand Up @@ -92,7 +93,7 @@ export interface ChooseAddressResult extends Address {
export interface SignTransactionRequest extends BasicRequest {
sender: string;
recipient: string;
recipientType?: Nimiq.Account.Type | 3;
recipientType?: Nimiq.AccountType;
recipientLabel?: string;
value: number;
fee?: number;
Expand All @@ -113,7 +114,7 @@ export interface NimiqCheckoutRequest extends BasicRequest {
sender?: string;
forceSender?: boolean;
recipient: string;
recipientType?: Nimiq.Account.Type;
recipientType?: Nimiq.AccountType;
value: number;
fee?: number;
extraData?: Bytes;
Expand Down Expand Up @@ -236,9 +237,9 @@ export interface SignedTransaction {
signature: Uint8Array;

sender: string; // Userfriendly address
senderType: Nimiq.Account.Type | 3;
senderType: Nimiq.AccountType;
recipient: string; // Userfriendly address
recipientType: Nimiq.Account.Type | 3;
recipientType: Nimiq.AccountType;
value: number; // Luna
fee: number; // Luna
validityStartHeight: number;
Expand Down Expand Up @@ -460,19 +461,19 @@ export interface Address {
}

export interface VestingContract {
type: Nimiq.Account.Type.VESTING;
type: Nimiq.AccountType.Vesting;
address: string; // Userfriendly address
label: string;

owner: string; // Userfriendly address
start: number;
startTime: number;
stepAmount: number;
stepBlocks: number;
timeStep: number;
totalAmount: number;
}

export interface HashedTimeLockedContract {
type: Nimiq.Account.Type.HTLC;
type: Nimiq.AccountType.HTLC;
address: string; // Userfriendly address
label: string;

Expand Down
258 changes: 91 additions & 167 deletions src/components/Network.vue

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/config/config.local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { POLYGON_NETWORK_TEST } from '../lib/polygon/PolygonConstants';

export default {
keyguardEndpoint: window.location.protocol + '//' + window.location.hostname + ':8000/src',
ledgerApiNimiqVersion: 'legacy' as LedgerApiNimiqVersion.LEGACY,
ledgerApiNimiqVersion: 'albatross' as LedgerApiNimiqVersion.ALBATROSS,
network: NETWORK_TEST,
networkEndpoint: 'https://network.nimiq-testnet.com',
seedNodes: [
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { POLYGON_NETWORK_MAIN } from '../lib/polygon/PolygonConstants';

export default {
keyguardEndpoint: 'https://keyguard.nimiq.com',
ledgerApiNimiqVersion: 'legacy' as LedgerApiNimiqVersion.LEGACY,
ledgerApiNimiqVersion: 'albatross' as LedgerApiNimiqVersion.ALBATROSS,
network: NETWORK_MAIN,
networkEndpoint: 'https://network.nimiq.com',
seedNodes: [],
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.testnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { POLYGON_NETWORK_TEST } from '../lib/polygon/PolygonConstants';

export default {
keyguardEndpoint: process.env.VUE_APP_KEYGUARD_URL,
ledgerApiNimiqVersion: 'legacy' as LedgerApiNimiqVersion.LEGACY,
ledgerApiNimiqVersion: 'albatross' as LedgerApiNimiqVersion.ALBATROSS,
network: NETWORK_TEST,
networkEndpoint: 'https://network.nimiq-testnet.com',
seedNodes: [
Expand Down
9 changes: 5 additions & 4 deletions src/i18n/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ msgstr ""
msgid "Connecting to Nimiq..."
msgstr ""

#: src/components/CheckoutCardNimiq.vue:285
#: src/components/CheckoutCardNimiq.vue:284
#: src/components/CheckoutCardNimiq.vue:289
#: src/views/CashlinkManage.vue:186
#: src/views/CheckoutTransmission.vue:120
msgid "Contacting seed nodes..."
Expand Down Expand Up @@ -903,7 +904,7 @@ msgstr ""
msgid "Request failed: "
msgstr ""

#: src/components/CheckoutCardNimiq.vue:289
#: src/components/CheckoutCardNimiq.vue:293
msgid "Requesting balances..."
msgstr ""

Expand Down Expand Up @@ -1016,7 +1017,7 @@ msgstr ""
msgid "Syncing"
msgstr ""

#: src/components/CheckoutCardNimiq.vue:287
#: src/components/CheckoutCardNimiq.vue:291
#: src/views/AddVestingContract.vue:11
#: src/views/CashlinkManage.vue:188
#: src/views/CheckoutTransmission.vue:121
Expand Down Expand Up @@ -1169,7 +1170,7 @@ msgstr ""
msgid "Unsupported Request"
msgstr ""

#: src/components/CheckoutCardNimiq.vue:319
#: src/components/CheckoutCardNimiq.vue:329
msgid "Updating balances"
msgstr ""

Expand Down
71 changes: 39 additions & 32 deletions src/lib/ContractInfo.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import type * as Nimiq from '@nimiq/albatross-wasm';
import { VestingContract, HashedTimeLockedContract, Contract } from '../../client/PublicRequestTypes';
import AddressUtils from './AddressUtils';
import { labelAddress } from './LabelingMachine';
import { bytesToHex } from './BufferUtils';

export class ContractInfoHelper {
public static fromObject(o: ContractInfoEntry): VestingContractInfo | HashedTimeLockedContractInfo {
public static async fromObject(o: ContractInfoEntry): Promise<VestingContractInfo | HashedTimeLockedContractInfo> {
const Nimiq = await window.loadAlbatross();
switch (o.type) {
case Nimiq.Account.Type.VESTING:
case Nimiq.AccountType.Vesting:
return VestingContractInfo.fromObject(o);
case Nimiq.Account.Type.HTLC:
case Nimiq.AccountType.HTLC:
return HashedTimeLockedContractInfo.fromObject(o);
// @ts-ignore Property 'type' does not exist on type 'never'.
default: throw new Error('Unknown contract type: ' + o.type);
}
}

// Used in iframe
public static objectToContractType(o: ContractInfoEntry): VestingContract | HashedTimeLockedContract {
public static async objectToContractType(o: ContractInfoEntry): Promise<VestingContract | HashedTimeLockedContract> {
const Nimiq = await window.loadAlbatross();
switch (o.type) {
case 1 /* Nimiq.Account.Type.VESTING */:
case Nimiq.AccountType.Vesting:
return VestingContractInfo.objectToContractType(o);
case 2 /* Nimiq.Account.Type.HTLC */:
case Nimiq.AccountType.HTLC:
return HashedTimeLockedContractInfo.objectToContractType(o);
// @ts-ignore Property 'type' does not exist on type 'never'.
default: throw new Error('Unknown contract type: ' + o.type);
Expand All @@ -28,14 +32,15 @@ export class ContractInfoHelper {
}

export class VestingContractInfo {
public static fromObject(o: VestingContractInfoEntry): VestingContractInfo {
public static async fromObject(o: VestingContractInfoEntry): Promise<VestingContractInfo> {
const Nimiq = await window.loadAlbatross();
return new VestingContractInfo(
o.label,
new Nimiq.Address(o.address),
new Nimiq.Address(o.owner),
o.start,
o.startTime,
o.stepAmount,
o.stepBlocks,
o.timeStep,
o.totalAmount,
o.balance,
);
Expand All @@ -48,23 +53,23 @@ export class VestingContractInfo {
label: o.label,
address: AddressUtils.toUserFriendlyAddress(o.address),
owner: AddressUtils.toUserFriendlyAddress(o.owner),
start: o.start,
startTime: o.startTime,
stepAmount: o.stepAmount,
stepBlocks: o.stepBlocks,
timeStep: o.timeStep,
totalAmount: o.totalAmount,
};
}

public type = Nimiq.Account.Type.VESTING;
public type = 1 /* VESTING */;
public walletId?: string;

public constructor(
public label: string,
public address: Nimiq.Address,
public owner: Nimiq.Address,
public start: number,
public startTime: number,
public stepAmount: number,
public stepBlocks: number,
public timeStep: number,
public totalAmount: number,
public balance?: number,
) {}
Expand All @@ -83,9 +88,9 @@ export class VestingContractInfo {
label: this.label,
address: new Uint8Array(this.address.serialize()),
owner: new Uint8Array(this.owner.serialize()),
start: this.start,
startTime: this.startTime,
stepAmount: this.stepAmount,
stepBlocks: this.stepBlocks,
timeStep: this.timeStep,
totalAmount: this.totalAmount,
balance: this.balance,
};
Expand All @@ -97,9 +102,9 @@ export class VestingContractInfo {
label: this.label,
address: this.userFriendlyAddress,
owner: this.owner.toUserFriendlyAddress(),
start: this.start,
startTime: this.startTime,
stepAmount: this.stepAmount,
stepBlocks: this.stepBlocks,
timeStep: this.timeStep,
totalAmount: this.totalAmount,
};
}
Expand Down Expand Up @@ -136,22 +141,23 @@ export class VestingContractInfo {
* and currentBalance. The withdrawn amount is simply subtracted from the released amount:
* <previous result> - (this.totalAmount - currentBalance)
*/
public calculateAvailableAmount(height: number, currentBalance = this.totalAmount) {
public calculateAvailableAmount(currentBalance = this.totalAmount) {
return Math.min(
this.totalAmount,
Math.max(0, Math.floor((height - this.start) / this.stepBlocks)) * this.stepAmount,
Math.max(0, Math.floor((Date.now() - this.startTime) / this.timeStep)) * this.stepAmount,
) - (this.totalAmount - currentBalance);
}
}

export class HashedTimeLockedContractInfo {
public static fromObject(o: HashedTimeLockedContractInfoEntry): HashedTimeLockedContractInfo {
public static async fromObject(o: HashedTimeLockedContractInfoEntry): Promise<HashedTimeLockedContractInfo> {
const Nimiq = await window.loadAlbatross();
return new HashedTimeLockedContractInfo(
o.label,
new Nimiq.Address(o.address),
new Nimiq.Address(o.sender),
new Nimiq.Address(o.recipient),
new Nimiq.Hash(o.hashRoot),
o.hashRoot,
o.hashCount,
o.timeout,
o.totalAmount,
Expand All @@ -177,15 +183,15 @@ export class HashedTimeLockedContractInfo {
};
}

public type = Nimiq.Account.Type.HTLC;
public type = 2 /* HTLC */;
public walletId?: string;

public constructor(
public label: string,
public address: Nimiq.Address,
public sender: Nimiq.Address,
public recipient: Nimiq.Address,
public hashRoot: Nimiq.Hash,
public hashRoot: Uint8Array,
public hashCount: number,
public timeout: number,
public totalAmount: number,
Expand All @@ -207,22 +213,23 @@ export class HashedTimeLockedContractInfo {
address: new Uint8Array(this.address.serialize()),
sender: new Uint8Array(this.sender.serialize()),
recipient: new Uint8Array(this.recipient.serialize()),
hashRoot: new Uint8Array(this.hashRoot.serialize()),
hashRoot: this.hashRoot,
hashCount: this.hashCount,
timeout: this.timeout,
totalAmount: this.totalAmount,
balance: this.balance,
};
}

public toContractType(): HashedTimeLockedContract {
public async toContractType(): Promise<HashedTimeLockedContract> {
const Nimiq = await window.loadAlbatross();
return {
type: Nimiq.Account.Type.HTLC,
type: Nimiq.AccountType.HTLC,
label: this.label,
address: this.userFriendlyAddress,
sender: this.sender.toUserFriendlyAddress(),
recipient: this.recipient.toUserFriendlyAddress(),
hashRoot: this.hashRoot.toHex(),
hashRoot: bytesToHex(this.hashRoot),
hashCount: this.hashCount,
timeout: this.timeout,
totalAmount: this.totalAmount,
Expand All @@ -236,19 +243,19 @@ export type ContractInfo = VestingContractInfo | HashedTimeLockedContractInfo;
* Database Types
*/
export interface VestingContractInfoEntry {
type: Nimiq.Account.Type.VESTING;
type: Nimiq.AccountType.Vesting;
label: string;
address: Uint8Array;
owner: Uint8Array;
start: number;
startTime: number;
stepAmount: number;
stepBlocks: number;
timeStep: number;
totalAmount: number;
balance?: number;
}

export interface HashedTimeLockedContractInfoEntry {
type: Nimiq.Account.Type.HTLC;
type: Nimiq.AccountType.HTLC;
label: string;
address: Uint8Array;
sender: Uint8Array;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/CookieJar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ContractInfoEntry, VestingContractInfoEntry } from './ContractInfo';
import { AccountInfoEntry } from './AccountInfo';
import { Utf8Tools } from '@nimiq/utils';
import { decodeBase58 } from './bitcoin/Base58';
import { bytesToHex } from './BufferUtils';

class CookieJar {
public static readonly VERSION = 3;
Expand Down Expand Up @@ -303,7 +304,7 @@ class CookieJar {
if (!xpub) return;

const xpubBytes = decodeBase58(xpub);
const xpubType = CookieJar.XPUB_TYPES.indexOf(Nimiq.BufferUtils.toHex(new Uint8Array(xpubBytes.slice(0, 4))));
const xpubType = CookieJar.XPUB_TYPES.indexOf(bytesToHex(new Uint8Array(xpubBytes.slice(0, 4))));
const xpubBody = xpubBytes.slice(4);

bytes.push(xpubType);
Expand Down
30 changes: 0 additions & 30 deletions src/lib/Helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import Config from 'config';
import {
NETWORK_TEST,
NETWORK_DEV,
NETWORK_MAIN,
ERROR_INVALID_NETWORK,
MOBILE_MAX_WIDTH,
} from './Constants';

Expand All @@ -20,31 +15,6 @@ export function getHistoryStorage(key: string): any | undefined {
return history.state ? history.state[key] : undefined;
}

export const loadNimiq = async () => {
await Nimiq.WasmHelper.doImport();
let genesisConfigInitialized = true;
try {
Nimiq.GenesisConfig.NETWORK_ID; // tslint:disable-line:no-unused-expression
} catch (e) {
genesisConfigInitialized = false;
}
if (!genesisConfigInitialized) {
switch (Config.network) {
case NETWORK_TEST:
Nimiq.GenesisConfig.test();
break;
case NETWORK_MAIN:
Nimiq.GenesisConfig.main();
break;
case NETWORK_DEV:
Nimiq.GenesisConfig.dev();
break;
default:
throw new Error(ERROR_INVALID_NETWORK);
}
}
};

export function includesOrigin(list: string[], origin: string) {
return list.includes(origin) || list.includes('*');
}
Expand Down
Loading

0 comments on commit 094ec38

Please sign in to comment.