Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Begin callback loop
while (true) {
if (responseType === MessageType.MESSAGETYPE_FAILURE) {
const errorResponse = response as Failure
throw new Error(`Signing failed: ${errorResponse.getMessage()}`)
}
if (responseType !== MessageType.MESSAGETYPE_TXREQUEST) {
throw new Error(`Unexpected message type: ${responseType}`)
}
let txRequest = response as TxRequest
// If there's some part of signed transaction, add it
if (txRequest.hasSerialized() && txRequest.getSerialized().hasSerializedTx()) {
serializedTx += toHexString(txRequest.getSerialized().getSerializedTx_asU8())
}
if (txRequest.hasSerialized() && txRequest.getSerialized().hasSignatureIndex()) {
if (signatures[txRequest.getSerialized().getSignatureIndex()] !== null) {
throw new Error(`Signature for index ${txRequest.getSerialized().getSignatureIndex()} already filled`)
}
signatures[txRequest.getSerialized().getSignatureIndex()] = toHexString(txRequest.getSerialized().getSignature_asU8())
}
if (txRequest.getRequestType() === RequestType.TXFINISHED) {
// Device didn't ask for more information, finish workflow
break
}
let currentTx: TransactionType = null
let msg: TransactionType = null
if (responseType !== MessageType.MESSAGETYPE_TXREQUEST) {
throw new Error(`Unexpected message type: ${responseType}`)
}
let txRequest = response as TxRequest
// If there's some part of signed transaction, add it
if (txRequest.hasSerialized() && txRequest.getSerialized().hasSerializedTx()) {
serializedTx += toHexString(txRequest.getSerialized().getSerializedTx_asU8())
}
if (txRequest.hasSerialized() && txRequest.getSerialized().hasSignatureIndex()) {
if (signatures[txRequest.getSerialized().getSignatureIndex()] !== null) {
throw new Error(`Signature for index ${txRequest.getSerialized().getSignatureIndex()} already filled`)
}
signatures[txRequest.getSerialized().getSignatureIndex()] = toHexString(txRequest.getSerialized().getSignature_asU8())
}
if (txRequest.getRequestType() === RequestType.TXFINISHED) {
// Device didn't ask for more information, finish workflow
break
}
let currentTx: TransactionType = null
let msg: TransactionType = null
let txAck: TxAck = null
// Device asked for one more information, let's process it.
if (txRequest.hasDetails() && !txRequest.getDetails().hasTxHash()) {
currentTx = txmap['unsigned']
} else {
currentTx = txmap[toHexString(txRequest.getDetails().getTxHash_asU8())]
export async function ethSignMessage (transport: KeepKeyTransport, msg: ETHSignMessage): Promise {
const m = new EthereumSignMessage()
m.setAddressNList(msg.addressNList)
m.setMessage(toUTF8Array(msg.message))
const response = await transport.call(MessageType.MESSAGETYPE_ETHEREUMSIGNMESSAGE, m, LONG_TIMEOUT) as Event
const sig = response.proto as EthereumMessageSignature
return {
address: EIP55.encode('0x' + toHexString(sig.getAddress_asU8())), // FIXME: this should be done in the firmware
signature: '0x' + toHexString(sig.getSignature_asU8())
}
}
export async function ethSignMessage (transport: KeepKeyTransport, msg: ETHSignMessage): Promise {
const m = new EthereumSignMessage()
m.setAddressNList(msg.addressNList)
m.setMessage(toUTF8Array(msg.message))
const response = await transport.call(MessageType.MESSAGETYPE_ETHEREUMSIGNMESSAGE, m, LONG_TIMEOUT) as Event
const sig = response.proto as EthereumMessageSignature
return {
address: EIP55.encode('0x' + toHexString(sig.getAddress_asU8())), // FIXME: this should be done in the firmware
signature: '0x' + toHexString(sig.getSignature_asU8())
}
}
export async function btcSignMessage (transport: TrezorTransport, msg: BTCSignMessage): Promise {
let res = await transport.call('signMessage', {
path: msg.addressNList,
message: msg.message,
coin: translateCoin(msg.coin)
})
handleError(transport, res, "Could not sign message with Trezor")
return {
address: res.payload.address,
signature: toHexString(Uint8Array.from(Base64.toByteArray(res.payload.signature)))
}
}
export async function ethGetAddress (transport: KeepKeyTransport, msg: ETHGetAddress): Promise {
const getAddr = new EthereumGetAddress()
getAddr.setAddressNList(msg.addressNList)
getAddr.setShowDisplay(msg.showDisplay !== false)
const response = await transport.call(MessageType.MESSAGETYPE_ETHEREUMGETADDRESS, getAddr, LONG_TIMEOUT)
const ethAddress = response.proto as EthereumAddress
if(response.message_type === Events.FAILURE) throw response
let address = null
if (ethAddress.hasAddressStr())
address = ethAddress.getAddressStr()
else if (ethAddress.hasAddress())
address = '0x' + toHexString(ethAddress.getAddress_asU8())
else
throw new Error('Unable to obtain ETH address from device.')
return address
}
handleError(res, transport, 'Could not sign ETH tx with Ledger')
const { v, r, s } = res.payload
const tx = new EthereumTx({
...txParams,
v: '0x' + v,
r: '0x' + r,
s: '0x' + s
})
return {
v: parseInt(v, 16),
r: '0x' + r,
s: '0x' + s,
serialized: '0x' + toHexString(tx.serialize())
}
}
path: msg.addressNList,
transaction: utx
})
handleError(transport, res, "Could not sign ETH transaction with Trezor")
const tx = new EthereumTx(utx)
tx.v = res.payload.v
tx.r = res.payload.r
tx.s = res.payload.s
return {
v: parseInt(res.payload.v),
r: res.payload.r,
s: res.payload.s,
serialized: '0x' + toHexString(tx.serialize())
}
}
export async function btcSignMessage (wallet: BTCWallet, transport: KeepKeyTransport, msg: BTCSignMessage): Promise {
await ensureCoinSupport(wallet, msg.coin)
const sign = new SignMessage()
sign.setAddressNList(msg.addressNList)
sign.setMessage(toUTF8Array(msg.message))
sign.setCoinName(msg.coin || 'Bitcoin')
sign.setScriptType(translateInputScriptType(msg.scriptType || BTCInputScriptType.SpendAddress))
const event = await transport.call(MessageType.MESSAGETYPE_SIGNMESSAGE, sign, LONG_TIMEOUT) as Event
const messageSignature = event.proto as MessageSignature
return {
address: messageSignature.getAddress(),
signature: toHexString(messageSignature.getSignature_asU8())
}
}
chainId: msg.chainId,
nonce: msg.nonce,
gasLimit: msg.gasLimit,
gasPrice: msg.gasPrice,
r,
s,
v
}
const tx = new EthereumTx(utx)
return {
r,
s,
v: response.getSignatureV(),
serialized: '0x' + toHexString(tx.serialize())
}
})
}