Skip to content

Commit b293518

Browse files
committedMar 8, 2025·
feat: added sign txn functionality to constellation app
1 parent a30560b commit b293518

File tree

6 files changed

+112
-3
lines changed

6 files changed

+112
-3
lines changed
 

‎packages/app-constellation/src/app.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export class ConstellationApp {
3131
);
3232
}
3333

34-
public async signTxn(params: any) {
35-
throw new Error(`Method not implemented: ${this.sdk},${params}`);
34+
public async signTxn(params: operations.ISignTxnParams) {
35+
return this.sdk.runOperation(() => operations.signTxn(this.sdk, params));
3636
}
3737

3838
public async destroy() {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './getPublicKeys';
22
export * from './getUserVerifiedPublicKey';
33
export * from './runGetPublicKeys';
4+
export * from './signTxn';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { ISDK } from '@cypherock/sdk-core';
2+
import {
3+
createStatusListener,
4+
assert,
5+
uint8ArrayToHex,
6+
createLoggerWithPrefix,
7+
} from '@cypherock/sdk-utils';
8+
import { APP_VERSION } from '../../constants/appId';
9+
import {
10+
SeedGenerationStatus,
11+
SignTxnStatus,
12+
} from '../../proto/generated/types';
13+
import {
14+
assertOrThrowInvalidResult,
15+
OperationHelper,
16+
logger as rootLogger,
17+
} from '../../utils';
18+
import { ISignTxnParams, ISignTxnResult, SignTxnEvent } from './types';
19+
20+
export * from './types';
21+
22+
const logger = createLoggerWithPrefix(rootLogger, 'SignTxn');
23+
24+
export const signTxn = async (
25+
sdk: ISDK,
26+
params: ISignTxnParams,
27+
): Promise<ISignTxnResult> => {
28+
assert(params, 'Params should be defined');
29+
assert(params.walletId, 'walletId should be defined');
30+
assert(params.txn, 'txn should be defined');
31+
assert(typeof params.txn === 'object', 'txn should be an object');
32+
assert(params.derivationPath, 'derivationPath should be defined');
33+
assert(
34+
params.derivationPath.length === 5,
35+
'derivationPath should be equal to 5',
36+
);
37+
38+
await sdk.checkAppCompatibility(APP_VERSION);
39+
40+
const { onStatus, forceStatusUpdate } = createStatusListener({
41+
enums: SignTxnEvent,
42+
operationEnums: SignTxnStatus,
43+
seedGenerationEnums: SeedGenerationStatus,
44+
onEvent: params.onEvent,
45+
logger,
46+
});
47+
48+
const helper = new OperationHelper({
49+
sdk,
50+
queryKey: 'signTxn',
51+
resultKey: 'signTxn',
52+
onStatus,
53+
});
54+
55+
await helper.sendQuery({
56+
initiate: {
57+
walletId: params.walletId,
58+
derivationPath: params.derivationPath,
59+
},
60+
});
61+
62+
const { confirmation } = await helper.waitForResult();
63+
assertOrThrowInvalidResult(confirmation);
64+
forceStatusUpdate(SignTxnEvent.CONFIRM);
65+
66+
await helper.sendQuery({
67+
txnData: params.txn,
68+
});
69+
70+
const accepted = await helper.waitForResult();
71+
assertOrThrowInvalidResult(accepted.unsignedTxnAccepted);
72+
73+
await helper.sendQuery({
74+
signature: {},
75+
});
76+
const result = await helper.waitForResult();
77+
assertOrThrowInvalidResult(result.signature);
78+
79+
forceStatusUpdate(SignTxnEvent.PIN_CARD);
80+
81+
return {
82+
signature: uint8ArrayToHex(result.signature.signature),
83+
};
84+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ISignTxnData } from '../../proto/generated/types';
2+
3+
export enum SignTxnEvent {
4+
INIT = 0,
5+
CONFIRM = 1,
6+
VERIFY = 2,
7+
PASSPHRASE = 3,
8+
PIN_CARD = 4,
9+
}
10+
11+
export type SignTxnEventHandler = (event: SignTxnEvent) => void;
12+
13+
export interface ISignTxnParams {
14+
onEvent?: SignTxnEventHandler;
15+
16+
walletId: Uint8Array;
17+
derivationPath: number[];
18+
txn: ISignTxnData;
19+
}
20+
21+
export interface ISignTxnResult {
22+
signature: string;
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './runGetPublicKeys/types';
22
export * from './getUserVerifiedPublicKey/types';
3+
export * from './signTxn/types';

0 commit comments

Comments
 (0)
Please sign in to comment.