-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(llm): ✂️ truncate some coins memo on the recipient step of the se…
…nd flow (#8340) * fix(llm): fix the "Memo is too long" issue of Algorand * fix(llm): truncate Solana's memo too * fix(llm): truncate ICP's memo * chore: update change log * chore(llm): remove extra changes on Solana * chore: update the LL-common non imported file
- Loading branch information
Showing
8 changed files
with
49 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"live-mobile": minor | ||
"@ledgerhq/live-common": patch | ||
--- | ||
|
||
Truncate some coins memo on the recipient step of the send flow |
2 changes: 2 additions & 0 deletions
2
apps/ledger-live-mobile/src/families/algorand/MemoTagInput.tsx
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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import React from "react"; | ||
|
||
import { ALGORAND_MAX_MEMO_SIZE } from "@ledgerhq/live-common/families/algorand/logic"; | ||
import type { Transaction as AlgorandTransaction } from "@ledgerhq/live-common/families/algorand/types"; | ||
import type { MemoTagInputProps } from "LLM/features/MemoTag/types"; | ||
import { GenericMemoTagInput } from "LLM/features/MemoTag/components/GenericMemoTagInput"; | ||
|
||
export default (props: MemoTagInputProps<AlgorandTransaction>) => ( | ||
<GenericMemoTagInput | ||
{...props} | ||
textToValue={text => text.slice(0, ALGORAND_MAX_MEMO_SIZE)} | ||
valueToTxPatch={value => tx => ({ ...tx, memo: value || undefined })} | ||
/> | ||
); |
8 changes: 6 additions & 2 deletions
8
apps/ledger-live-mobile/src/families/internet_computer/MemoTagInput.tsx
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 |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import React from "react"; | ||
|
||
import { Transaction as ICPTransaction } from "@ledgerhq/live-common/families/internet_computer/types"; | ||
import { MAX_MEMO_VALUE } from "@ledgerhq/live-common/families/internet_computer/consts"; | ||
import type { Transaction as ICPTransaction } from "@ledgerhq/live-common/families/internet_computer/types"; | ||
import type { MemoTagInputProps } from "LLM/features/MemoTag/types"; | ||
import { GenericMemoTagInput } from "LLM/features/MemoTag/components/GenericMemoTagInput"; | ||
|
||
export default (props: MemoTagInputProps<ICPTransaction>) => ( | ||
<GenericMemoTagInput | ||
{...props} | ||
textToValue={text => text.replace(/\D/g, "")} | ||
textToValue={text => { | ||
const value = Math.min(MAX_MEMO_VALUE, Number(text.replace(/\D/g, ""))); | ||
return value ? String(value) : ""; | ||
}} | ||
valueToTxPatch={value => tx => ({ ...tx, memo: value || undefined })} | ||
/> | ||
); |
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
12 changes: 12 additions & 0 deletions
12
apps/ledger-live-mobile/src/newArch/utils/__tests__/truncateUtf8.test.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,12 @@ | ||
import { truncateUtf8 } from "../truncateUtf8"; | ||
|
||
test("truncateUtf8", () => { | ||
expect(truncateUtf8("hello world", 0)).toBe(""); | ||
expect(truncateUtf8("hello world", 50)).toBe("hello world"); | ||
expect(truncateUtf8("hello world", 1)).toBe("h"); | ||
expect(truncateUtf8("hello world", 5)).toBe("hello"); | ||
expect(truncateUtf8("👋💬🌎", 4)).toBe("👋"); | ||
expect(truncateUtf8("👋💬🌎", 3)).toBe(""); | ||
expect(truncateUtf8("👋💬🌎", 12)).toBe("👋💬🌎"); | ||
expect(truncateUtf8("👋💬🌎", 11)).toBe("👋💬"); | ||
}); |
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,16 @@ | ||
export function truncateUtf8(str: string, maxBytes: number) { | ||
const totalBytesLength = Buffer.from(str).byteLength; | ||
if (totalBytesLength <= maxBytes) return str; | ||
if (totalBytesLength === str.length) return str.slice(0, maxBytes); | ||
|
||
// NOTE in js strings characters above U+FFFF (i.e outside the Basic Multilingual Plane) are represented by two UTF-16 code units | ||
// the spread operator will split these strings correctly with some "characters" being strings of length 2. | ||
let byteLen = 0; | ||
let charLen = 0; | ||
for (const char of [...str]) { | ||
byteLen += Buffer.from(char).byteLength; | ||
if (byteLen > maxBytes) return str.slice(0, charLen); | ||
charLen += char.length; | ||
} | ||
return str; | ||
} |
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,2 @@ | ||
// Encapsulate for LLD et LLM | ||
export * from "@ledgerhq/coin-algorand/logic"; |