-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LedgerDappProvider #43
Open
arhtudormorar
wants to merge
8
commits into
development
Choose a base branch
from
tm/feature/sign-screens
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9202888
added transaction manager
DanutIlie 2226d44
update changelog
DanutIlie 248099a
updates after review
DanutIlie 5cf7855
Refactor Ledger Provider
arhtudormorar c3237f1
Remove logs
arhtudormorar 8d1210d
Fixed ledger reload
arhtudormorar 9280858
DappProvider types
arhtudormorar 78124ec
Minor fixes
arhtudormorar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { Transaction } from '@multiversx/sdk-core/out'; | ||
import axios, { AxiosError } from 'axios'; | ||
import { BATCH_TRANSACTIONS_ID_SEPARATOR } from 'constants/transactions.constants'; | ||
import { getAccount } from 'core/methods/account/getAccount'; | ||
import { networkSelector } from 'store/selectors'; | ||
import { getState } from 'store/store'; | ||
import { GuardianActionsEnum } from 'types'; | ||
import { BatchTransactionsResponseType } from 'types/serverTransactions.types'; | ||
import { SignedTransactionType } from 'types/transactions.types'; | ||
|
||
export class TransactionManager { | ||
public static send = async ( | ||
signedTransactions: Transaction[] | Transaction[][] | ||
): Promise<string[]> => { | ||
if (signedTransactions.length === 0) { | ||
throw new Error('No transactions to send'); | ||
} | ||
|
||
try { | ||
const isBatchTransaction = | ||
TransactionManager.isBatchTransaction(signedTransactions); | ||
|
||
if (!isBatchTransaction) { | ||
const hashes = await this.sendSignedTransactions(signedTransactions); | ||
return hashes; | ||
} | ||
|
||
const sentTransactions = | ||
await this.sendSignedBatchTransactions(signedTransactions); | ||
|
||
if (!sentTransactions.data || sentTransactions.data.error) { | ||
throw new Error( | ||
sentTransactions.data?.error || 'Failed to send transactions' | ||
); | ||
} | ||
|
||
const flatSentTransactions = this.sequentialToFlatArray( | ||
sentTransactions.data.transactions | ||
); | ||
|
||
return flatSentTransactions.map((transaction) => transaction.hash); | ||
} catch (error) { | ||
const responseData = <{ message: string }>( | ||
(error as AxiosError).response?.data | ||
); | ||
throw responseData?.message ?? (error as any).message; | ||
} | ||
}; | ||
|
||
private static sendSignedTransactions = async ( | ||
signedTransactions: Transaction[] | ||
): Promise<string[]> => { | ||
const { apiAddress, apiTimeout } = networkSelector(getState()); | ||
|
||
const promises = signedTransactions.map((transaction) => | ||
axios.post(`${apiAddress}/transactions`, transaction.toPlainObject(), { | ||
timeout: Number(apiTimeout) | ||
}) | ||
); | ||
|
||
const response = await Promise.all(promises); | ||
|
||
return response.map(({ data }) => data.txHash); | ||
}; | ||
|
||
private static sendSignedBatchTransactions = async ( | ||
signedTransactions: Transaction[][] | ||
) => { | ||
const { address } = getAccount(); | ||
const { apiAddress, apiTimeout } = networkSelector(getState()); | ||
|
||
if (!address) { | ||
return { | ||
error: | ||
'Invalid address provided. You need to be logged in to send transactions' | ||
}; | ||
} | ||
|
||
const batchId = this.buildBatchId(address); | ||
const parsedTransactions = signedTransactions.map((transactions) => | ||
transactions.map((transaction) => | ||
this.parseSignedTransaction(transaction) | ||
) | ||
); | ||
|
||
const payload = { | ||
transactions: parsedTransactions, | ||
id: batchId | ||
}; | ||
|
||
const { data } = await axios.post<BatchTransactionsResponseType>( | ||
`${apiAddress}/batch`, | ||
payload, | ||
{ | ||
timeout: Number(apiTimeout) | ||
} | ||
); | ||
|
||
return { data }; | ||
}; | ||
|
||
private static buildBatchId = (address: string) => { | ||
const sessionId = Date.now().toString(); | ||
return `${sessionId}${BATCH_TRANSACTIONS_ID_SEPARATOR}${address}`; | ||
}; | ||
|
||
private static sequentialToFlatArray = ( | ||
transactions: SignedTransactionType[] | SignedTransactionType[][] = [] | ||
) => | ||
this.getIsSequential(transactions) | ||
? transactions.flat() | ||
: (transactions as SignedTransactionType[]); | ||
|
||
private static getIsSequential = ( | ||
transactions?: SignedTransactionType[] | SignedTransactionType[][] | ||
) => transactions?.every((transaction) => Array.isArray(transaction)); | ||
|
||
private static isBatchTransaction = ( | ||
transactions: Transaction[] | Transaction[][] | ||
): transactions is Transaction[][] => { | ||
return Array.isArray(transactions[0]); | ||
}; | ||
|
||
private static parseSignedTransaction = (signedTransaction: Transaction) => { | ||
const parsedTransaction = { | ||
...signedTransaction.toPlainObject(), | ||
hash: signedTransaction.getHash().hex() | ||
}; | ||
|
||
// TODO: Remove when the protocol supports usernames for guardian transactions | ||
if (this.isGuardianTx(parsedTransaction.data)) { | ||
delete parsedTransaction.senderUsername; | ||
delete parsedTransaction.receiverUsername; | ||
} | ||
|
||
return parsedTransaction; | ||
}; | ||
|
||
private static isGuardianTx = (transactionData?: string) => | ||
transactionData && | ||
transactionData.startsWith(GuardianActionsEnum.SetGuardian); | ||
} |
37 changes: 0 additions & 37 deletions
37
src/core/methods/sendTransactions/helpers/sendSignedTransactions.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/core/providers/DappProvider/helpers/signTransactions/helpers/getSignedTransactions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Transaction } from '@multiversx/sdk-core/out'; | ||
import { IProvider } from 'core/providers/types/providerFactory.types'; | ||
|
||
interface ISignWithUIProps { | ||
transactions: Transaction[]; | ||
provider: IProvider; | ||
} | ||
|
||
export const getSignedTransactions = async ({ | ||
transactions, | ||
provider | ||
}: ISignWithUIProps): Promise<Transaction[]> => { | ||
if (!provider.mountSignUI) { | ||
const signedTransactions = await provider.signTransactions(transactions); | ||
return signedTransactions ?? []; | ||
} | ||
|
||
provider.mountSignUI(); | ||
|
||
return transactions; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still need ?