Skip to content

Commit 713e411

Browse files
committed
CR: update
1 parent 62319a9 commit 713e411

File tree

2 files changed

+67
-90
lines changed

2 files changed

+67
-90
lines changed

apps/wallet-mobile/src/yoroi-wallets/cardano/cip30/cip30.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ describe('cip30ExtensionMaker', () => {
2323
})
2424

2525
it('should support getBalance', async () => {
26+
console.log('test-1')
2627
const cip30 = cip30ExtensionMaker(mocks.wallet, mocks.walletMeta)
28+
console.log('test2', cip30)
2729
const result = await cip30.getBalance()
30+
console.log('test3')
2831
expect(result).toBeDefined()
32+
console.log('test4')
2933
expect(await (await result.coin()).toStr()).toBe('2282543724')
3034
})
3135

apps/wallet-mobile/src/yoroi-wallets/cardano/utils.ts

+63-90
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,27 @@ import {BaseAsset, RawUtxo} from '../types/other'
99
import {DefaultAsset} from '../types/tokens'
1010
import {YoroiEntry} from '../types/yoroi'
1111
import {Amounts} from '../utils/utils'
12+
import {CardanoMobile} from '../wallets'
1213
import {toAssetNameHex, toPolicyId} from './api/utils'
1314
import {withMinAmounts} from './getMinAmounts'
1415
import {MultiToken} from './MultiToken'
1516
import {CardanoTypes} from './types'
16-
import {wrappedCsl} from './wrappedCsl'
17+
import {wrappedCsl as getCSL, wrappedCsl} from './wrappedCsl'
1718

1819
export const deriveRewardAddressHex = async (
1920
accountPubKeyHex: string,
2021
chainId: number,
2122
role: number,
2223
index: number,
2324
): 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
3833
}
3934

4035
export const deriveRewardAddressFromAddress = async (address: string, chainId: number): Promise<string> => {
@@ -65,46 +60,34 @@ export const identifierToCardanoAsset = async (
6560
policyId: CardanoTypes.ScriptHash
6661
name: CardanoTypes.AssetName
6762
}> => {
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)
7365

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')),
8069
}
8170
}
8271

8372
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+
}
10385

104-
return value
105-
} finally {
106-
release()
86+
if ((await assets.len()) > 0) {
87+
await value.setMultiasset(assets)
10788
}
89+
90+
return value
10891
}
10992
// matches RawUtxo and a tx input/output
11093
type RemoteValue = {
@@ -188,52 +171,40 @@ export const isTokenInfo = (token: Balance.TokenInfo | DefaultAsset): token is B
188171
}
189172

190173
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')
196176

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')
201181

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')
204184

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)
208188

209-
return transactionUnspentOutput.toHex()
210-
} finally {
211-
release()
212-
}
189+
return transactionUnspentOutput.toHex()
213190
}
214191

215192
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
237208
}
238209

239210
export const copyFromCSL = async <T extends {toHex: () => Promise<string>}>(
@@ -259,8 +230,7 @@ export const getTransactionUnspentOutput = async ({
259230
bytes: Uint8Array
260231
index: number
261232
}) => {
262-
const {csl, release} = wrappedCsl()
263-
233+
const {csl, release} = getCSL()
264234
try {
265235
const tx = await csl.Transaction.fromBytes(bytes)
266236
const body = await tx.body()
@@ -271,7 +241,10 @@ export const getTransactionUnspentOutput = async ({
271241
const value = await originalOutput.amount()
272242
const receiver = await originalOutput.address()
273243
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+
)
275248
} finally {
276249
release()
277250
}

0 commit comments

Comments
 (0)