@@ -9,32 +9,27 @@ import {BaseAsset, RawUtxo} from '../types/other'
9
9
import { DefaultAsset } from '../types/tokens'
10
10
import { YoroiEntry } from '../types/yoroi'
11
11
import { Amounts } from '../utils/utils'
12
+ import { CardanoMobile } from '../wallets'
12
13
import { toAssetNameHex , toPolicyId } from './api/utils'
13
14
import { withMinAmounts } from './getMinAmounts'
14
15
import { MultiToken } from './MultiToken'
15
16
import { CardanoTypes } from './types'
16
- import { wrappedCsl } from './wrappedCsl'
17
+ import { wrappedCsl as getCSL , wrappedCsl } from './wrappedCsl'
17
18
18
19
export const deriveRewardAddressHex = async (
19
20
accountPubKeyHex : string ,
20
21
chainId : number ,
21
22
role : number ,
22
23
index : number ,
23
24
) : Promise < string > => {
24
- const { csl, release} = wrappedCsl ( )
25
-
26
- try {
27
- const accountPubKeyPtr = await csl . Bip32PublicKey . fromBytes ( Buffer . from ( accountPubKeyHex , 'hex' ) )
28
- const stakingKey = await ( await ( await accountPubKeyPtr . derive ( role ) ) . derive ( index ) ) . toRawKey ( )
29
- const credential = await csl . Credential . fromKeyhash ( await stakingKey . hash ( ) )
30
- const rewardAddr = await csl . RewardAddress . new ( chainId , credential )
31
- const rewardAddrAsAddr = await rewardAddr . toAddress ( )
32
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
- const result = Buffer . from ( ( await rewardAddrAsAddr . toBytes ( ) ) as any , 'hex' ) . toString ( 'hex' )
34
- return result
35
- } finally {
36
- release ( )
37
- }
25
+ const accountPubKeyPtr = await CardanoMobile . Bip32PublicKey . fromBytes ( Buffer . from ( accountPubKeyHex , 'hex' ) )
26
+ const stakingKey = await ( await ( await accountPubKeyPtr . derive ( role ) ) . derive ( index ) ) . toRawKey ( )
27
+ const credential = await CardanoMobile . Credential . fromKeyhash ( await stakingKey . hash ( ) )
28
+ const rewardAddr = await CardanoMobile . RewardAddress . new ( chainId , credential )
29
+ const rewardAddrAsAddr = await rewardAddr . toAddress ( )
30
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
31
+ const result = Buffer . from ( ( await rewardAddrAsAddr . toBytes ( ) ) as any , 'hex' ) . toString ( 'hex' )
32
+ return result
38
33
}
39
34
40
35
export const deriveRewardAddressFromAddress = async ( address : string , chainId : number ) : Promise < string > => {
@@ -65,46 +60,34 @@ export const identifierToCardanoAsset = async (
65
60
policyId : CardanoTypes . ScriptHash
66
61
name : CardanoTypes . AssetName
67
62
} > => {
68
- const { csl, release} = wrappedCsl ( )
69
-
70
- try {
71
- const policyId = toPolicyId ( tokenId )
72
- const assetNameHex = toAssetNameHex ( tokenId )
63
+ const policyId = toPolicyId ( tokenId )
64
+ const assetNameHex = toAssetNameHex ( tokenId )
73
65
74
- return {
75
- policyId : await csl . ScriptHash . fromBytes ( Buffer . from ( policyId , 'hex' ) ) ,
76
- name : await csl . AssetName . new ( Buffer . from ( assetNameHex , 'hex' ) ) ,
77
- }
78
- } finally {
79
- release ( )
66
+ return {
67
+ policyId : await CardanoMobile . ScriptHash . fromBytes ( Buffer . from ( policyId , 'hex' ) ) ,
68
+ name : await CardanoMobile . AssetName . new ( Buffer . from ( assetNameHex , 'hex' ) ) ,
80
69
}
81
70
}
82
71
83
72
export const cardanoValueFromRemoteFormat = async ( utxo : RawUtxo ) => {
84
- const { csl, release} = wrappedCsl ( )
85
-
86
- try {
87
- const value = await csl . Value . new ( await csl . BigNum . fromStr ( utxo . amount ) )
88
- if ( utxo . assets . length === 0 ) return value
89
- const assets = await csl . MultiAsset . new ( )
90
-
91
- for ( const remoteAsset of utxo . assets ) {
92
- const { policyId, name} = await identifierToCardanoAsset ( remoteAsset . assetId )
93
- let policyContent = await assets . get ( policyId )
94
- policyContent = policyContent ?. hasValue ( ) ? policyContent : await csl . Assets . new ( )
95
- await policyContent . insert ( name , await csl . BigNum . fromStr ( remoteAsset . amount ) )
96
- // recall: we always have to insert since WASM returns copies of objects
97
- await assets . insert ( policyId , policyContent )
98
- }
99
-
100
- if ( ( await assets . len ( ) ) > 0 ) {
101
- await value . setMultiasset ( assets )
102
- }
73
+ const value = await CardanoMobile . Value . new ( await CardanoMobile . BigNum . fromStr ( utxo . amount ) )
74
+ if ( utxo . assets . length === 0 ) return value
75
+ const assets = await CardanoMobile . MultiAsset . new ( )
76
+
77
+ for ( const remoteAsset of utxo . assets ) {
78
+ const { policyId, name} = await identifierToCardanoAsset ( remoteAsset . assetId )
79
+ let policyContent = await assets . get ( policyId )
80
+ policyContent = policyContent ?. hasValue ( ) ? policyContent : await CardanoMobile . Assets . new ( )
81
+ await policyContent . insert ( name , await CardanoMobile . BigNum . fromStr ( remoteAsset . amount ) )
82
+ // recall: we always have to insert since WASM returns copies of objects
83
+ await assets . insert ( policyId , policyContent )
84
+ }
103
85
104
- return value
105
- } finally {
106
- release ( )
86
+ if ( ( await assets . len ( ) ) > 0 ) {
87
+ await value . setMultiasset ( assets )
107
88
}
89
+
90
+ return value
108
91
}
109
92
// matches RawUtxo and a tx input/output
110
93
type RemoteValue = {
@@ -188,52 +171,40 @@ export const isTokenInfo = (token: Balance.TokenInfo | DefaultAsset): token is B
188
171
}
189
172
190
173
export const generateCIP30UtxoCbor = async ( utxo : RawUtxo ) => {
191
- const { csl, release} = wrappedCsl ( )
192
-
193
- try {
194
- const txHash = await csl . TransactionHash . fromBytes ( Buffer . from ( utxo . tx_hash , 'hex' ) )
195
- if ( ! txHash ) throw new Error ( 'Invalid tx hash' )
174
+ const txHash = await CardanoMobile . TransactionHash . fromBytes ( Buffer . from ( utxo . tx_hash , 'hex' ) )
175
+ if ( ! txHash ) throw new Error ( 'Invalid tx hash' )
196
176
197
- const index = utxo . tx_index
198
- const input = await csl . TransactionInput . new ( txHash , index )
199
- const address = await csl . Address . fromBech32 ( utxo . receiver )
200
- if ( ! address ) throw new Error ( 'Invalid address' )
177
+ const index = utxo . tx_index
178
+ const input = await CardanoMobile . TransactionInput . new ( txHash , index )
179
+ const address = await CardanoMobile . Address . fromBech32 ( utxo . receiver )
180
+ if ( ! address ) throw new Error ( 'Invalid address' )
201
181
202
- const amount = await csl . BigNum . fromStr ( utxo . amount )
203
- if ( ! amount ) throw new Error ( 'Invalid amount' )
182
+ const amount = await CardanoMobile . BigNum . fromStr ( utxo . amount )
183
+ if ( ! amount ) throw new Error ( 'Invalid amount' )
204
184
205
- const collateral = await csl . Value . new ( amount )
206
- const output = await csl . TransactionOutput . new ( address , collateral )
207
- const transactionUnspentOutput = await csl . TransactionUnspentOutput . new ( input , output )
185
+ const collateral = await CardanoMobile . Value . new ( amount )
186
+ const output = await CardanoMobile . TransactionOutput . new ( address , collateral )
187
+ const transactionUnspentOutput = await CardanoMobile . TransactionUnspentOutput . new ( input , output )
208
188
209
- return transactionUnspentOutput . toHex ( )
210
- } finally {
211
- release ( )
212
- }
189
+ return transactionUnspentOutput . toHex ( )
213
190
}
214
191
215
192
export const createRawTxSigningKey = async ( rootKey : string , derivationPath : number [ ] ) => {
216
- const { csl, release} = wrappedCsl ( )
217
-
218
- try {
219
- if ( derivationPath . length !== 5 ) throw new Error ( 'Invalid derivation path' )
220
- const masterKey = await csl . Bip32PrivateKey . fromBytes ( Buffer . from ( rootKey , 'hex' ) )
221
- const accountPrivateKey = await masterKey
222
- . derive ( derivationPath [ 0 ] )
223
- . then ( ( key ) => key . derive ( derivationPath [ 1 ] ) )
224
- . then ( ( key ) => key . derive ( derivationPath [ 2 ] ) )
225
- . then ( ( key ) => key . derive ( derivationPath [ 3 ] ) )
226
- . then ( ( key ) => key . derive ( derivationPath [ 4 ] ) )
227
-
228
- const rawKey = await accountPrivateKey . toRawKey ( )
229
- const bech32 = await rawKey . toBech32 ( )
230
-
231
- const pkey = await csl . PrivateKey . fromBech32 ( bech32 )
232
- if ( ! pkey ) throw new Error ( 'Invalid private key' )
233
- return pkey
234
- } finally {
235
- release ( )
236
- }
193
+ if ( derivationPath . length !== 5 ) throw new Error ( 'Invalid derivation path' )
194
+ const masterKey = await CardanoMobile . Bip32PrivateKey . fromBytes ( Buffer . from ( rootKey , 'hex' ) )
195
+ const accountPrivateKey = await masterKey
196
+ . derive ( derivationPath [ 0 ] )
197
+ . then ( ( key ) => key . derive ( derivationPath [ 1 ] ) )
198
+ . then ( ( key ) => key . derive ( derivationPath [ 2 ] ) )
199
+ . then ( ( key ) => key . derive ( derivationPath [ 3 ] ) )
200
+ . then ( ( key ) => key . derive ( derivationPath [ 4 ] ) )
201
+
202
+ const rawKey = await accountPrivateKey . toRawKey ( )
203
+ const bech32 = await rawKey . toBech32 ( )
204
+
205
+ const pkey = await CardanoMobile . PrivateKey . fromBech32 ( bech32 )
206
+ if ( ! pkey ) throw new Error ( 'Invalid private key' )
207
+ return pkey
237
208
}
238
209
239
210
export const copyFromCSL = async < T extends { toHex : ( ) => Promise < string > } > (
@@ -259,8 +230,7 @@ export const getTransactionUnspentOutput = async ({
259
230
bytes : Uint8Array
260
231
index : number
261
232
} ) => {
262
- const { csl, release} = wrappedCsl ( )
263
-
233
+ const { csl, release} = getCSL ( )
264
234
try {
265
235
const tx = await csl . Transaction . fromBytes ( bytes )
266
236
const body = await tx . body ( )
@@ -271,7 +241,10 @@ export const getTransactionUnspentOutput = async ({
271
241
const value = await originalOutput . amount ( )
272
242
const receiver = await originalOutput . address ( )
273
243
const output = await csl . TransactionOutput . new ( receiver , value )
274
- return copyFromCSL ( csl . TransactionUnspentOutput , await csl . TransactionUnspentOutput . new ( input , output ) )
244
+ return copyFromCSL (
245
+ CardanoMobile . TransactionUnspentOutput ,
246
+ await CardanoMobile . TransactionUnspentOutput . new ( input , output ) ,
247
+ )
275
248
} finally {
276
249
release ( )
277
250
}
0 commit comments