-
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 sign-txn functionality to xrp app
- Loading branch information
1 parent
d242be6
commit 591d689
Showing
7 changed files
with
196 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './getPublicKeys'; | ||
export * from './getUserVerifiedPublicKey'; | ||
export * from './runGetPublicKeys'; | ||
export * from './signTxn'; |
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,105 @@ | ||
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, | ||
getXrpLib, | ||
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.rawTxn === 'object', | ||
'txn.rawTxn should be an object', | ||
); | ||
assert( | ||
typeof params.txn.txnHex === 'string', | ||
'txn.txnHex should be a string', | ||
); | ||
assert(params.derivationPath, 'derivationPath should be defined'); | ||
assert( | ||
params.derivationPath.length === 5, | ||
'derivationPath should be equal to 5', | ||
); | ||
|
||
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.txnHex); | ||
|
||
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'); | ||
|
||
await helper.sendQuery({ | ||
signature: {}, | ||
}); | ||
const result = await helper.waitForResult(); | ||
assertOrThrowInvalidResult(result.signature); | ||
|
||
forceStatusUpdate(SignTxnEvent.PIN_CARD); | ||
|
||
const signature = uint8ArrayToHex(result.signature.signature); | ||
|
||
let serializedTxn: string | undefined; | ||
|
||
if (params.serializeTxn) { | ||
const signedTransaction = { | ||
...params.txn.rawTxn, | ||
TxnSignature: signature, | ||
}; | ||
serializedTxn = getXrpLib().encode(signedTransaction); | ||
} | ||
|
||
return { | ||
signature, | ||
serializedTxn, | ||
}; | ||
}; |
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,30 @@ | ||
import { Payment as PaymentTransaction } from 'xrpl'; | ||
|
||
export enum SignTxnEvent { | ||
INIT = 0, | ||
CONFIRM = 1, | ||
VERIFY = 2, | ||
PASSPHRASE = 3, | ||
PIN_CARD = 4, | ||
} | ||
|
||
export type SignTxnEventHandler = (event: SignTxnEvent) => void; | ||
|
||
export interface IUnsignedTransaction { | ||
rawTxn: PaymentTransaction; | ||
txnHex: string; | ||
} | ||
|
||
export interface ISignTxnParams { | ||
onEvent?: SignTxnEventHandler; | ||
|
||
walletId: Uint8Array; | ||
derivationPath: number[]; | ||
txn: IUnsignedTransaction; | ||
serializeTxn?: boolean; | ||
} | ||
|
||
export interface ISignTxnResult { | ||
signature: string; | ||
serializedTxn?: string; | ||
} |
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,2 +1,3 @@ | ||
export * from './runGetPublicKeys/types'; | ||
export * from './getUserVerifiedPublicKey/types'; | ||
export * from './signTxn/types'; |
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