-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added encryption and decryption operations in inheritance app (#…
…120) Co-authored-by: TejasvOnly <[email protected]> Co-authored-by: Md Irshad Ansari <[email protected]> Co-authored-by: Vaibhav Sethia <[email protected]> fix: remove inbetween acks from decryption flow (#122) fix: update chunking methods to send at least a single chunk (#126)
- Loading branch information
1 parent
d090cc9
commit 9b2748a
Showing
31 changed files
with
8,270 additions
and
2,493 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './appId'; | ||
export * from './walletId'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const WALLET_ID_LENGTH = 32; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 13 additions & 5 deletions
18
packages/app-inheritance/src/operations/authWallet/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
export enum WalletAuthEvent { | ||
export enum AuthWalletEvent { | ||
INIT = 0, | ||
CARD_TAP = 1, | ||
CONFIRMED = 1, | ||
SEED_BASED_CARD_TAP = 2, | ||
CARD_PAIRING_CARD_TAP = 3, | ||
WALLET_BASED_CARD_TAP = 4, | ||
} | ||
|
||
export type AuthWalletEventHandler = (event: WalletAuthEvent) => void; | ||
export const WALLET_ID_LENGTH = 32; | ||
export type AuthWalletType = | ||
| 'seed-based' | ||
| 'wallet-based' | ||
| 'seed-and-wallet-based'; | ||
|
||
export type AuthWalletEventHandler = (event: AuthWalletEvent) => void; | ||
|
||
export interface IAuthWalletParams { | ||
challenge: Uint8Array; | ||
walletId: Uint8Array; | ||
isPublicKey: boolean; | ||
withPublicKey: boolean; | ||
type: AuthWalletType; | ||
onEvent?: AuthWalletEventHandler; | ||
} |
100 changes: 100 additions & 0 deletions
100
packages/app-inheritance/src/operations/decryptMessagesWithPin/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { ISDK } from '@cypherock/sdk-core'; | ||
import { | ||
assert, | ||
createLoggerWithPrefix, | ||
createStatusListener, | ||
} from '@cypherock/sdk-utils'; | ||
import { WALLET_ID_LENGTH } from '../../constants'; | ||
import { APP_VERSION } from '../../constants/appId'; | ||
import { | ||
DecryptDataWithPinDecryptedDataStructure, | ||
DecryptDataWithPinEncryptedDataStructure, | ||
} from '../../proto/generated/inheritance/decrypt_data_with_pin'; | ||
import { DecryptDataStatus } from '../../types'; | ||
import { | ||
assertOrThrowInvalidResult, | ||
OperationHelper, | ||
logger as rootLogger, | ||
} from '../../utils'; | ||
import { | ||
DecryptMessagesWithPinEvent, | ||
IDecryptMessagesWithPinParams, | ||
IDecryptMessagesWithPinResult, | ||
} from './types'; | ||
|
||
export * from './types'; | ||
|
||
const logger = createLoggerWithPrefix(rootLogger, 'decryptMessages'); | ||
|
||
export const decryptMessagesWithPin = async ( | ||
sdk: ISDK, | ||
params: IDecryptMessagesWithPinParams, | ||
): Promise<IDecryptMessagesWithPinResult> => { | ||
assert(params, 'Params should be defined'); | ||
assert(params.walletId, 'walletId should be defined'); | ||
assert( | ||
params.walletId.length === WALLET_ID_LENGTH, | ||
`Wallet Id should be exactly ${WALLET_ID_LENGTH} bytes`, | ||
); | ||
assert(params.encryptedData, 'data should be defined'); | ||
assert( | ||
params.encryptedData.length > 0, | ||
'At least one message required to decrypt', | ||
); | ||
|
||
await sdk.checkAppCompatibility(APP_VERSION); | ||
|
||
logger.info('Started', { ...params, onEvent: undefined }); | ||
|
||
const { forceStatusUpdate, onStatus } = createStatusListener({ | ||
enums: DecryptMessagesWithPinEvent, | ||
operationEnums: DecryptDataStatus, | ||
onEvent: params.onEvent, | ||
logger, | ||
}); | ||
|
||
const helper = new OperationHelper({ | ||
sdk, | ||
queryKey: 'decrypt', | ||
resultKey: 'decrypt', | ||
onStatus, | ||
}); | ||
|
||
await helper.sendQuery({ | ||
initiate: { | ||
walletId: params.walletId, | ||
}, | ||
}); | ||
|
||
await helper.waitForResult(); | ||
logger.verbose('decryptMessages confirmed'); | ||
|
||
const rawData = DecryptDataWithPinEncryptedDataStructure.encode( | ||
DecryptDataWithPinEncryptedDataStructure.create({ | ||
data: params.encryptedData, | ||
}), | ||
).finish(); | ||
await helper.sendInChunks(rawData, 'encryptedData', 'dataAccepted'); | ||
|
||
const decryptedData = await helper.receiveInChunks( | ||
'decryptedDataRequest', | ||
'decryptedData', | ||
); | ||
const result = DecryptDataWithPinDecryptedDataStructure.decode(decryptedData); | ||
logger.verbose('decryptMessages response', result); | ||
|
||
assertOrThrowInvalidResult(result.decryptedData); | ||
|
||
const output: IDecryptMessagesWithPinResult = {}; | ||
|
||
for (const data of result.decryptedData) { | ||
output[data.tag] = { | ||
data: data.message, | ||
dataAsString: Buffer.from(data.message).toString(), | ||
}; | ||
} | ||
|
||
forceStatusUpdate(DecryptMessagesWithPinEvent.PIN_VERIFIED); | ||
logger.info('Completed'); | ||
return output; | ||
}; |
26 changes: 26 additions & 0 deletions
26
packages/app-inheritance/src/operations/decryptMessagesWithPin/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export enum DecryptMessagesWithPinEvent { | ||
INIT = 0, | ||
CONFIRMED = 1, | ||
MESSAGE_DECRYPTED_CARD_TAP = 2, | ||
PIN_VERIFIED = 3, | ||
} | ||
|
||
export type DecryptMessagesWithPinEventHandler = ( | ||
event: DecryptMessagesWithPinEvent, | ||
) => void; | ||
|
||
export interface IDecryptMessagesWithPinResultValue { | ||
data: Uint8Array; | ||
dataAsString: string; | ||
} | ||
|
||
export type IDecryptMessagesWithPinResult = Record< | ||
number, | ||
IDecryptMessagesWithPinResultValue | ||
>; | ||
|
||
export interface IDecryptMessagesWithPinParams { | ||
walletId: Uint8Array; | ||
encryptedData: Uint8Array; | ||
onEvent?: DecryptMessagesWithPinEventHandler; | ||
} |
95 changes: 95 additions & 0 deletions
95
packages/app-inheritance/src/operations/encryptMessagesWithPin/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { ISDK } from '@cypherock/sdk-core'; | ||
import { | ||
assert, | ||
createLoggerWithPrefix, | ||
createStatusListener, | ||
} from '@cypherock/sdk-utils'; | ||
import { WALLET_ID_LENGTH } from '../../constants'; | ||
import { APP_VERSION } from '../../constants/appId'; | ||
import { | ||
EncryptDataWithPinEncryptedDataStructure, | ||
EncryptDataWithPinPlainDataStructure, | ||
} from '../../proto/generated/inheritance/encrypt_data_with_pin'; | ||
import { EncryptDataStatus } from '../../types'; | ||
import { | ||
assertOrThrowInvalidResult, | ||
OperationHelper, | ||
logger as rootLogger, | ||
} from '../../utils'; | ||
import { | ||
EncryptMessagesWithPinEvent, | ||
IEncryptMessagesWithPinParams, | ||
IEncryptMessagesWithPinResult, | ||
} from './types'; | ||
|
||
export * from './types'; | ||
|
||
const logger = createLoggerWithPrefix(rootLogger, 'encryptMessages'); | ||
|
||
export const encryptMessageWithPin = async ( | ||
sdk: ISDK, | ||
params: IEncryptMessagesWithPinParams, | ||
): Promise<IEncryptMessagesWithPinResult> => { | ||
assert(params, 'Params should be defined'); | ||
assert(params.walletId, 'walletId should be defined'); | ||
assert(params.messages, 'messages should be defined'); | ||
assert( | ||
params.walletId.length === WALLET_ID_LENGTH, | ||
`Wallet Id should be exactly ${WALLET_ID_LENGTH} bytes`, | ||
); | ||
Object.values(params.messages).forEach(message => | ||
assert(message.value, 'Every message should have a valid value'), | ||
); | ||
|
||
await sdk.checkAppCompatibility(APP_VERSION); | ||
|
||
logger.info('Started', { ...params, onEvent: undefined }); | ||
|
||
const { forceStatusUpdate, onStatus } = createStatusListener({ | ||
enums: EncryptMessagesWithPinEvent, | ||
operationEnums: EncryptDataStatus, | ||
onEvent: params.onEvent, | ||
logger, | ||
}); | ||
|
||
const helper = new OperationHelper({ | ||
sdk, | ||
queryKey: 'encrypt', | ||
resultKey: 'encrypt', | ||
onStatus, | ||
}); | ||
|
||
await helper.sendQuery({ | ||
initiate: { | ||
walletId: params.walletId, | ||
}, | ||
}); | ||
|
||
await helper.waitForResult(); | ||
logger.verbose('encryptMessages confirmed'); | ||
|
||
const rawData = EncryptDataWithPinPlainDataStructure.encode( | ||
EncryptDataWithPinPlainDataStructure.create({ | ||
data: Object.entries(params.messages).map(([key, message]) => ({ | ||
message: Buffer.from(message.value), | ||
isVerifiedOnDevice: message.verifyOnDevice ?? false, | ||
tag: parseInt(key, 10), | ||
})), | ||
}), | ||
).finish(); | ||
await helper.sendInChunks(rawData, 'plainData', 'dataAccepted'); | ||
|
||
const encryptedData = await helper.receiveInChunks( | ||
'encryptedDataRequest', | ||
'encryptedData', | ||
); | ||
const result = EncryptDataWithPinEncryptedDataStructure.decode(encryptedData); | ||
logger.verbose('encryptMessages response', result); | ||
|
||
assertOrThrowInvalidResult(result.encryptedData); | ||
|
||
forceStatusUpdate(EncryptMessagesWithPinEvent.MESSAGE_ENCRYPTED_CARD_TAP); | ||
|
||
logger.info('Completed'); | ||
return { encryptedPacket: result.encryptedData }; | ||
}; |
Oops, something went wrong.