Skip to content
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

Detect chainIds in eip681 URIs #882

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- added: Add eip681 chainId detection

## 4.34.0 (2025-01-14)

- changed: Upgrade edge-core-js to v2.23.0
Expand Down
31 changes: 31 additions & 0 deletions src/ethereum/EthereumTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { encodeUriCommon, parseUriCommon } from '../common/uriHelpers'
import {
biggyScience,
getLegacyDenomination,
hexToDecimal,
mergeDeeply,
multicastEthProviders
} from '../common/utils'
Expand Down Expand Up @@ -176,6 +177,13 @@ export class EthereumTools implements EdgeCurrencyTools {
networks[network] = true
})

// Non-ethereum networks can use ethereum: but must include a chainId
let mustValidateChainId = false
if (networks.ethereum == null) {
networks.ethereum = true
mustValidateChainId = true
}

const { parsedUri, edgeParsedUri } = await parseUriCommon({
currencyInfo: this.currencyInfo,
uri,
Expand Down Expand Up @@ -205,6 +213,29 @@ export class EthereumTools implements EdgeCurrencyTools {
}
address = contractAddress

if (
parsedUri.protocol === 'ethereum' &&
mustValidateChainId &&
!address.includes('@')
) {
throw new Error(
'chainId must be present when using ethereum: prefix on a non-ethereum network'
)
} else if (address.includes('@')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This else isn't needed, since the previous if can only throw. Un-indenting this block would make it more obvious that it runs in every case, assuming we pass the error check.

// Split the address to get the chainId according to EIP-681
const [address2, chainId] = address.split('@')
if (
chainId != null &&
hexToDecimal(chainId) !==
this.networkInfo.chainParams.chainId.toString()
) {
throw new Error(
`chainId '${chainId}' mismatch with pluginId ${this.currencyInfo.pluginId}.`
)
}
address = address2
}

// Verify checksum if it's present in the address
if (
/[A-F]/.test(address) &&
Expand Down
42 changes: 42 additions & 0 deletions test/plugin/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,27 @@ export default [
nativeAmount: '123'
}
},
'uri eip681 payment address with chainId': {
args: ['ethereum:0xf5d81254c269a1e984044e4d542adc07bf18c541@0x1'],
output: {
publicAddress: '0xf5d81254c269a1e984044e4d542adc07bf18c541'
}
},
'uri eip681 payment address with pay prefix and chainId': {
args: ['ethereum:pay-0xf5d81254c269a1e984044e4d542adc07bf18c541@0x1'],
output: {
publicAddress: '0xf5d81254c269a1e984044e4d542adc07bf18c541'
}
},
'uri eip681 payment address with pay prefix, chainId, and amount': {
args: [
'ethereum:pay-0xf5d81254c269a1e984044e4d542adc07bf18c541@0x1?value=123'
],
output: {
publicAddress: '0xf5d81254c269a1e984044e4d542adc07bf18c541',
nativeAmount: '123'
}
},
'uri eip681 payment address using scientific notation': {
args: [
'ethereum:0xf5d81254c269a1e984044e4d542adc07bf18c541?value=2.014e18'
Expand Down Expand Up @@ -744,6 +765,27 @@ export default [
'12345678900000000000000',
'RBTC'
],
'uri eip681 payment address with chainId': {
args: ['rsk:0xf5d81254c269a1e984044e4d542adc07bf18c541@0x1E'],
output: {
publicAddress: '0xf5d81254c269a1e984044e4d542adc07bf18c541'
}
},
'uri eip681 payment address with pay prefix and chainId': {
args: ['ethereum:pay-0xf5d81254c269a1e984044e4d542adc07bf18c541@0x1E'],
output: {
publicAddress: '0xf5d81254c269a1e984044e4d542adc07bf18c541'
}
},
'uri eip681 payment address with pay prefix, chainId, and amount': {
args: [
'ethereum:pay-0xf5d81254c269a1e984044e4d542adc07bf18c541@0x1E?value=123'
],
output: {
publicAddress: '0xf5d81254c269a1e984044e4d542adc07bf18c541',
nativeAmount: '123'
}
},
'uri address with unique identifier': [
'rsk:0x04b6b3bcbc16a5fb6a20301d650f8def513122a8?dt=123456789',
'0x04b6b3bcbc16a5fb6a20301d650f8def513122a8',
Expand Down
3 changes: 3 additions & 0 deletions test/plugin/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ for (const fixture of fixtures) {
'address only with provided currency code',
'uri eip681 payment address',
'uri eip681 payment address with pay prefix',
'uri eip681 payment address with chainId',
'uri eip681 payment address with pay prefix and chainId',
'uri eip681 payment address with pay prefix, chainId, and amount',
'uri eip681 payment address using scientific notation',
'uri eip681 transfer contract invocation',
'RenBrige Gateway uri address with amount, label & message',
Expand Down
Loading