-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add EIP-712 management in prepareMessageToSign (#892)
* feat: Add EIP-712 management in prepareMessageToSign Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * fix: remove unused import Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * fix: resolve import issue Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * fix: change expected result of prepareMessage in case of thrown error Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * feat: rollback on TypedMessageData properties Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * fix: test with EIP712Message Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * feat: add tests Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * fix: move fixtures folder Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * feat: update changelog Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * chore: change test object name Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * fix: Update preparedMessageType in mobile Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * fix: revert unintended modification Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * chore: rename createCryptoCurrency into createFixtureCryptoCurrency to avoid confusion Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * chore: extract some code to test it Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * fix: eslint issue Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * feat: review feedback Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * fix: review feedback Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * fix: rebase issue with mobile platform player Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * chore: revert mobile package modification Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * fix: unit test Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> * feat: simplify a little bit eth signMessage * fix: PR feedback Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr> Signed-off-by: Stéphane Prohaszka <stephane.prohaszka@ledger.fr>
- Loading branch information
1 parent
d9800ac
commit d70bb70
Showing
19 changed files
with
505 additions
and
141 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 @@ | ||
--- | ||
"@ledgerhq/live-common": minor | ||
"@ledgerhq/hw-app-eth": patch | ||
--- | ||
|
||
Add EIP-712 capability when preparing message to sign |
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
177 changes: 177 additions & 0 deletions
177
apps/ledger-live-mobile/src/components/WebPlatformPlayer/liveSDKLogic.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,177 @@ | ||
import BigNumber from "bignumber.js"; | ||
import { CryptoCurrency, TokenCurrency } from "@ledgerhq/types-cryptoassets"; | ||
import { Account, TokenAccount } from "@ledgerhq/types-live"; | ||
import { PlatformTransaction } from "@ledgerhq/live-common/platform/types"; | ||
import { Transaction } from "@ledgerhq/live-common/generated/types"; | ||
import prepareSignTransaction from "./liveSDKLogic"; | ||
|
||
// Fake the support of the test currency | ||
jest.mock("@ledgerhq/live-common/currencies/support", () => ({ | ||
isCurrencySupported: () => true, | ||
})); | ||
|
||
describe("prepareSignTransaction", () => { | ||
it("returns a Transaction", () => { | ||
// Given | ||
const parentAccount = createAccount("12"); | ||
const childAccount = createTokenAccount( | ||
"22", | ||
"ethereumjs:2:ethereum:0x012:", | ||
); | ||
const expectedResult = { | ||
amount: new BigNumber("1000"), | ||
data: Buffer.from([]), | ||
estimatedGasLimit: null, | ||
family: "ethereum", | ||
feeCustomUnit: { code: "Gwei", magnitude: 9, name: "Gwei" }, | ||
feesStrategy: "medium", | ||
gasPrice: new BigNumber("700000"), | ||
gasLimit: new BigNumber("1200000"), | ||
userGasLimit: new BigNumber("1200000"), | ||
mode: "send", | ||
networkInfo: null, | ||
nonce: 8, | ||
recipient: "0x0123456", | ||
subAccountId: "ethereumjs:2:ethereum:0x022:", | ||
useAllAmount: false, | ||
}; | ||
|
||
// When | ||
const result = prepareSignTransaction( | ||
childAccount, | ||
parentAccount, | ||
createEtherumTransaction() as Partial<Transaction>, | ||
); | ||
|
||
// Then | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
// *** UTIL FUNCTIONS *** | ||
function createEtherumTransaction(): PlatformTransaction { | ||
return { | ||
family: "ethereum" as any, | ||
amount: new BigNumber("1000"), | ||
recipient: "0x0123456", | ||
nonce: 8, | ||
data: Buffer.from("Some data...", "hex"), | ||
gasPrice: new BigNumber("700000"), | ||
gasLimit: new BigNumber("1200000"), | ||
}; | ||
} | ||
|
||
const createCryptoCurrency = (family: string): CryptoCurrency => ({ | ||
type: "CryptoCurrency", | ||
id: "testCoinId", | ||
coinType: 8008, | ||
name: "MyCoin", | ||
managerAppName: "MyCoin", | ||
ticker: "MYC", | ||
countervalueTicker: "MYC", | ||
scheme: "mycoin", | ||
color: "#ff0000", | ||
family, | ||
units: [ | ||
{ | ||
name: "MYC", | ||
code: "MYC", | ||
magnitude: 8, | ||
}, | ||
{ | ||
name: "SmallestUnit", | ||
code: "SMALLESTUNIT", | ||
magnitude: 0, | ||
}, | ||
], | ||
explorerViews: [ | ||
{ | ||
address: "https://mycoinexplorer.com/account/$address", | ||
tx: "https://mycoinexplorer.com/transaction/$hash", | ||
token: "https://mycoinexplorer.com/token/$contractAddress/?a=$address", | ||
}, | ||
], | ||
}); | ||
|
||
const defaultEthCryptoFamily = createCryptoCurrency("ethereum"); | ||
const createAccount = ( | ||
id: string, | ||
crypto: CryptoCurrency = defaultEthCryptoFamily, | ||
): Account => ({ | ||
type: "Account", | ||
id: `ethereumjs:2:ethereum:0x0${id}:`, | ||
seedIdentifier: "0x01", | ||
derivationMode: "ethM", | ||
index: 0, | ||
freshAddress: "0x01", | ||
freshAddressPath: "44'/60'/0'/0/0", | ||
freshAddresses: [], | ||
name: "Ethereum 1", | ||
starred: false, | ||
used: false, | ||
balance: new BigNumber("51281813126095913"), | ||
spendableBalance: new BigNumber("51281813126095913"), | ||
creationDate: new Date(), | ||
blockHeight: 8168983, | ||
currency: crypto, | ||
unit: { | ||
name: "satoshi", | ||
code: "BTC", | ||
magnitude: 5, | ||
}, | ||
operationsCount: 0, | ||
operations: [], | ||
pendingOperations: [], | ||
lastSyncDate: new Date(), | ||
balanceHistoryCache: { | ||
HOUR: { | ||
balances: [], | ||
latestDate: undefined, | ||
}, | ||
DAY: { | ||
balances: [], | ||
latestDate: undefined, | ||
}, | ||
WEEK: { | ||
balances: [], | ||
latestDate: undefined, | ||
}, | ||
}, | ||
swapHistory: [], | ||
}); | ||
|
||
function createTokenAccount(id = "32", parentId = "whatever"): TokenAccount { | ||
return { | ||
type: "TokenAccount", | ||
id: `ethereumjs:2:ethereum:0x0${id}:`, | ||
parentId, | ||
token: createTokenCurrency(), | ||
balance: new BigNumber(0), | ||
spendableBalance: new BigNumber(0), | ||
creationDate: new Date(), | ||
operationsCount: 0, | ||
operations: [], | ||
pendingOperations: [], | ||
starred: false, | ||
balanceHistoryCache: { | ||
WEEK: { latestDate: null, balances: [] }, | ||
HOUR: { latestDate: null, balances: [] }, | ||
DAY: { latestDate: null, balances: [] }, | ||
}, | ||
swapHistory: [], | ||
}; | ||
} | ||
|
||
function createTokenCurrency(): TokenCurrency { | ||
return { | ||
type: "TokenCurrency", | ||
id: "3", | ||
contractAddress: "", | ||
parentCurrency: defaultEthCryptoFamily, | ||
tokenType: "", | ||
// -- CurrencyCommon | ||
name: "", | ||
ticker: "", | ||
units: [], | ||
}; | ||
} |
23 changes: 23 additions & 0 deletions
23
apps/ledger-live-mobile/src/components/WebPlatformPlayer/liveSDKLogic.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,23 @@ | ||
import { isTokenAccount } from "@ledgerhq/live-common/account/index"; | ||
import { getAccountBridge } from "@ledgerhq/live-common/bridge/index"; | ||
import { Transaction } from "@ledgerhq/live-common/lib/generated/types"; | ||
import { Account, AccountLike, TransactionCommon } from "@ledgerhq/types-live"; | ||
|
||
export default function prepareSignTransaction( | ||
account: AccountLike, | ||
parentAccount: Account | null, | ||
liveTx: Partial<Transaction>, | ||
): TransactionCommon { | ||
const bridge = getAccountBridge(account, parentAccount); | ||
const t = bridge.createTransaction(account); | ||
const { recipient, ...txData } = liveTx; | ||
const t2 = bridge.updateTransaction(t, { | ||
recipient, | ||
subAccountId: isTokenAccount(account) ? account.id : undefined, | ||
}); | ||
|
||
return bridge.updateTransaction(t2, { | ||
userGasLimit: txData.gasLimit, | ||
...txData, | ||
}); | ||
} |
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.