Skip to content

Commit

Permalink
🐛 Fix Infura endpoint mapping for ERC115 integration
Browse files Browse the repository at this point in the history
  • Loading branch information
davisshaver committed Feb 15, 2024
1 parent 962e731 commit 134f1b4
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 30 deletions.
27 changes: 1 addition & 26 deletions inc/class-wp-rainbow-login-functionality.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,31 +220,6 @@ public function nonce_callback(): WP_REST_Response {
return new WP_REST_Response( $nonce );
}

/**
* Map filtered Infura network to an Infura endpoint value.
*
* @param string $filtered_network Filtered Infura network.
*
* @return string Infura endpoint or default
*/
private function map_filtered_network_to_infura_endpoint( string $filtered_network ): string {
$overrides = [
'arbitrum' => 'arbitrum-mainnet',
'arbitrumGoerli' => 'arbitrum-goerli',
'arbitrumSepolia' => 'arbitrum-sepolia',
'optimism' => 'optimism-mainnet',
'optimismGoerli' => 'optimism-goerli',
'optimismSepolia' => 'optimism-sepolia',
'polygon' => 'polygon-mainnet',
'polygonMumbai' => 'polygon-mumbai',
];
if ( ! empty( $overrides[ $filtered_network ] ) ) {
return $overrides[ $filtered_network ];
}

return $filtered_network;
}

/**
* Validates Log In with Ethereum request.
*
Expand Down Expand Up @@ -321,7 +296,7 @@ public function login_callback( WP_REST_Request $request ): WP_REST_Response {
// @TODO Figure out if ABI should be an option (or formatted differently).

$example_abi = '[{"constant":true,"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]';
$contract = new Contract( 'https://' . $this->map_filtered_network_to_infura_endpoint( $filtered_infura_network ) . '.infura.io/v3/' . $filtered_infura_id, $example_abi );
$contract = new Contract( 'https://' . map_filtered_network_to_infura_endpoint( $filtered_infura_network ) . '.infura.io/v3/' . $filtered_infura_id, $example_abi );
$contract->at( $wp_rainbow_options['wp_rainbow_field_required_token'] )->call(
'balanceOf',
$address,
Expand Down
25 changes: 25 additions & 0 deletions inc/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Map filtered Infura network to an Infura endpoint value.
*
* @param string $filtered_network Filtered Infura network.
*
* @return string Infura endpoint or default
*/
function map_filtered_network_to_infura_endpoint( string $filtered_network ): string {
$overrides = [
'arbitrum' => 'arbitrum-mainnet',
'arbitrumGoerli' => 'arbitrum-goerli',
'arbitrumSepolia' => 'arbitrum-sepolia',
'optimism' => 'optimism-mainnet',
'optimismGoerli' => 'optimism-goerli',
'optimismSepolia' => 'optimism-sepolia',
'polygon' => 'polygon-mainnet',
'polygonMumbai' => 'polygon-mumbai',
];
if ( ! empty( $overrides[ $filtered_network ] ) ) {
return $overrides[ $filtered_network ];
}

return $filtered_network;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wp-rainbow",
"version": "0.4.5",
"version": "0.4.6",
"description": "RainbowKit Login (Web3 Integration for Sign-In With Ethereum)",
"author": "Davis Shaver",
"license": "GPL-2.0-or-later",
Expand Down
2 changes: 1 addition & 1 deletion plugins/class-wp-rainbow-plugins-erc-1155-roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function filter_wp_rainbow_role_for_address( string $default_role, string
}
$erc1155_abi = '[{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]';
$erc1155_role = '';
$contract = new Contract( 'https://' . $filtered_infura_network . '.infura.io/v3/' . $filtered_infura_id, $erc1155_abi );
$contract = new Contract( 'https://' . map_filtered_network_to_infura_endpoint( $filtered_infura_network ) . '.infura.io/v3/' . $filtered_infura_id, $erc1155_abi );
$wp_rainbow_options = get_option(
'wp_rainbow_options',
[
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: WordPress, web3, SIWE, Ethereum, RainbowKit, Sign-In With Ethereum
Tested up to: 6.4.3
Requires at least: 5.9
Requires PHP: 7.0
Stable tag: 0.4.5
Stable tag: 0.4.6
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -70,6 +70,9 @@ Find reference implementations of all filters in [example plugin here](https://g

== Changelog ==

= 0.4.6 =
* Fix bug with Infura endpoint mapping

= 0.4.5 =
* Fix bug with Polygon network, add Polygon Mumbai

Expand Down
20 changes: 20 additions & 0 deletions tests/unit/class-erc-1155-role-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,24 @@ function ( $err, $balance ) use ( &$erc1155_check ) {
);
$this->assertTrue( $erc1155_check );
}

/**
* Verify ERC-1155 role logic on Polygon.
*/
public function testVerifyERC1155Polygon() {
$erc1155_abi = '[{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]';
$contract = new Contract( 'https://polygon-mainnet.infura.io/v3/9861d9fc6201412a801f7a2380fb2f0c', $erc1155_abi );
$erc1155_check = false;
$contract->at( '0xB5Cd475c41b7c59646Fa2779Dd5fA54765B61039' )->call(
'balanceOf',
'0xBaab0948403fc1D524928074663d21282F7F1412',
'0',
function ( $err, $balance ) use ( &$erc1155_check ) {
if ( hexdec( $balance[0]->value ) >= 1 ) {
$erc1155_check = true;
}
}
);
$this->assertTrue( $erc1155_check );
}
}
5 changes: 4 additions & 1 deletion wp-rainbow.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Plugin Name: RainbowKit Login (Web3 Integration for Sign-In With Ethereum)
* Plugin URI: https://wp-rainbow.davisshaver.com/
* Description: RainbowKit Login allows WordPress users to log in with Ethereum using the Sign-In With Ethereum standard, powered by RainbowKit.
* Version: 0.4.5
* Version: 0.4.6
* Author: Davis Shaver
* Author URI: https://davisshaver.com/
* License: GPL v2 or later
Expand Down Expand Up @@ -40,6 +40,9 @@ function () {
1
);

// Require the helper functions.
require_once __DIR__ . '/inc/functions.php';

// Require the main class.
require_once __DIR__ . '/inc/class-wp-rainbow.php';
require_once __DIR__ . '/inc/class-wp-rainbow-login-functionality.php';
Expand Down

0 comments on commit 134f1b4

Please sign in to comment.