Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed May 25, 2024
1 parent e05bbc7 commit a402226
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
26 changes: 14 additions & 12 deletions apps/wallet-mobile/src/yoroi-wallets/cardano/cip30.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ export const cip30ExtensionMaker = (wallet: YoroiWallet) => {

const getCSL = () => wrappedCsl()

const recreateValue = async (value: CSL.Value) => {
return CardanoMobile.Value.fromHex(await value.toHex())
const copy = async <T extends {toHex: () => Promise<string>}>(
creator: {fromHex: (hex: string) => Promise<T>},
value: T,
): Promise<T> => {
return creator.fromHex(await value.toHex())
}

const recreateMultiple = async <T>(items: T[], recreate: (item: T) => Promise<T>) => {
return Promise.all(items.map(recreate))
const copyMultiple = async <T extends {toHex: () => Promise<string>}>(
items: T[],
creator: {fromHex: (hex: string) => Promise<T>},
) => {
return Promise.all(items.map((item) => copy(creator, item)))
}

const recreateTransactionUnspentOutput = async (utxo: CSL.TransactionUnspentOutput) => {
return CardanoMobile.TransactionUnspentOutput.fromHex(await utxo.toHex())
}

const recreateWitnessSet = async (witnessSet: CSL.TransactionWitnessSet) => {
return CardanoMobile.TransactionWitnessSet.fromHex(await witnessSet.toHex())
return copy(CardanoMobile.TransactionUnspentOutput, utxo)
}

class CIP30Extension {
Expand All @@ -48,7 +50,7 @@ class CIP30Extension {
const {csl, release} = getCSL()
try {
const value = await _getBalance(csl, tokenId, this.wallet.utxos, this.wallet.primaryTokenInfo.id)
return recreateValue(value)
return copy(CardanoMobile.Value, value)
} finally {
release()
}
Expand Down Expand Up @@ -111,7 +113,7 @@ class CIP30Extension {

const multipleUtxosCollateral = await _drawCollateralInMultipleUtxos(csl, this.wallet, asQuantity(valueNum))
if (multipleUtxosCollateral && multipleUtxosCollateral.length > 0) {
return recreateMultiple(multipleUtxosCollateral, recreateTransactionUnspentOutput)
return copyMultiple(multipleUtxosCollateral, CardanoMobile.TransactionUnspentOutput)
}

return null
Expand Down Expand Up @@ -146,7 +148,7 @@ class CIP30Extension {
const keys = await Promise.all(signers.map(async (signer) => createRawTxSigningKey(rootKey, signer)))
const signedTxBytes = await signRawTransaction(csl, cbor, keys)
const signedTx = await csl.Transaction.fromBytes(signedTxBytes)
return recreateWitnessSet(await signedTx.witnessSet())
return copy(CardanoMobile.TransactionWitnessSet, await signedTx.witnessSet())
} finally {
release()
}
Expand Down
56 changes: 35 additions & 21 deletions packages/dapp-connector/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,20 @@ export const resolver: Resolver = {
: undefined
const result = await context.wallet.getCollateral(value)

if (result === null || (result.length === 0 && typeof value === 'string')) return null
if (result === null) {
// offer reorganisation
const balance = await context.wallet.getBalance('*')
const coin = await balance.coin()
// check min collateral value
if ((await coin.toStr()) !== '0') {
const utxos = await context.wallet.getUtxos(value ?? '1000000')
if (utxos === null || utxos.length === 0) {
return null
}
}

return null
}

return Promise.all(result.map((u) => u.toHex()))
},
Expand Down Expand Up @@ -212,27 +225,28 @@ const handleMethod = async (
supportedExtensions: trustedContext.supportedExtensions,
}

if (method === 'cardano_enable') {
return resolver.enable(params, context)
}

if (method === 'cardano_is_enabled') {
return resolver.isEnabled(params, context)
}

if (method === LOG_MESSAGE_EVENT) {
return resolver.logMessage(params, context)
}

if (method.startsWith('api.')) {
const methodParts = method.split('.')
if (methodParts.length !== 2) throw new Error(`Invalid method ${method}`)
const apiMethod = methodParts[1]
if (!isKeyOf(apiMethod, resolver.api)) throw new Error(`Unknown method ${method}`)
return resolver.api[apiMethod](params, context)
}
if (!method) throw new Error('Method is required')
const isValidMethod = isKeyOf(method, methods)
if (!isValidMethod) throw new Error(`Unknown method '${method}'`)
return methods[method](params, context)
}

throw new Error(`Unknown method '${method}' with params ${JSON.stringify(params)}`)
const methods = {
'cardano_enable': resolver.enable,
'cardano_is_enabled': resolver.isEnabled,
'log_message': resolver.logMessage,
'api.getBalance': resolver.api.getBalance,
'api.getChangeAddress': resolver.api.getChangeAddress,
'api.getNetworkId': resolver.api.getNetworkId,
'api.getRewardAddresses': resolver.api.getRewardAddresses,
'api.getUsedAddresses': resolver.api.getUsedAddresses,
'api.getExtensions': resolver.api.getExtensions,
'api.getUnusedAddresses': resolver.api.getUnusedAddresses,
'api.getUtxos': resolver.api.getUtxos,
'api.getCollateral': resolver.api.getCollateral,
'api.submitTx': resolver.api.submitTx,
'api.signTx': resolver.api.signTx,
'api.signData': resolver.api.signData,
}

export const resolverHandleEvent = async (
Expand Down

0 comments on commit a402226

Please sign in to comment.