/
shelley-trezor-crypto-provider.ts
225 lines (194 loc) · 6 KB
/
shelley-trezor-crypto-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// eslint-disable-next-line import/no-unresolved
import CachedDeriveXpubFactory from '../helpers/CachedDeriveXpubFactory'
import {ADALITE_SUPPORT_EMAIL} from '../constants'
import derivationSchemes from '../helpers/derivation-schemes'
import NamedError from '../../helpers/NamedError'
import debugLog from '../../helpers/debugLog'
const CardanoTrezorCryptoProvider = ({network, config}) => {
const derivationScheme = derivationSchemes.v2
const TrezorConnect = require('../../libs/trezor-connect-local').default
TrezorConnect.manifest({
email: ADALITE_SUPPORT_EMAIL,
appUrl: config.ADALITE_SERVER_URL,
})
const isHwWallet = () => true
const getHwWalletName = () => 'Trezor'
const deriveXpub = CachedDeriveXpubFactory(derivationScheme, async (absDerivationPath) => {
const response = await TrezorConnect.cardanoGetPublicKey({
path: absDerivationPath,
showOnTrezor: false,
})
throwIfNotSuccess(response)
return Buffer.from(response.payload.publicKey, 'hex')
})
function deriveHdNode(childIndex) {
throw NamedError(
'UnsupportedOperationError',
'This operation is not supported on TrezorCryptoProvider!'
)
}
function sign(message, absDerivationPath) {
throw NamedError('UnsupportedOperationError', 'Operation not supported')
}
async function displayAddressForPath(absDerivationPath) {
const response = await TrezorConnect.cardanoGetAddress({
path: absDerivationPath,
showOnTrezor: true,
})
throwIfNotSuccess(response)
}
type CardanoTxInputType = {
address_n: Array<number>
prev_hash: Buffer
prev_index: number
}
type CardanoAddressParametersType = {
address_type: number // 0 for base address etc.
address_n: Array<number> // BIP32
address_n_staking: Array<number>
staking_key_hash?: Buffer
certificate_pointer?: any // TODO: CardanoBlockchainPointerType
}
type CardanoTxOutputType = {
address: string
amount: number
address_parameters?: CardanoAddressParametersType
}
type CardanoTxCertificateType = {
type: number
path: Array<number>
pool: Buffer
}
function prepareInput(input, addressToAbsPathMapper): CardanoTxInputType {
const data = {
address_n: addressToAbsPathMapper(input.address),
prev_hash: Buffer.from(input.txid, 'hex'),
prev_index: input.outputIndex,
}
return data
}
function prepareOutput(output, addressToAbsPathMapper): CardanoTxOutputType {
const data: CardanoTxOutputType = {
address: output.address,
amount: output.coins,
}
let addressParameters: CardanoAddressParametersType
if (output.isChange) {
addressParameters = {
address_type: 0, // TODO: 0 for base address
address_n: output.spendingPath,
address_n_staking: output.stakingPath,
}
data.address_parameters = addressParameters
}
return data
}
function prepareCertificate(cert, addressToAbsPathMapper): CardanoTxCertificateType {
return {
type: cert.type,
path: addressToAbsPathMapper(cert.accountAddress),
pool: cert.poolHash ? Buffer.from(cert.poolHash, 'hex') : null,
}
}
async function signTx(unsignedTx, rawInputTxs, addressToAbsPathMapper) {
const _inputs = []
for (const input of unsignedTx.inputs) {
const data = prepareInput(input, addressToAbsPathMapper)
_inputs.push(data)
}
const inputs = await Promise.all(_inputs)
const _outputs = []
for (const output of unsignedTx.outputs) {
const data = prepareOutput(output, addressToAbsPathMapper)
_outputs.push(data)
}
const outputs = await Promise.all(_outputs)
const certificates = []
for (const cert of unsignedTx.certs) {
const data = prepareCertificate(cert, addressToAbsPathMapper)
certificates.push(data)
}
const fee = `${unsignedTx.fee.fee}`
const ttl = `${unsignedTx.ttl.ttl}`
const withdrawals = []
const metadata = null
// inputs: List[CardanoTxInputType] = None,
// outputs: List[CardanoTxOutputType] = None,
// protocol_magic: int = None,
// fee: int = None,
// ttl: int = None,
// network_id: int = None,
// certificates: List[CardanoTxCertificateType] = None,
// withdrawals: List[CardanoTxWithdrawalType] = None,
// metadata: bytes = None,
console.log('\n\n\n\n\n\n\n', {
inputs,
outputs,
protocol_magic: network.protocolMagic,
fee: unsignedTx.fee,
ttl: unsignedTx.ttl,
network_id: network.networkId,
certificates,
withdrawals,
})
const response = await TrezorConnect.cardanoSignTransaction({
inputs,
outputs,
protocolMagic: network.protocolMagic,
fee,
ttl,
networkId: network.networkId,
// certificates,
// withdrawals,
// metadata,
}).catch((e) =>
console.log(e, {
inputs,
outputs,
protocolMagic: network.protocolMagic,
fee: unsignedTx.fee.fee,
ttl: unsignedTx.ttl.ttl,
networkId: network.networkId,
certificates,
// withdrawals,
// metadata,
})
)
if (response.error || !response.success) {
debugLog(response)
throw NamedError('TrezorSignTxError', response.payload.error)
}
return {
txHash: response.payload.hash,
txBody: response.payload.body,
}
}
function getWalletSecret() {
throw NamedError('UnsupportedOperationError', 'Unsupported operation!')
}
function getDerivationScheme() {
return derivationScheme
}
function throwIfNotSuccess(response) {
if (response.error || !response.success) {
debugLog(response)
throw NamedError(
'TrezorError',
'Trezor operation failed, please make sure ad blockers are switched off for this site'
)
}
}
return {
getWalletSecret,
getDerivationScheme,
signTx,
displayAddressForPath,
deriveXpub,
isHwWallet,
getHwWalletName,
_sign: sign,
_deriveHdNode: deriveHdNode,
network,
}
}
export default CardanoTrezorCryptoProvider