-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1424015
commit 084aae2
Showing
14 changed files
with
848 additions
and
417 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 |
---|---|---|
|
@@ -108,5 +108,8 @@ | |
}, | ||
"resolutions": { | ||
"react-refresh": "0.9.0" | ||
}, | ||
"@parcel/resolver-default": { | ||
"packageExports": true | ||
} | ||
} |
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
Empty file.
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,33 @@ | ||
{ | ||
"name": "@rango-dev/provider-ledger", | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
"type": "module", | ||
"source": "./src/index.ts", | ||
"main": "./dist/index.js", | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"scripts": { | ||
"build": "node ../../scripts/build/command.mjs --path wallets/provider-ledger", | ||
"ts-check": "tsc --declaration --emitDeclarationOnly -p ./tsconfig.json", | ||
"clean": "rimraf dist", | ||
"format": "prettier --write '{.,src}/**/*.{ts,tsx}'", | ||
"lint": "eslint \"**/*.{ts,tsx}\" --ignore-path ../../.eslintignore" | ||
}, | ||
"dependencies": { | ||
"@ledgerhq/hw-app-eth": "^6.35.7", | ||
"@ledgerhq/hw-transport-webhid": "^6.28.5", | ||
"@rango-dev/wallets-shared": "^0.31.0", | ||
"ethers": "^6.11.1", | ||
"rango-types": "^0.1.59" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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 @@ | ||
# @rango-dev/provider-ledger |
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,71 @@ | ||
import type Transport from '@ledgerhq/hw-transport'; | ||
|
||
const ETHEREUM_CHAIN_ID = '0x1'; | ||
|
||
export const ETH_BIP32_PATH = "44'/60'/0'/0/0"; | ||
|
||
const ledgerErrorMessages: { [statusCode: number | string]: string } = { | ||
21781: 'The device is locked', | ||
25871: 'Related application is not ready on your device', | ||
27013: 'Action denied by user', | ||
INSUFFICIENT_FUNDS: 'Insufficient funds for transaction', | ||
}; | ||
|
||
export function getLedgerInstance() { | ||
/* | ||
* Instances have a required property which is `chainId` and is using in swap execution. | ||
* Here we are setting it as Ethereum always since we are supporting only eth for now. | ||
*/ | ||
return { chainId: ETHEREUM_CHAIN_ID }; | ||
} | ||
|
||
export async function getLedgerAccounts(): Promise<{ | ||
accounts: string[]; | ||
chainId: string; | ||
}> { | ||
try { | ||
const transport = await transportConnect(); | ||
|
||
const eth = new (await import('@ledgerhq/hw-app-eth')).default(transport); | ||
|
||
const accounts: string[] = []; | ||
|
||
const result = await eth.getAddress(ETH_BIP32_PATH, false, true); | ||
accounts.push(result.address); | ||
|
||
return { | ||
accounts: accounts, | ||
chainId: ETHEREUM_CHAIN_ID, | ||
}; | ||
} catch (error: any) { | ||
throw getLedgerError(error); | ||
} finally { | ||
await transportDisconnect(); | ||
} | ||
} | ||
|
||
export function getLedgerError(error: any) { | ||
const errorCode = error?.statusCode || error?.code; // ledger error || broadcast error | ||
|
||
if (errorCode && !!ledgerErrorMessages[errorCode]) { | ||
return new Error(ledgerErrorMessages[errorCode]); | ||
} | ||
return error; | ||
} | ||
|
||
let transportConnection: Transport | null = null; | ||
|
||
export async function transportConnect() { | ||
transportConnection = await ( | ||
await import('@ledgerhq/hw-transport-webhid') | ||
).default.create(); | ||
|
||
return transportConnection; | ||
} | ||
|
||
export async function transportDisconnect() { | ||
if (transportConnection) { | ||
await transportConnection.close(); | ||
transportConnection = null; | ||
} | ||
} |
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,52 @@ | ||
import type { | ||
Connect, | ||
Disconnect, | ||
WalletInfo, | ||
} from '@rango-dev/wallets-shared'; | ||
import type { BlockchainMeta, SignerFactory } from 'rango-types'; | ||
|
||
import { Networks, WalletTypes } from '@rango-dev/wallets-shared'; | ||
|
||
import { | ||
getLedgerAccounts, | ||
getLedgerInstance, | ||
transportDisconnect, | ||
} from './helpers'; | ||
import signer from './signer'; | ||
|
||
const WALLET = WalletTypes.LEDGER; | ||
|
||
export const config = { | ||
type: WALLET, | ||
}; | ||
|
||
export const getInstance = getLedgerInstance; | ||
export const connect: Connect = async () => { | ||
const ledgerAccounts = await getLedgerAccounts(); | ||
|
||
return ledgerAccounts; | ||
}; | ||
|
||
export const disconnect: Disconnect = async () => { | ||
void transportDisconnect(); | ||
}; | ||
|
||
export const getSigners: (provider: any) => SignerFactory = signer; | ||
|
||
export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = ( | ||
allBlockChains | ||
) => { | ||
const ethereumBlockchain = allBlockChains.find( | ||
(chain) => chain.name === Networks.ETHEREUM | ||
); | ||
return { | ||
name: 'Ledger', | ||
img: 'https://raw.githubusercontent.com/rango-exchange/assets/main/wallets/ledger/icon.svg', | ||
installLink: { | ||
DEFAULT: | ||
'https://support.ledger.com/hc/en-us/articles/4404389606417-Download-and-install-Ledger-Live?docs=true', | ||
}, | ||
color: 'black', | ||
supportedChains: ethereumBlockchain ? [ethereumBlockchain] : [], | ||
}; | ||
}; |
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,11 @@ | ||
import type { SignerFactory } from 'rango-types'; | ||
|
||
import { DefaultSignerFactory, TransactionType as TxType } from 'rango-types'; | ||
|
||
import { EthereumSigner } from './signers/ethereum'; | ||
|
||
export default function getSigners(): SignerFactory { | ||
const signers = new DefaultSignerFactory(); | ||
signers.registerSigner(TxType.EVM, new EthereumSigner()); | ||
return signers; | ||
} |
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,80 @@ | ||
import type { TransactionLike } from 'ethers'; | ||
import type { GenericSigner } from 'rango-types'; | ||
import type { EvmTransaction } from 'rango-types/lib/api/main'; | ||
|
||
import { JsonRpcProvider, Transaction } from 'ethers'; | ||
import { SignerError } from 'rango-types'; | ||
|
||
import { | ||
ETH_BIP32_PATH, | ||
getLedgerError, | ||
transportConnect, | ||
transportDisconnect, | ||
} from '../helpers'; | ||
|
||
export const RPC_PROVIDER_URL = 'https://rpc.ankr.com/eth'; | ||
|
||
export class EthereumSigner implements GenericSigner<EvmTransaction> { | ||
async signMessage(): Promise<string> { | ||
// TODO: Should be implemented using eth.signPersonalMessage | ||
throw SignerError.UnimplementedError('signMessage'); | ||
} | ||
|
||
async signAndSendTx( | ||
tx: EvmTransaction, | ||
fromAddress: string, | ||
chainId: string | null | ||
): Promise<{ hash: string }> { | ||
try { | ||
const provider = new JsonRpcProvider(RPC_PROVIDER_URL); // Provider to broadcast transaction | ||
|
||
const transactionCount = await provider.getTransactionCount(fromAddress); // Get nonce | ||
|
||
const transaction: TransactionLike<string> = { | ||
to: tx.to, | ||
gasPrice: tx.gasPrice, | ||
gasLimit: tx.gasLimit, | ||
nonce: transactionCount, | ||
chainId: chainId, | ||
data: tx.data, | ||
value: tx.value, | ||
maxPriorityFeePerGas: tx.maxPriorityFeePerGas, | ||
maxFeePerGas: tx.maxFeePerGas, | ||
}; | ||
|
||
const unsignedTx = | ||
Transaction.from(transaction).unsignedSerialized.substring(2); // Create unsigned transaction | ||
|
||
const resolution = await ( | ||
await import('@ledgerhq/hw-app-eth') | ||
).ledgerService.resolveTransaction(unsignedTx, {}, {}); // metadata necessary to allow the device to clear sign information | ||
|
||
const transport = await transportConnect(); | ||
|
||
const eth = new (await import('@ledgerhq/hw-app-eth')).default(transport); | ||
|
||
const signature = await eth.signTransaction( | ||
ETH_BIP32_PATH, | ||
unsignedTx, | ||
resolution | ||
); | ||
|
||
const signedTx = Transaction.from({ | ||
...transaction, | ||
signature: { | ||
r: '0x' + signature.r, | ||
s: '0x' + signature.s, | ||
v: parseInt(signature.v), | ||
}, | ||
}).serialized; | ||
|
||
const broadcastResult = await provider.broadcastTransaction(signedTx); | ||
|
||
return { hash: broadcastResult.hash }; | ||
} catch (error) { | ||
throw getLedgerError(error); | ||
} finally { | ||
await transportDisconnect(); | ||
} | ||
} | ||
} |
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,11 @@ | ||
{ | ||
// see https://www.typescriptlang.org/tsconfig to better understand tsconfigs | ||
"extends": "../../tsconfig.lib.json", | ||
"include": ["src", "types", "../../global-wallets-env.d.ts"], | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "./src", | ||
"lib": ["dom", "esnext"] | ||
// match output dir to input dir. e.g. dist/index instead of dist/src/index | ||
} | ||
} |
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.