|
| 1 | +import { TypedCall } from '@onekeyfe/hd-transport'; |
| 2 | +import { TypedResponseMessage } from '../../device/DeviceCommands'; |
| 3 | +import { validatePath } from '../helpers/pathUtils'; |
| 4 | +import { BaseMethod } from '../BaseMethod'; |
| 5 | +import { validateParams } from '../helpers/paramsValidator'; |
| 6 | +import { NexaSignTransactionParams, NexaSignature } from '../../types'; |
| 7 | + |
| 8 | +export default class NexaSignTransaction extends BaseMethod<NexaSignTransactionParams> { |
| 9 | + hasBundle = false; |
| 10 | + |
| 11 | + init() { |
| 12 | + const payload = this.payload as NexaSignTransactionParams; |
| 13 | + |
| 14 | + payload.inputs.forEach(input => { |
| 15 | + validateParams(input, [ |
| 16 | + { name: 'path', type: 'string', required: true }, |
| 17 | + { name: 'message', type: 'string', required: true }, |
| 18 | + { name: 'prefix', type: 'string', required: true }, |
| 19 | + ]); |
| 20 | + return input; |
| 21 | + }); |
| 22 | + this.params = payload; |
| 23 | + } |
| 24 | + |
| 25 | + getVersionRange() { |
| 26 | + return { |
| 27 | + model_mini: { |
| 28 | + min: '3.2.0', |
| 29 | + }, |
| 30 | + model_touch: { |
| 31 | + min: '4.4.0', |
| 32 | + }, |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + async processTxRequest( |
| 37 | + typedCall: TypedCall, |
| 38 | + res: TypedResponseMessage<'NexaTxInputRequest'> | TypedResponseMessage<'NexaSignedTx'>, |
| 39 | + index: number, |
| 40 | + signature: NexaSignature[] |
| 41 | + ): Promise<NexaSignature[]> { |
| 42 | + if (res.type === 'NexaSignedTx') { |
| 43 | + signature.push({ |
| 44 | + index, |
| 45 | + signature: res.message.signature, |
| 46 | + }); |
| 47 | + |
| 48 | + return signature; |
| 49 | + } |
| 50 | + |
| 51 | + if (res.type === 'NexaTxInputRequest') { |
| 52 | + signature.push({ |
| 53 | + index, |
| 54 | + signature: res.message.signature ?? '', |
| 55 | + }); |
| 56 | + |
| 57 | + const nextIndex = res.message.request_index; |
| 58 | + const input = this.params.inputs[nextIndex]; |
| 59 | + const response = await typedCall( |
| 60 | + 'NexaTxInputAck', |
| 61 | + // @ts-expect-error |
| 62 | + ['NexaTxInputRequest', 'NexaSignedTx'], |
| 63 | + { |
| 64 | + address_n: input.path, |
| 65 | + raw_message: input.message, |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + return this.processTxRequest(typedCall, response, nextIndex, signature); |
| 70 | + } |
| 71 | + |
| 72 | + return signature; |
| 73 | + } |
| 74 | + |
| 75 | + async run() { |
| 76 | + const { device, params } = this; |
| 77 | + const input = params.inputs[0]; |
| 78 | + |
| 79 | + const response = await device.commands.typedCall( |
| 80 | + 'NexaSignTx', |
| 81 | + ['NexaTxInputRequest', 'NexaSignedTx'], |
| 82 | + { |
| 83 | + address_n: validatePath(input.path, 3), |
| 84 | + raw_message: input.message, |
| 85 | + prefix: input.prefix, |
| 86 | + input_count: params.inputs.length, |
| 87 | + } |
| 88 | + ); |
| 89 | + return this.processTxRequest( |
| 90 | + device.commands.typedCall.bind(device.commands), |
| 91 | + response as any, |
| 92 | + 0, |
| 93 | + [] |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
0 commit comments