Skip to content

Commit

Permalink
Merge pull request #210 from alephium/update-ledger-sdk-to-0.5
Browse files Browse the repository at this point in the history
Update ledger sdk to 0.5.2
  • Loading branch information
polarker authored Jul 26, 2024
2 parents e0b1a65 + 68ab490 commit 9a42b5a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"devDependencies": {
"@alephium/get-extension-wallet": "^1.2.2",
"@alephium/ledger-app": "0.4.0",
"@alephium/ledger-app": "0.5.2",
"@alephium/token-list": "0.0.19",
"@alephium/web3": "^1.2.2",
"@alephium/web3-test": "^1.2.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import LedgerApp from "@alephium/ledger-app"
import { AlephiumApp as LedgerApp } from "@alephium/ledger-app"
import { TokenMetadata, MAX_TOKEN_SIZE, MAX_TOKEN_SYMBOL_LENGTH } from "@alephium/ledger-app"
import { ALPH_TOKEN_ID, ONE_ALPH, prettifyTokenAmount, TransactionBuilder } from "@alephium/web3"
import { getHDWalletPath } from "@alephium/web3-wallet"
import { L1, icons } from "@argent/ui"
Expand All @@ -10,13 +11,14 @@ import {
ReviewTransactionResult,
TransactionParams,
} from "../../../shared/actionQueue/types"
import { BaseTokenWithBalance } from "../../../shared/token/type"
import { BaseTokenWithBalance, Token } from "../../../shared/token/type"
import { isEqualTokenId } from "../../services/token"
import { useAppState } from "../../app.state"
import { routes } from "../../routes"
import { usePageTracking } from "../../services/analytics"
import { rejectAction } from "../../services/backgroundActions"
import { Account } from "../accounts/Account"
import { useAllTokensWithBalance } from "../accountTokens/tokens.state"
import { useAllTokensWithBalance, useFungibleTokensWithBalance } from "../accountTokens/tokens.state"
import { getLedgerApp } from "../ledger/utils"
import { useNetwork } from "../networks/useNetworks"
import { ConfirmScreen } from "./ConfirmScreen"
Expand Down Expand Up @@ -209,6 +211,7 @@ export const ApproveTransactionScreen: FC<ApproveTransactionScreenProps> = ({
selectedAccount?.networkId ?? "unknown",
)

const { tokenDetails: fungibleTokens } = useFungibleTokensWithBalance(selectedAccount)
const useLedger =
selectedAccount !== undefined && selectedAccount.signer.type === "ledger"
const [ledgerState, setLedgerState] = useState<
Expand Down Expand Up @@ -261,7 +264,8 @@ export const ApproveTransactionScreen: FC<ApproveTransactionScreenProps> = ({
selectedAccount.signer.keyType,
selectedAccount.signer.derivationIndex,
)
const signature = await app.signUnsignedTx(path, Buffer.from(buildResult.result.unsignedTx, "hex"))
const unsignedTx = Buffer.from(buildResult.result.unsignedTx, "hex")
const signature = await ledgerSignTx(app, path, unsignedTx, fungibleTokens, transaction)
setLedgerState("succeeded")
onSubmit({ ...buildResult, signature })
await app.close()
Expand Down Expand Up @@ -355,3 +359,40 @@ export const ApproveTransactionScreen: FC<ApproveTransactionScreenProps> = ({
</ConfirmScreen>
)
}

async function ledgerSignTx(
ledgerApp: LedgerApp,
path: string,
unsignedTx: Buffer,
fungibleTokens: Token[],
params: TransactionParams
) {
if (params.type !== 'TRANSFER') {
return ledgerApp.signUnsignedTx(path, unsignedTx)
}
const tokens: TokenMetadata[] = []
const allTokenIds = params
.params
.destinations
.map((d) => d.tokens?.map((t) => t.id) ?? [])
.flat()
for (const tokenId of allTokenIds) {
if (tokens.length === MAX_TOKEN_SIZE) {
break
}
if (tokens.find((t) => isEqualTokenId(t.tokenId, tokenId)) !== undefined) {
continue
}
const tokenInfo = fungibleTokens.find((t) => isEqualTokenId(t.id, tokenId))
if (tokenInfo === undefined || !tokenInfo.verified || tokenInfo.symbol.length > MAX_TOKEN_SYMBOL_LENGTH) {
continue
}
tokens.push({
version: 0,
tokenId: tokenInfo.id,
symbol: tokenInfo.symbol,
decimals: tokenInfo.decimals
})
}
return ledgerApp.signUnsignedTx(path, unsignedTx, tokens)
}
2 changes: 1 addition & 1 deletion packages/extension/src/ui/features/ledger/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LedgerApp from '@alephium/ledger-app'
import { AlephiumApp as LedgerApp } from '@alephium/ledger-app'
import Transport from "@ledgerhq/hw-transport"
import TransportWebHID from "@ledgerhq/hw-transport-webhid";
import TransportWebUSB from "@ledgerhq/hw-transport-webusb";
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"@alephium/web3" "^1.2.2"
bowser "^2.11.0"

"@alephium/ledger-app@0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@alephium/ledger-app/-/ledger-app-0.4.0.tgz#9f2ee38063a69390695c03b03c61ac3f5e4c6f29"
integrity sha512-nLO9B7Y6RuF8O3RhAHoqUdK4DqyP8W7zGW/lTm57eWeeJ+fbcVo5RuwRx+1jVPaXfiUwqBwZJZMiU0z1DBCfhQ==
"@alephium/ledger-app@0.5.2":
version "0.5.2"
resolved "https://registry.yarnpkg.com/@alephium/ledger-app/-/ledger-app-0.5.2.tgz#2451c11af2974e9681b7b7967fe523c3f78045cd"
integrity sha512-1PEi9z6D4pq7cbfqUmDRiL2UooFDUVdX2v9STMAgcI9a5nJfGfx51rD1hYdLvElPGcj/yeafud4CUgr5cCvmsw==
dependencies:
"@alephium/web3" "^1.2.0"
"@ledgerhq/hw-transport" "6.31.0"
Expand Down

0 comments on commit 9a42b5a

Please sign in to comment.