Skip to content
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
4 changes: 4 additions & 0 deletions packages/app-cardano/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export class CardanoApp {
);
}

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

public async destroy() {
return this.sdk.destroy();
}
Expand Down
1 change: 1 addition & 0 deletions packages/app-cardano/src/operations/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './getPublicKeys';
export * from './getUserVerifiedPublicKey';
export * from './runGetPublicKeys';
export * from './signTxn';
89 changes: 89 additions & 0 deletions packages/app-cardano/src/operations/signTxn/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { ISDK } from '@cypherock/sdk-core';
import {
createStatusListener,
assert,
hexToUint8Array,
uint8ArrayToHex,
createLoggerWithPrefix,
} from '@cypherock/sdk-utils';
import { APP_VERSION } from '../../constants/appId';
import {
SeedGenerationStatus,
SignTxnStatus,
} from '../../proto/generated/types';
import {
assertOrThrowInvalidResult,
OperationHelper,
logger as rootLogger,
} from '../../utils';
import { ISignTxnParams, ISignTxnResult, SignTxnEvent } from './types';

export * from './types';

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

export const signTxn = async (
sdk: ISDK,
params: ISignTxnParams,
): Promise<ISignTxnResult> => {
assert(params, 'Params should be defined');
assert(params.walletId, 'walletId should be defined');
assert(params.txn, 'txn should be defined');
assert(typeof params.txn === 'object', 'txn should be an object');
assert(
typeof params.txn.txnDataHex === 'string',
'txn.raw_data_hex should be a string',
);
assert(params.derivationPath, 'derivationPath should be defined');
assert(
params.derivationPath.length > 2,
'derivationPath should be greater than 2',
);

await sdk.checkAppCompatibility(APP_VERSION);

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

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

const txnBytes = hexToUint8Array(params.txn.txnDataHex);

await helper.sendQuery({
initiate: {
walletId: params.walletId,
derivationPath: params.derivationPath,
transactionSize: txnBytes.length,
},
});

const { confirmation } = await helper.waitForResult();
assertOrThrowInvalidResult(confirmation);
forceStatusUpdate(SignTxnEvent.CONFIRM);

await helper.sendInChunks(txnBytes, 'txnData', 'dataAccepted');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will have to evaluate this. Either ask the software using sdk to send only the required part of the txn which just the txn body OR decode here only in sdk and only send the txn body to firmware


await helper.sendQuery({
signature: {},
});
const result = await helper.waitForResult();
assertOrThrowInvalidResult(result.signature);

forceStatusUpdate(SignTxnEvent.PIN_CARD);

const signature = uint8ArrayToHex(result.signature.signature);

return {
signature,
};
};
25 changes: 25 additions & 0 deletions packages/app-cardano/src/operations/signTxn/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export enum SignTxnEvent {
INIT = 0,
CONFIRM = 1,
VERIFY = 2,
PASSPHRASE = 3,
PIN_CARD = 4,
}

export type SignTxnEventHandler = (event: SignTxnEvent) => void;

export interface IUnsignedTransaction {
txnDataHex: string;
}

export interface ISignTxnParams {
onEvent?: SignTxnEventHandler;

walletId: Uint8Array;
derivationPath: number[];
txn: IUnsignedTransaction;
}

export interface ISignTxnResult {
signature: string;
}
1 change: 1 addition & 0 deletions packages/app-cardano/src/operations/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './runGetPublicKeys/types';
export * from './getUserVerifiedPublicKey/types';
export * from './signTxn/types';
2 changes: 1 addition & 1 deletion submodules/common
Loading