Skip to content

Commit

Permalink
Add submitTx
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed May 3, 2024
1 parent 1d42e1f commit 51516ec
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions apps/wallet-mobile/src/features/Discover/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const createDappConnector = (appStorage: App.Storage, wallet: YoroiWallet
getBalance: (tokenId) => wallet.getBalance(tokenId),
getChangeAddress: () => wallet.CIP30getChangeAddress(),
getRewardAddresses: () => wallet.CIP30getRewardAddresses(),
submitTx: async (cbor) => wallet.CIP30submitTx(cbor),
getCollateral: async (value) => {
// TODO: Move serialisation out of here
const result = await wallet.CIP30getCollateral(value)
Expand Down
2 changes: 1 addition & 1 deletion apps/wallet-mobile/src/yoroi-wallets/cardano/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const filterUsedAddresses = async (addresses: Addresses, config: BackendC
return copy.filter((addr) => used.includes(addr))
}

export const submitTransaction = async (signedTx: string, config: BackendConfig) => {
export const submitTransaction = async (signedTx: string, config: BackendConfig): Promise<void> => {
try {
await fetchDefault('txs/signed', {signedTx}, config)
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,6 @@ export const makeShelleyWallet = (constants: typeof MAINNET | typeof TESTNET | t
}

async CIP30getCollateral(value?: string) {
console.log('CIP30getCollateral', value)
const valueStr = value?.trim() ?? collateralConfig.minLovelace.toString()
const valueNum = new BigNumber(valueStr)

Expand All @@ -1020,28 +1019,32 @@ export const makeShelleyWallet = (constants: typeof MAINNET | typeof TESTNET | t
// if has collateral and requested collateral is lower or equal to current collateral
// return current collateral
if (currentCollateral.utxo && valueNum.lte(currentCollateral.utxo.amount)) {
console.log('CIP30getCollateral: return current collateral')
const utxo = await cardanoUtxoFromRemoteFormat(rawUtxoToRemoteUnspentOutput(currentCollateral.utxo))
return [utxo]
}

// if can draw collateral in one utxo, use the utxo as a collateral
const oneUtxoCollateral = await this._drawCollateralInOneUtxo(asQuantity(valueNum))
if (oneUtxoCollateral) {
console.log('CIP30getCollateral: draw collateral in one utxo')
return [oneUtxoCollateral]
}

// if can draw collateral in multiple utxos, use all required utxos
const multipleUtxosCollateral = await this._drawCollateralInMultipleUtxos(asQuantity(valueNum))
if (multipleUtxosCollateral && multipleUtxosCollateral.length > 0) {
console.log('CIP30getCollateral: draw collateral in multiple utxos')
return multipleUtxosCollateral
}

return null
}

async CIP30submitTx(cbor: string) {
const base64 = Buffer.from(cbor, 'hex').toString('base64')
const txId = await Cardano.calculateTxId(base64, 'base64')
await this.submitTransaction(base64)
return txId
}

async signSwapCancellationWithLedger(cbor: string, useUSB: boolean): Promise<void> {
if (!this.hwDeviceInfo) throw new Error('Invalid wallet state')

Expand Down Expand Up @@ -1134,9 +1137,8 @@ export const makeShelleyWallet = (constants: typeof MAINNET | typeof TESTNET | t
return this.cardanoApi.getProtocolParams()
}

async submitTransaction(signedTx: string) {
const response: any = await legacyApi.submitTransaction(signedTx, BACKEND)
return response as any
async submitTransaction(base64SignedTx: string) {
await legacyApi.submitTransaction(base64SignedTx, BACKEND)
}

private async syncUtxos() {
Expand Down
3 changes: 2 additions & 1 deletion apps/wallet-mobile/src/yoroi-wallets/cardano/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export type YoroiWallet = {
createUnsignedTx(entries: YoroiEntry[], metadata?: Array<CardanoTypes.TxMetadata>): Promise<YoroiUnsignedTx>
signTxWithLedger(request: YoroiUnsignedTx, useUSB: boolean): Promise<YoroiSignedTx>
signTx(signRequest: YoroiUnsignedTx, rootKey: string): Promise<YoroiSignedTx>
submitTransaction(signedTx: string): Promise<[]>
submitTransaction(signedTx: string): Promise<void>

// Voting
createVotingRegTx(
Expand Down Expand Up @@ -199,6 +199,7 @@ export type YoroiWallet = {
CIP30getRewardAddresses(): Promise<string[]>
CIP30getUtxos(value?: string, paginate?: {page: number; limit: number}): Promise<TransactionUnspentOutput[] | null>
CIP30getCollateral(value?: string): Promise<TransactionUnspentOutput[] | null>
CIP30submitTx(cbor: string): Promise<string>
}

export const isYoroiWallet = (wallet: unknown): wallet is YoroiWallet => {
Expand Down
1 change: 1 addition & 0 deletions packages/dapp-connector/src/dapp-connector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,6 @@ const mockWallet: ResolverWallet = {
getRewardAddresses: () => Promise.resolve(['e184d958399bcce03402fd853d43a4e7366f2018932e5aff4eea904693']),
getUtxos: () => Promise.resolve([]),
getCollateral: () => Promise.resolve([]),
submitTx: () => Promise.resolve(''),
}
const trustedUrl = 'https://yoroi-wallet.com/'
15 changes: 15 additions & 0 deletions packages/dapp-connector/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Resolver = {
getUnusedAddresses: ResolvableMethod<string[]>
getUtxos: ResolvableMethod<string[]>
getCollateral: ResolvableMethod<string[] | null>
submitTx: ResolvableMethod<string>
}
}

Expand All @@ -49,6 +50,19 @@ export const resolver: Resolver = {
return hasWalletAcceptedConnection(context)
},
api: {
submitTx: async (params: unknown, context: Context) => {
assertOriginsMatch(context)
await assertWalletAcceptedConnection(context)
if (
!isRecord(params) ||
!isKeyOf('args', params) ||
!Array.isArray(params.args) ||
typeof params.args[0] !== 'string'
) {
throw new Error('Invalid params')
}
return context.wallet.submitTx(params.args[0])
},
getCollateral: async (params: unknown, context: Context) => {
// offer to reorganise transactions if possible
// check if collateral is less than or equal to 5 ADA
Expand Down Expand Up @@ -217,6 +231,7 @@ export type ResolverWallet = {
getRewardAddresses: () => Promise<string[]>
getUtxos: (value?: string, pagination?: Pagination) => Promise<string[]>
getCollateral: (value?: string) => Promise<string[] | null>
submitTx: (cbor: string) => Promise<string>
}

type Pagination = {
Expand Down

0 comments on commit 51516ec

Please sign in to comment.