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

feat: adds get-public-key functionality to xrp app #129

Merged
merged 6 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions apps/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"@cypherock/sdk-app-evm": "workspace:^",
"@cypherock/sdk-app-manager": "workspace:^",
"@cypherock/sdk-app-near": "workspace:^",
"@cypherock/sdk-app-xrp": "workspace:^",
"@cypherock/sdk-app-solana": "workspace:^",
"@cypherock/sdk-app-tron": "workspace:^",
"@cypherock/sdk-app-xrp": "workspace:^",
"@cypherock/sdk-core": "workspace:^",
"@cypherock/sdk-hw-hid": "workspace:^",
"@cypherock/sdk-hw-serialport": "workspace:^",
Expand All @@ -38,6 +38,7 @@
"ethers": "^6.7.0",
"near-api-js": "^2.1.4",
"tronweb": "^5.3.2",
"winston": "^3.11.0"
"winston": "^3.11.0",
"xrpl": "^4.0.0"
}
}
3 changes: 2 additions & 1 deletion packages/app-xrp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"@cypherock/sdk-interfaces": "workspace:^0.0.15",
"@cypherock/sdk-utils": "workspace:^0.0.18",
"long": "^5.2.1",
"protobufjs": "^7.2.2"
"protobufjs": "^7.2.2",
"xrpl": "^4.0.0"
},
"lint-staged": {
"*.{ts,tsx}": [
Expand Down
16 changes: 16 additions & 0 deletions packages/app-xrp/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IDeviceConnection } from '@cypherock/sdk-interfaces';
import { SDK } from '@cypherock/sdk-core';

import * as operations from './operations';

export class XrpApp {
private readonly sdk: SDK;

Expand All @@ -15,6 +17,20 @@ export class XrpApp {
return new XrpApp(sdk);
}

public async getPublicKeys(params: operations.IGetPublicKeysParams) {
return this.sdk.runOperation(() =>
operations.getPublicKeys(this.sdk, params),
);
}

public async getUserVerifiedPublicKey(
params: operations.IGetUserVerifiedPublicKeyParams,
) {
return this.sdk.runOperation(() =>
operations.getUserVerifiedPublicKey(this.sdk, params),
);
}

public async destroy() {
return this.sdk.destroy();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/app-xrp/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './app';
export * from './types';
export { updateLogger } from './utils';
export { updateLogger, setXrpLib } from './utils';
59 changes: 59 additions & 0 deletions packages/app-xrp/src/operations/getPublicKeys/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ISDK } from '@cypherock/sdk-core';
import {
createStatusListener,
assert,
createLoggerWithPrefix,
} from '@cypherock/sdk-utils';
import { APP_VERSION } from '../../constants/appId';
import {
GetPublicKeysStatus,
SeedGenerationStatus,
} from '../../proto/generated/types';
import { OperationHelper, logger as rootLogger } from '../../utils';
import { runGetPublicKeysOnDevice } from '../runGetPublicKeys';
import {
IGetPublicKeysParams,
IGetPublicKeysResult,
GetPublicKeysEvent,
} from '../types';

const logger = createLoggerWithPrefix(rootLogger, 'GetPublicKeys');

export const getPublicKeys = async (
sdk: ISDK,
params: IGetPublicKeysParams,
): Promise<IGetPublicKeysResult> => {
assert(params, 'Params should be defined');
assert(params.walletId, 'walletId should be defined');
assert(params.derivationPaths, 'derivationPaths should be defined');
assert(
params.derivationPaths.length > 0,
'derivationPaths should not be empty',
);
assert(
params.derivationPaths.reduce(
(acc, path) => acc && path.path.length > 3,
true,
),
'derivationPaths should be greater than 3',
);

await sdk.checkAppCompatibility(APP_VERSION);

const { onStatus, forceStatusUpdate } = createStatusListener({
enums: GetPublicKeysEvent,
operationEnums: GetPublicKeysStatus,
seedGenerationEnums: SeedGenerationStatus,
onEvent: params.onEvent,
logger,
});

const helper = new OperationHelper({
sdk,
queryKey: 'getPublicKeys',
resultKey: 'getPublicKeys',
onStatus,
});

return runGetPublicKeysOnDevice(helper, params, forceStatusUpdate);
};
67 changes: 67 additions & 0 deletions packages/app-xrp/src/operations/getUserVerifiedPublicKey/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ISDK } from '@cypherock/sdk-core';
import {
createStatusListener,
assert,
createLoggerWithPrefix,
} from '@cypherock/sdk-utils';
import { APP_VERSION } from '../../constants/appId';
import {
GetPublicKeysStatus,
SeedGenerationStatus,
} from '../../proto/generated/types';
import { OperationHelper, logger as rootLogger } from '../../utils';
import { runGetPublicKeysOnDevice } from '../runGetPublicKeys';
import { GetPublicKeysEvent } from '../types';
import {
IGetUserVerifiedPublicKeyParams,
IGetUserVerifiedPublicKeyResult,
} from './types';

export * from './types';

const logger = createLoggerWithPrefix(rootLogger, 'GetPublicKeys');

export const getUserVerifiedPublicKey = async (
sdk: ISDK,
params: IGetUserVerifiedPublicKeyParams,
): Promise<IGetUserVerifiedPublicKeyResult> => {
assert(params, 'Params should be defined');
assert(params.walletId, 'walletId should be defined');
assert(params.derivationPath, 'derivationPath should be defined');
assert(
params.derivationPath.length > 3,
'derivationPath should be greater than 3',
);

await sdk.checkAppCompatibility(APP_VERSION);

const { onStatus, forceStatusUpdate } = createStatusListener({
enums: GetPublicKeysEvent,
operationEnums: GetPublicKeysStatus,
seedGenerationEnums: SeedGenerationStatus,
onEvent: params.onEvent,
logger,
});

const helper = new OperationHelper({
sdk,
queryKey: 'getUserVerifiedPublicKey',
resultKey: 'getUserVerifiedPublicKey',
onStatus,
});

const result = await runGetPublicKeysOnDevice(
helper,
{
walletId: params.walletId,
derivationPaths: [{ path: params.derivationPath }],
onEvent: params.onEvent,
},
forceStatusUpdate,
);

return {
publicKey: result.publicKeys[0],
address: result.addresses[0],
};
};
13 changes: 13 additions & 0 deletions packages/app-xrp/src/operations/getUserVerifiedPublicKey/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GetPublicKeysEventHandler } from '../runGetPublicKeys/types';

export interface IGetUserVerifiedPublicKeyParams {
onEvent?: GetPublicKeysEventHandler;

walletId: Uint8Array;
derivationPath: number[];
}

export interface IGetUserVerifiedPublicKeyResult {
publicKey: string;
address: string;
}
3 changes: 3 additions & 0 deletions packages/app-xrp/src/operations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './getPublicKeys';
export * from './getUserVerifiedPublicKey';
export * from './runGetPublicKeys';
45 changes: 45 additions & 0 deletions packages/app-xrp/src/operations/runGetPublicKeys/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { uint8ArrayToHex } from '@cypherock/sdk-utils';
import {
OperationHelper,
assertOrThrowInvalidResult,
getAddressFromPublicKey,
} from '../../utils';
import { IGetPublicKeysParams, GetPublicKeysEvent } from './types';

export * from './types';

export const runGetPublicKeysOnDevice = async (
helper:
| OperationHelper<'getPublicKeys', 'getPublicKeys'>
| OperationHelper<'getUserVerifiedPublicKey', 'getUserVerifiedPublicKey'>,
params: IGetPublicKeysParams,
forceStatusUpdate: (flowStatus: number) => void,
) => {
await helper.sendQuery({
initiate: {
walletId: params.walletId,
derivationPaths: params.derivationPaths,
},
});

let publicKeys: Uint8Array[] = [];
const hasMore = () => publicKeys.length !== params.derivationPaths.length;
do {
const result = await helper.waitForResult();
assertOrThrowInvalidResult(result.result);
publicKeys = [...publicKeys, ...result.result.publicKeys];
forceStatusUpdate(GetPublicKeysEvent.PIN_CARD);
if (hasMore()) {
await helper.sendQuery({
fetchNext: {},
});
}
} while (hasMore());

forceStatusUpdate(GetPublicKeysEvent.VERIFY);

return {
publicKeys: publicKeys.map(e => uint8ArrayToHex(e)),
addresses: publicKeys.map(e => getAddressFromPublicKey(e)),
};
};
23 changes: 23 additions & 0 deletions packages/app-xrp/src/operations/runGetPublicKeys/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IGetPublicKeysDerivationPath } from '../../proto/generated/types';

export enum GetPublicKeysEvent {
INIT = 0,
CONFIRM = 1,
PASSPHRASE = 2,
PIN_CARD = 3,
VERIFY = 4,
}

export type GetPublicKeysEventHandler = (event: GetPublicKeysEvent) => void;

export interface IGetPublicKeysParams {
onEvent?: GetPublicKeysEventHandler;

walletId: Uint8Array;
derivationPaths: IGetPublicKeysDerivationPath[];
}

export interface IGetPublicKeysResult {
publicKeys: string[];
addresses: string[];
}
2 changes: 2 additions & 0 deletions packages/app-xrp/src/operations/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './runGetPublicKeys/types';
export * from './getUserVerifiedPublicKey/types';
1 change: 1 addition & 0 deletions packages/app-xrp/src/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './operations/types';
export * from './proto/generated/types';
11 changes: 11 additions & 0 deletions packages/app-xrp/src/utils/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { uint8ArrayToHex } from '@cypherock/sdk-utils';
import { getXrpLib } from './xrpLib';

export const getAddressFromPublicKey = (publicKey: string | Uint8Array) => {
const publicKeyHex =
typeof publicKey === 'string' ? publicKey : uint8ArrayToHex(publicKey);

const xrpl = getXrpLib();

return xrpl.deriveAddress(publicKeyHex);
};
2 changes: 2 additions & 0 deletions packages/app-xrp/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './operationHelper';
export * from './asserts';
export * from './logger';
export * from './xrpLib';
export * from './address';
16 changes: 16 additions & 0 deletions packages/app-xrp/src/utils/xrpLib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type xrpl from 'xrpl';

export type XrpLibType = typeof xrpl;

let xrpLibInstance: XrpLibType | undefined;

export const getXrpLib = () => {
if (!xrpLibInstance) {
throw new Error('xrpl has not been set yet');
}
return xrpLibInstance;
};

export const setXrpLib = (xrpLibrary: XrpLibType) => {
xrpLibInstance = xrpLibrary;
};
Loading
Loading