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: added inheritance auth wallet operation #119

Merged
merged 13 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
8 changes: 3 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ jobs:

steps:
- name: checkout code repository
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: "recursive"

- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
with:
version: next-8
uses: pnpm/action-setup@v4

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: 'pnpm'
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ jobs:

steps:
- name: Checkout Commit
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: "recursive"

- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
with:
version: next-8
uses: pnpm/action-setup@v4

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: 'pnpm'
Expand Down
1 change: 1 addition & 0 deletions apps/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@cypherock/sdk-hw-serialport": "workspace:^",
"@cypherock/sdk-interfaces": "workspace:^",
"@cypherock/sdk-utils": "workspace:^",
"@cypherock/sdk-app-inheritance": "workspace:^",
"@solana/web3.js": "^1.87.6",
"bitcoinjs-lib": "^6.1.3",
"ethers": "^6.7.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@cypherock/prettier-config": "workspace:*",
"husky": "^8.0.0",
"prettier": "^3.2.4",
"turbo": "latest"
"turbo": "^1.13.4"
},
"engines": {
"node": ">=18.0.0"
Expand Down
8 changes: 8 additions & 0 deletions packages/app-inheritance/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
root: true,
extends: ['@cypherock/eslint-config'],
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.eslint.json'],
},
};
7 changes: 7 additions & 0 deletions packages/app-inheritance/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.turbo
dist

# stryker temp files
.stryker-tmp
reports
src/proto
1 change: 1 addition & 0 deletions packages/app-inheritance/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/proto/generated
1 change: 1 addition & 0 deletions packages/app-inheritance/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@cypherock/prettier-config"
3 changes: 3 additions & 0 deletions packages/app-inheritance/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction

Documentation pending
22 changes: 22 additions & 0 deletions packages/app-inheritance/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
collectCoverage: true,
collectCoverageFrom: [
'src/**/*.ts',
'!src/**/*.test.ts',
'!src/**/__fixtures__/*',
'!src/proto/generated/**/*',
],
testTimeout: 500,
preset: 'ts-jest',
testEnvironment: 'node',
rootDir: '.',
testPathIgnorePatterns: ['/node_modules/', '/__fixtures__/', '/dist/'],
testMatch: [
'**/tests/**/*.[jt]s?(x)',
'**/__tests__/**/*.[jt]s?(x)',
'!**/__mocks__/**/*.[jt]s?(x)',
'!**/__helpers__/**/*.[jt]s?(x)',
'!**/.stryker-tmp/**/*.[jt]s?(x)',
],
modulePathIgnorePatterns: ['<rootDir>/.stryker-tmp'],
};
21 changes: 21 additions & 0 deletions packages/app-inheritance/jest.stryker.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
collectCoverage: true,
collectCoverageFrom: [
'src/**/*.ts',
'!src/**/*.test.ts',
'!src/**/__fixtures__/*',
'!src/proto/generated/**/*',
],
testTimeout: 500,
preset: 'ts-jest',
testEnvironment: 'node',
rootDir: '.',
testPathIgnorePatterns: ['/node_modules/', '/__fixtures__/', '/dist/'],
testMatch: [
'**/tests/**/*.[jt]s?(x)',
'**/__tests__/**/*.[jt]s?(x)',
'!**/__mocks__/**/*.[jt]s?(x)',
'!**/__helpers__/**/*.[jt]s?(x)',
],
modulePathIgnorePatterns: ['<rootDir>/.stryker-tmp'],
};
1 change: 1 addition & 0 deletions packages/app-inheritance/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
site_name: '@cypherock/sdk-app-inheritance'
53 changes: 53 additions & 0 deletions packages/app-inheritance/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "@cypherock/sdk-app-inheritance",
"version": "0.0.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"scripts": {
"lint": "eslint src/ tests/ --fix",
"lint:check": "eslint --ext .ts,tsx,js,jsx,js src/",
"pretty": "prettier --write 'src/**/*.ts' 'tests/**/*.ts'",
"pretty:check": "prettier --check 'src/**/*.ts' 'tests/**/*.ts'",
"prebuild": "bash ./scripts/prebuild.sh",
"build": "rimraf dist && tsc -p tsconfig.json",
"test": "cross-env LOG_LEVEL=error jest",
"test:mutation": "stryker run",
"pre-commit": "lint-staged"
},
"devDependencies": {
"@cypherock/eslint-config": "workspace:*",
"@cypherock/prettier-config": "workspace:^0.0.7",
"@cypherock/tsconfig": "workspace:*",
"@jest/globals": "^29.4.1",
"@stryker-mutator/core": "^6.4.1",
"@stryker-mutator/jest-runner": "^6.4.1",
"@stryker-mutator/typescript-checker": "^6.4.1",
"@types/jest": "^29.4.0",
"@types/node": "18.11.18",
"cross-env": "^7.0.3",
"eslint": "^7.32.0",
"jest": "^29.4.1",
"lint-staged": "^13.2.0",
"rimraf": "^4.1.2",
"ts-jest": "^29.0.5",
"ts-proto": "^1.139.0",
"typescript": "^4.5.2"
},
"dependencies": {
"@cypherock/sdk-core": "workspace:^0.0.25",
"@cypherock/sdk-interfaces": "workspace:^0.0.15",
"@cypherock/sdk-utils": "workspace:^0.0.18",
"long": "^5.2.1",
"protobufjs": "^7.2.2"
},
"lint-staged": {
"*.{ts,tsx}": [
"eslint --ext ts,tsx --quiet --fix --",
"prettier --write"
],
"*.{js,jsx,md,mdx,mjs,yml,yaml,css,json}": [
"prettier --write"
]
}
}
16 changes: 16 additions & 0 deletions packages/app-inheritance/scripts/prebuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

set -e

rm -rf ./src/proto/generated/*.ts || true
rm -rf ./src/proto/generated/inheritance || true

mkdir -p src/proto/generated

if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
protoc --plugin=protoc-gen-ts_proto=$(pwd)/node_modules/.bin/protoc-gen-ts_proto.cmd --ts_proto_out=./src/proto/generated ../../submodules/common/proto/inheritance/*.proto ../../submodules/common/proto/common.proto -I../../submodules/common/proto --ts_proto_opt=forceLong=string --ts_proto_opt=esModuleInterop=true
else
protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=./src/proto/generated ../../submodules/common/proto/inheritance/*.proto ../../submodules/common/proto/common.proto -I../../submodules/common/proto --ts_proto_opt=forceLong=string --ts_proto_opt=esModuleInterop=true
fi

node ../../scripts/extractTypes/index.js ./src/proto/generated ./src/proto/generated/types.ts
60 changes: 60 additions & 0 deletions packages/app-inheritance/src/__mocks__/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { ISDK } from '@cypherock/sdk-core';
import { DeviceState } from '@cypherock/sdk-interfaces';
import { jest } from '@jest/globals';

let sequenceNumber = 0;

export const getStatus = jest.fn<ISDK['getStatus']>();
export const sendAbort = jest.fn<ISDK['sendAbort']>();
export const getResult = jest.fn<ISDK['getResult']>();
export const sendQuery = jest.fn<ISDK['sendQuery']>();

export const configureAppletId = jest.fn<ISDK['configureAppletId']>();
export const checkAppCompatibility = jest.fn<ISDK['checkAppCompatibility']>();

export const waitForResult = jest.fn<ISDK['waitForResult']>();
export const getSequenceNumber = jest.fn<ISDK['getSequenceNumber']>(
async () => sequenceNumber,
);
export const getNewSequenceNumber = jest.fn<ISDK['getNewSequenceNumber']>(
async () => {
sequenceNumber += 1;
return sequenceNumber;
},
);

export const runOperation = jest.fn<ISDK['runOperation']>(func => func());

export const destroy = jest.fn<ISDK['destroy']>();
export const getDeviceState = jest.fn<ISDK['getDeviceState']>(
async () => DeviceState.MAIN,
);

export const create = jest.fn(async () =>
Promise.resolve({
configureAppletId,
checkAppCompatibility,
sendAbort,
getResult,
getStatus,
sendQuery,
waitForResult,
getSequenceNumber,
getNewSequenceNumber,
runOperation,
destroy,
getDeviceState,
}),
);

jest.mock('@cypherock/sdk-core', () => {
const originalModule: any = jest.requireActual('@cypherock/sdk-core');

Check warning on line 51 in packages/app-inheritance/src/__mocks__/sdk.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Unexpected any. Specify a different type

return {
__esModule: true,
...originalModule,
SDK: {
create,
},
};
});
30 changes: 30 additions & 0 deletions packages/app-inheritance/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { IDeviceConnection } from '@cypherock/sdk-interfaces';
import { SDK } from '@cypherock/sdk-core';
import * as operations from './operations';

export class InheritanceApp {
private readonly sdk: SDK;

private static readonly APPLET_ID = 19;

private constructor(sdk: SDK) {
this.sdk = sdk;
}

public static async create(connection: IDeviceConnection) {
const sdk = await SDK.create(connection, InheritanceApp.APPLET_ID);
return new InheritanceApp(sdk);
}

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

public async destroy() {
return this.sdk.destroy();
}

public async abort() {
await this.sdk.sendAbort();
}
}
4 changes: 4 additions & 0 deletions packages/app-inheritance/src/constants/appId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const APP_VERSION = {
from: '1.0.0',
to: '2.0.0',
};
3 changes: 3 additions & 0 deletions packages/app-inheritance/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './app';
export * from './types';
export { updateLogger } from './utils';
71 changes: 71 additions & 0 deletions packages/app-inheritance/src/operations/authWallet/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { ISDK } from '@cypherock/sdk-core';
import {
assert,
createLoggerWithPrefix,
createStatusListener,
} from '@cypherock/sdk-utils';
import { APP_VERSION } from '../../constants/appId';
import { IWalletAuthResultResponse } from '../../proto/generated/types';
import {
assertOrThrowInvalidResult,
OperationHelper,
logger as rootLogger,
} from '../../utils';
import { IAuthWalletParams, WALLET_ID_LENGTH, WalletAuthEvent } from './types';
import { WalletAuthStatus } from '../../proto/generated/inheritance/wallet_auth';

export * from './types';

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

export const authWallet = async (
sdk: ISDK,
params: IAuthWalletParams,
): Promise<IWalletAuthResultResponse> => {
assert(params, 'Params should be defined');
assert(params.walletId, 'walletId should be defined');
assert(params.challenge, 'challenge should be defined');
assert(
typeof params.isPublicKey === 'boolean',
'isPublicKey should be defined',
);
assert(
params.walletId.length === WALLET_ID_LENGTH,
`Wallet Id should be exactly ${WALLET_ID_LENGTH} bytes`,
);

await sdk.checkAppCompatibility(APP_VERSION);

logger.info('Started', { ...params, onEvent: undefined });
const { forceStatusUpdate, onStatus } = createStatusListener({
enums: WalletAuthEvent,
operationEnums: WalletAuthStatus,
onEvent: params.onEvent,
logger,
});

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

await helper.sendQuery({
initiate: {
challenge: params.challenge,
walletId: params.walletId,
isPublickey: params.isPublicKey,
},
});

const result = await helper.waitForResult();
logger.verbose('WalletAuthResponse', result);

assertOrThrowInvalidResult(result.result);

forceStatusUpdate(WalletAuthEvent.CARD_TAP);

logger.info('Completed');
return result.result;
};
14 changes: 14 additions & 0 deletions packages/app-inheritance/src/operations/authWallet/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export enum WalletAuthEvent {
INIT = 0,
CARD_TAP = 1,
}

export type AuthWalletEventHandler = (event: WalletAuthEvent) => void;
export const WALLET_ID_LENGTH = 32;

export interface IAuthWalletParams {
challenge: Uint8Array;
walletId: Uint8Array;
isPublicKey: boolean;
onEvent?: AuthWalletEventHandler;
}
1 change: 1 addition & 0 deletions packages/app-inheritance/src/operations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './authWallet';
1 change: 1 addition & 0 deletions packages/app-inheritance/src/operations/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './authWallet/types';
2 changes: 2 additions & 0 deletions packages/app-inheritance/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './proto/generated/types';
export * from './operations/types';
Loading
Loading