├── .editorconfig ├── .github ├── actions │ └── ci │ │ └── action.yml ├── dependabot.yml └── workflows │ ├── ci.yml │ ├── release.yml │ └── sonarcloud.yml ├── .gitignore ├── Core Modules ├── WalletConnectSharp.Common │ ├── Events │ │ ├── EventHandlerMap.cs │ │ └── GenericEventHolder.cs │ ├── IModule.cs │ ├── Logging │ │ ├── ILogger.cs │ │ ├── WCLogger.cs │ │ └── WrapperLogger.cs │ ├── Model │ │ ├── Errors │ │ │ ├── ErrorType.cs │ │ │ ├── ExpiredException.cs │ │ │ ├── NamespacesException.cs │ │ │ ├── SdkErrors.cs │ │ │ └── WalletConnectException.cs │ │ ├── IsolatedModule.cs │ │ └── Relay │ │ │ ├── Relay.cs │ │ │ └── RelayProtocols.cs │ ├── SDKConstants.cs │ ├── Utils │ │ ├── Clock.cs │ │ ├── Constants.cs │ │ ├── DictionaryComparer.cs │ │ ├── EventUtils.cs │ │ ├── Extensions.cs │ │ ├── HashUtils.cs │ │ ├── HexByteConvertorExtensions.cs │ │ ├── ListComparer.cs │ │ ├── RpcPayloadId.cs │ │ ├── TypeSafety.cs │ │ └── UrlUtils.cs │ └── WalletConnectSharp.Common.csproj ├── WalletConnectSharp.Crypto │ ├── Crypto.cs │ ├── Encoder │ │ ├── ArrayHelpers.cs │ │ ├── Base58Encoding.cs │ │ ├── BaseX.cs │ │ ├── Bases.cs │ │ └── Codec.cs │ ├── Interfaces │ │ ├── ICrypto.cs │ │ └── IKeyChain.cs │ ├── KeyChain.cs │ ├── Models │ │ ├── DecodeOptions.cs │ │ ├── EncodeOptions.cs │ │ ├── EncodingParams.cs │ │ ├── EncodingValidation.cs │ │ ├── EncryptParams.cs │ │ ├── IridiumJWTData.cs │ │ ├── IridiumJWTDecoded.cs │ │ ├── IridiumJWTHeader.cs │ │ ├── IridiumJWTPayload.cs │ │ └── IridiumJWTSigned.cs │ └── WalletConnectSharp.Crypto.csproj ├── WalletConnectSharp.Network.Websocket │ ├── WalletConnectSharp.Network.Websocket.csproj │ ├── WebsocketConnection.cs │ └── WebsocketConnectionBuilder.cs ├── WalletConnectSharp.Network │ ├── Interfaces │ │ ├── IBaseJsonRpcProvider.cs │ │ ├── IConnectionBuilder.cs │ │ ├── IJsonRpcConnection.cs │ │ ├── IJsonRpcError.cs │ │ ├── IJsonRpcPayload.cs │ │ ├── IJsonRpcProvider.cs │ │ ├── IJsonRpcRequest.cs │ │ ├── IJsonRpcResult.cs │ │ ├── IRelayUrlBuilder.cs │ │ └── IRequestArguments.cs │ ├── JsonRpcProvider.cs │ ├── Models │ │ ├── BaseJsonRpcRequest.cs │ │ ├── Error.cs │ │ ├── JsonRpcError.cs │ │ ├── JsonRpcPayload.cs │ │ ├── JsonRpcRequest.cs │ │ ├── JsonRpcResponse.cs │ │ ├── RequestArguments.cs │ │ ├── RpcMethodAttribute.cs │ │ ├── RpcOptionsAttribute.cs │ │ ├── RpcRequestOptionsAttribute.cs │ │ └── RpcResponseOptionsAttribute.cs │ ├── Utils │ │ ├── RelayUrlBuilder.cs │ │ └── Validation.cs │ └── WalletConnectSharp.Network.csproj └── WalletConnectSharp.Storage │ ├── FileSystemStorage.cs │ ├── InMemoryStorage.cs │ ├── Interfaces │ └── IKeyValueStorage.cs │ └── WalletConnectSharp.Storage.csproj ├── Directory.Build.props ├── Directory.Packages.props ├── LICENSE ├── README.md ├── Tests ├── WalletConnectSharp.Auth.Tests │ ├── AuthClientTest.cs │ ├── SignatureTest.cs │ └── WalletConnectSharp.Auth.Tests.csproj ├── WalletConnectSharp.Crypto.Tests │ ├── CryptoFixture.cs │ ├── CryptoTests.cs │ ├── Models │ │ └── TopicData.cs │ └── WalletConnectSharp.Crypto.Tests.csproj ├── WalletConnectSharp.Examples │ ├── BiDirectional.cs │ ├── IExample.cs │ ├── Program.cs │ ├── SimpleExample.cs │ └── WalletConnectSharp.Examples.csproj ├── WalletConnectSharp.Network.Tests │ ├── Models │ │ └── TopicData.cs │ ├── RelayTests.cs │ └── WalletConnectSharp.Network.Tests.csproj ├── WalletConnectSharp.Sign.Test │ ├── NamespaceTests.cs │ ├── Shared │ │ └── SignTestValues.cs │ ├── SignClientFixture.cs │ ├── SignTests.cs │ └── WalletConnectSharp.Sign.Test.csproj ├── WalletConnectSharp.Storage.Test │ ├── FileSystemStorageTest.cs │ └── WalletConnectSharp.Storage.Test.csproj ├── WalletConnectSharp.Tests.Common │ ├── CryptoWalletFixture.cs │ ├── TempFolder.cs │ ├── TestValues.cs │ ├── TwoClientsFixture.cs │ ├── UtilExtensions.cs │ └── WalletConnectSharp.Tests.Common.csproj └── WalletConnectSharp.Web3Wallet.Tests │ ├── AuthTests.cs │ ├── SignTests.cs │ └── WalletConnectSharp.Web3Wallet.Tests.csproj ├── WalletConnectSharp.Auth ├── Cacao.cs ├── Controllers │ └── AuthEngine.cs ├── Interfaces │ ├── IAuthClient.cs │ ├── IAuthClientEvents.cs │ └── IAuthEngine.cs ├── Internals │ ├── AuthEngineValidations.cs │ ├── CryptoUtils.cs │ ├── IssDidUtils.cs │ └── SignatureUtils.cs ├── Models │ ├── AuthData.cs │ ├── AuthErrorResponse.cs │ ├── AuthOptions.cs │ ├── AuthPayload.cs │ ├── AuthRequest.cs │ ├── AuthRequestData.cs │ ├── AuthResponse.cs │ ├── ErrorResponse.cs │ ├── Message.cs │ ├── PairingData.cs │ ├── PayloadParams.cs │ ├── PendingRequest.cs │ ├── RequestParams.cs │ ├── RequestUri.cs │ ├── Requester.cs │ ├── ResultResponse.cs │ └── TopicMessage.cs ├── WalletConnectAuthClient.cs ├── WalletConnectSharp.Auth.csproj └── WcAuthRequest.cs ├── WalletConnectSharp.Core ├── Controllers │ ├── Expirer.cs │ ├── HeartBeat.cs │ ├── JsonRpcHistory.cs │ ├── JsonRpcHistoryFactory.cs │ ├── MessageTracker.cs │ ├── Pairing.cs │ ├── PairingStore.cs │ ├── Publisher.cs │ ├── Relayer.cs │ ├── Store.cs │ ├── Subscriber.cs │ ├── TopicMap.cs │ └── TypedMessageHandler.cs ├── Core.cs ├── Interfaces │ ├── ICore.cs │ ├── IExpirer.cs │ ├── IHeartBeat.cs │ ├── IJsonRpcHistory.cs │ ├── IJsonRpcHistoryFactory.cs │ ├── IKeyHolder.cs │ ├── IMessageTracker.cs │ ├── IPairing.cs │ ├── IPairingStore.cs │ ├── IPublisher.cs │ ├── IRelayer.cs │ ├── IStore.cs │ ├── ISubscriber.cs │ ├── ISubscriberMap.cs │ └── ITypedMessageHandler.cs ├── Metadata.cs ├── Models │ ├── BatchFetchMessageRequest.cs │ ├── BatchFetchMessagesResponse.cs │ ├── CoreOptions.cs │ ├── DisposeHandlerToken.cs │ ├── Eth │ │ └── EthCall.cs │ ├── Expirer │ │ ├── Expiration.cs │ │ ├── ExpirerEventArgs.cs │ │ └── ExpirerTarget.cs │ ├── History │ │ ├── JsonRpcRecord.cs │ │ └── RequestEvent.cs │ ├── MessageHandler │ │ ├── RequestEventArgs.cs │ │ ├── ResponseEventArgs.cs │ │ └── TypedEventHandler.cs │ ├── Pairing │ │ ├── CreatePairingData.cs │ │ ├── Methods │ │ │ ├── PairingDelete.cs │ │ │ └── PairingPing.cs │ │ ├── PairingEvent.cs │ │ ├── PairingStruct.cs │ │ └── UriParameters.cs │ ├── Publisher │ │ └── PublishParams.cs │ ├── RedirectData.cs │ ├── Relay │ │ ├── DecodedMessageEvent.cs │ │ ├── MessageEvent.cs │ │ ├── ProtocolOptionHolder.cs │ │ ├── ProtocolOptions.cs │ │ ├── PublishOptions.cs │ │ ├── RelayPublishRequest.cs │ │ ├── RelayerOptions.cs │ │ ├── SubscribeOptions.cs │ │ └── UnsubscribeOptions.cs │ ├── Subscriber │ │ ├── ActiveSubscription.cs │ │ ├── BatchSubscribeParams.cs │ │ ├── DeletedSubscription.cs │ │ ├── JsonRpcSubscriberParams.cs │ │ ├── JsonRpcSubscriptionParams.cs │ │ ├── JsonRpcUnsubscribeParams.cs │ │ ├── MessageData.cs │ │ └── PendingSubscription.cs │ └── Verify │ │ ├── Validation.cs │ │ ├── VerifiedContext.cs │ │ └── Verifier.cs ├── Utils.cs ├── WalletConnectCore.cs └── WalletConnectSharp.Core.csproj ├── WalletConnectSharp.Sign ├── Controllers │ ├── AddressProvider.cs │ ├── PendingRequests.cs │ ├── Proposal.cs │ └── Session.cs ├── Engine.cs ├── Interfaces │ ├── IAddressProvider.cs │ ├── IEngine.cs │ ├── IEngineAPI.cs │ ├── IEnginePrivate.cs │ ├── IPendingRequests.cs │ ├── IProposal.cs │ ├── ISession.cs │ ├── ISignClient.cs │ └── IWcMethod.cs ├── Internals │ ├── EngineHandler.cs │ ├── EngineTasks.cs │ └── EngineValidation.cs ├── Models │ ├── Caip25Address.cs │ ├── DefaultsLoadingEventArgs.cs │ ├── Engine │ │ ├── ApproveParams.cs │ │ ├── ConnectOptions.cs │ │ ├── ConnectedData.cs │ │ ├── Events │ │ │ ├── EmitEvent.cs │ │ │ ├── EventData.cs │ │ │ ├── SessionEvent.cs │ │ │ ├── SessionProposalEvent.cs │ │ │ ├── SessionRequestEvent.cs │ │ │ └── SessionUpdateEvent.cs │ │ ├── IAcknowledgement.cs │ │ ├── IApprovedData.cs │ │ ├── Methods │ │ │ ├── SessionDelete.cs │ │ │ ├── SessionEvent.cs │ │ │ ├── SessionExtend.cs │ │ │ ├── SessionPing.cs │ │ │ ├── SessionPropose.cs │ │ │ ├── SessionProposeResponse.cs │ │ │ ├── SessionProposeResponseAutoReject.cs │ │ │ ├── SessionProposeResponseReject.cs │ │ │ ├── SessionRequest.cs │ │ │ ├── SessionSettle.cs │ │ │ └── SessionUpdate.cs │ │ └── RejectParams.cs │ ├── Namespace.cs │ ├── Namespaces.cs │ ├── Participant.cs │ ├── PendingRequestStruct.cs │ ├── ProposalStruct.cs │ ├── ProposedNamespace.cs │ ├── RequiredNamespaces.cs │ ├── SessionRequestEventHandler.cs │ ├── SessionStruct.cs │ └── SignClientOptions.cs ├── WalletConnectSharp.Sign.csproj └── WalletConnectSignClient.cs ├── WalletConnectSharp.Web3Wallet ├── Controllers │ └── Web3WalletEngine.cs ├── Interfaces │ ├── IWeb3Wallet.cs │ ├── IWeb3WalletApi.cs │ └── IWeb3WalletEngine.cs ├── Models │ ├── BaseEventArgs.cs │ └── SessionRequestEventArgs.cs ├── WalletConnectSharp.Web3Wallet.csproj └── Web3WalletClient.cs ├── WalletConnectSharpV2.sln ├── WalletConnectSharpV2.sln.DotSettings ├── global.json └── resources └── icon.png /.github/actions/ci/action.yml: -------------------------------------------------------------------------------- 1 | name: 'ci' 2 | description: 'Executes C# specific CI steps' 3 | inputs: 4 | type: 5 | description: 'The type of CI step to run' 6 | required: true 7 | relay-endpoint: 8 | description: 'The endpoint of the relay e.g. relay.walletconnect.com' 9 | required: false 10 | default: 'wss://relay.walletconnect.com' 11 | project-id: 12 | description: 'WalletConnect project id' 13 | required: true 14 | 15 | runs: 16 | using: "composite" 17 | steps: 18 | - name: Run tests 19 | if: inputs.type == 'unit-tests' 20 | shell: bash 21 | run: dotnet test --verbosity minimal --filter Category=unit 22 | 23 | - name: Run integration tests 24 | if: inputs.type == 'integration-tests' 25 | shell: bash 26 | env: 27 | RELAY_ENDPOINT: ${{ inputs.relay-endpoint }} 28 | PROJECT_ID: ${{ inputs.project-id }} 29 | run: dotnet test --verbosity minimal --filter Category=integration 30 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "nuget" 4 | open-pull-requests-limit: 2 5 | directory: "/" 6 | schedule: 7 | interval: "weekly" 8 | day: "monday" 9 | time: "09:00" 10 | timezone: "America/New_York" 11 | allow: 12 | - dependency-type: "direct" 13 | labels: 14 | - "dependencies" 15 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: .NET Build & Test 2 | 3 | on: 4 | push: 5 | branches: [ main, '2.0' ] 6 | pull_request: 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - uses: actions/checkout@v4 14 | 15 | - name: Setup .NET 16 | uses: actions/setup-dotnet@v3 17 | with: 18 | dotnet-version: | 19 | 8.0.x 20 | 21 | - name: Restore dependencies 22 | run: dotnet restore 23 | working-directory: ./ 24 | 25 | - name: Build 26 | run: dotnet build --no-restore 27 | working-directory: ./ 28 | 29 | - name: Upload build artifacts 30 | uses: actions/upload-artifact@v3 31 | with: 32 | name: build-artifacts 33 | path: | 34 | **/bin/**/*.dll 35 | !**/Core Modules/**/bin/**/*.dll 36 | !**/Tests/**/bin/**/*.dll 37 | 38 | test: 39 | needs: build 40 | runs-on: ubuntu-latest 41 | strategy: 42 | matrix: 43 | test-type: [unit-tests, integration-tests] 44 | 45 | steps: 46 | - uses: actions/checkout@v4 47 | 48 | - name: Setup .NET 49 | uses: actions/setup-dotnet@v3 50 | with: 51 | dotnet-version: | 52 | 8.0.x 53 | 54 | - name: Run tests 55 | uses: ./.github/actions/ci 56 | with: 57 | type: ${{ matrix.test-type }} 58 | project-id: ${{ secrets.PROJECT_ID }} -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | release: 5 | types: [published] 6 | 7 | jobs: 8 | test: 9 | runs-on: ubuntu-latest 10 | timeout-minutes: 15 11 | steps: 12 | - name: Checkout 13 | uses: actions/checkout@v4 14 | 15 | - name: Setup .NET 16 | uses: actions/setup-dotnet@v3 17 | with: 18 | dotnet-version: | 19 | 8.0.x 20 | 21 | - name: Run tests 22 | uses: ./.github/actions/ci 23 | with: 24 | type: 'unit-tests' 25 | project-id: ${{ secrets.PROJECT_ID }} 26 | 27 | pack: 28 | needs: test 29 | runs-on: ubuntu-latest 30 | timeout-minutes: 15 31 | steps: 32 | - name: Checkout 33 | uses: actions/checkout@v4 34 | 35 | - name: Setup .NET 36 | uses: actions/setup-dotnet@v3 37 | with: 38 | dotnet-version: | 39 | 8.0.x 40 | 41 | - name: Build Release 42 | run: dotnet build -c Release 43 | 44 | - name: Pack nugets 45 | run: dotnet pack -c Release --no-build --output . 46 | 47 | - name: Push to NuGet 48 | run: dotnet nuget push "*.nupkg" --api-key ${{secrets.nuget_api_key}} --source https://api.nuget.org/v3/index.json 49 | 50 | - name: Upload build artifacts 51 | uses: actions/upload-artifact@v3 52 | with: 53 | name: build-artifacts 54 | path: | 55 | **/bin/**/*.dll 56 | !**/Tests/**/bin/**/*.dll 57 | 58 | dispatch: 59 | needs: pack 60 | runs-on: ubuntu-latest 61 | steps: 62 | - name: Trigger workflow in WalletConnectUnity 63 | uses: peter-evans/repository-dispatch@v2 64 | with: 65 | token: ${{ secrets.REPOSITORY_DISPATCH_PAT }} 66 | repository: walletconnect/walletconnectunity 67 | event-type: new-release 68 | client-payload: '{"tag_name": "${{ github.event.release.tag_name }}", "sha": "${{ github.sha }}"}' -------------------------------------------------------------------------------- /.github/workflows/sonarcloud.yml: -------------------------------------------------------------------------------- 1 | name: SonarCloud 2 | on: 3 | workflow_dispatch: 4 | push: 5 | branches: 6 | - 2.0 7 | pull_request: 8 | types: [opened, synchronize, reopened] 9 | jobs: 10 | build: 11 | name: Build and analyze 12 | runs-on: windows-latest 13 | steps: 14 | - name: Set up JDK 17 15 | uses: actions/setup-java@v3 16 | with: 17 | java-version: 17 18 | distribution: 'zulu' # Alternative distribution options are available. 19 | - uses: actions/checkout@v3 20 | with: 21 | fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis 22 | - name: Cache SonarCloud packages 23 | uses: actions/cache@v3 24 | with: 25 | path: ~\sonar\cache 26 | key: ${{ runner.os }}-sonar 27 | restore-keys: ${{ runner.os }}-sonar 28 | - name: Cache SonarCloud scanner 29 | id: cache-sonar-scanner 30 | uses: actions/cache@v3 31 | with: 32 | path: .\.sonar\scanner 33 | key: ${{ runner.os }}-sonar-scanner 34 | restore-keys: ${{ runner.os }}-sonar-scanner 35 | - name: Install SonarCloud scanner 36 | if: steps.cache-sonar-scanner.outputs.cache-hit != 'true' 37 | shell: powershell 38 | run: | 39 | New-Item -Path .\.sonar\scanner -ItemType Directory 40 | dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner 41 | - name: Install dotnet-coverage 42 | run: dotnet tool install --global dotnet-coverage 43 | - name: Build and analyze 44 | env: 45 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any 46 | SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} 47 | shell: powershell 48 | run: | 49 | .\.sonar\scanner\dotnet-sonarscanner begin /k:"WalletConnect_WalletConnectSharp" /o:"walletconnect" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml 50 | dotnet build --no-incremental 51 | dotnet-coverage collect "dotnet test" -f xml -o "coverage.xml" 52 | .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}" 53 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Events/GenericEventHolder.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Concurrent; 2 | 3 | namespace WalletConnectSharp.Common.Events; 4 | 5 | public class GenericEventHolder 6 | { 7 | private readonly ConcurrentDictionary _dynamicTypeMapping = new(); 8 | 9 | public EventHandlerMap OfType() 10 | { 11 | Type t = typeof(T); 12 | 13 | if (_dynamicTypeMapping.TryGetValue(t, out var value)) 14 | { 15 | if (value is EventHandlerMap eventHandlerMap) 16 | { 17 | return eventHandlerMap; 18 | } 19 | 20 | throw new InvalidCastException($"Stored value cannot be cast to EventHandlerMap<{typeof(T).Name}>"); 21 | } 22 | 23 | var mapping = new EventHandlerMap(); 24 | _dynamicTypeMapping.TryAdd(t, mapping); 25 | 26 | return mapping; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/IModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace WalletConnectSharp.Common 4 | { 5 | /// 6 | /// An interface that represents a module 7 | /// 8 | public interface IModule : IDisposable 9 | { 10 | /// 11 | /// The name of this module 12 | /// 13 | string Name { get; } 14 | 15 | /// 16 | /// The context string of this module, used to define 17 | /// separate instances of the same module 18 | /// 19 | string Context { get; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Logging/ILogger.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Common.Logging 2 | { 3 | public interface ILogger 4 | { 5 | void Log(string message); 6 | 7 | void LogError(string message); 8 | 9 | void LogError(Exception e); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Logging/WCLogger.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Common.Logging 2 | { 3 | public class WCLogger 4 | { 5 | public static ILogger Logger; 6 | 7 | public static ILogger WithContext(string context) 8 | { 9 | return new WrapperLogger(Logger, context); 10 | } 11 | 12 | public static void Log(string message) 13 | { 14 | if (Logger == null) 15 | return; 16 | 17 | Logger.Log(message); 18 | } 19 | 20 | public static void LogError(string message) 21 | { 22 | if (Logger == null) 23 | return; 24 | 25 | Logger.LogError(message); 26 | } 27 | 28 | public static void LogError(Exception e) 29 | { 30 | if (Logger == null) 31 | return; 32 | 33 | Logger.LogError(e); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Logging/WrapperLogger.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Common.Logging; 2 | 3 | public class WrapperLogger : ILogger 4 | { 5 | private ILogger _logger; 6 | private string prefix; 7 | 8 | public WrapperLogger(ILogger logger, string prefix) 9 | { 10 | _logger = logger; 11 | this.prefix = prefix; 12 | } 13 | 14 | public void Log(string message) 15 | { 16 | if (_logger == null) return; 17 | _logger.Log($"[{prefix}] {message}"); 18 | } 19 | 20 | public void LogError(string message) 21 | { 22 | if (_logger == null) return; 23 | _logger.LogError($"[{prefix}] {message}"); 24 | } 25 | 26 | public void LogError(Exception e) 27 | { 28 | if (_logger == null) return; 29 | _logger.LogError(e); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Model/Errors/ErrorType.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Common.Model.Errors 2 | { 3 | /// 4 | /// The ErrorType is an enum of error codes defined by 5 | /// their name 6 | /// 7 | public enum ErrorType : uint 8 | { 9 | // 0 (Generic) 10 | GENERIC = 0, 11 | 12 | // 2000 (Timeout) 13 | SETTLE_TIMEOUT = 2000, 14 | JSONRPC_REQUEST_TIMEOUT = 2001, 15 | 16 | // 3000 (Unauthorized) 17 | UNAUTHORIZED_TARGET_CHAIN = 3000, 18 | UNAUTHORIZED_JSON_RPC_METHOD = 3001, 19 | UNAUTHORIZED_NOTIFICATION_TYPE = 3002, 20 | UNAUTHORIZED_UPDATE_REQUEST = 3003, 21 | UNAUTHORIZED_UPGRADE_REQUEST = 3004, 22 | UNAUTHORIZED_EXTEND_REQUEST = 3005, 23 | UNAUTHORIZED_MATCHING_CONTROLLER = 3100, 24 | UNAUTHORIZED_METHOD = 3101, 25 | 26 | // 4000 (EIP-1193) 27 | JSONRPC_REQUEST_METHOD_REJECTED = 4001, 28 | JSONRPC_REQUEST_METHOD_UNAUTHORIZED = 4100, 29 | JSONRPC_REQUEST_METHOD_UNSUPPORTED = 4200, 30 | DISCONNECTED_ALL_CHAINS = 4900, 31 | DISCONNECTED_TARGET_CHAIN = 4901, 32 | 33 | // 5000 (CAIP-25) 34 | DISAPPROVED_CHAINS = 5000, 35 | DISAPPROVED_JSONRPC = 5001, 36 | DISAPPROVED_NOTIFICATION = 5002, 37 | UNSUPPORTED_CHAINS = 5100, 38 | UNSUPPORTED_JSONRPC = 5101, 39 | UNSUPPORTED_NOTIFICATION = 5102, 40 | UNSUPPORTED_ACCOUNTS = 5103, 41 | 42 | // 6000 (Reason) 43 | USER_DISCONNECTED = 6000, 44 | 45 | // 8000 (Session) 46 | SESSION_REQUEST_EXPIRED = 8000, 47 | 48 | // 9000 (Unknown) 49 | UNKNOWN = 9000, 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Model/Errors/ExpiredException.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.Serialization; 2 | 3 | namespace WalletConnectSharp.Common.Model.Errors; 4 | 5 | public class ExpiredException : Exception 6 | { 7 | public ExpiredException() 8 | { 9 | } 10 | 11 | public ExpiredException(string message) 12 | : base(message) 13 | { 14 | } 15 | 16 | public ExpiredException(string message, Exception innerException) 17 | : base(message, innerException) 18 | { 19 | } 20 | 21 | protected ExpiredException(SerializationInfo info, StreamingContext context) 22 | : base(info, context) 23 | { 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Model/Errors/NamespacesException.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.Serialization; 2 | 3 | namespace WalletConnectSharp.Common.Model.Errors; 4 | 5 | public class NamespacesException : Exception 6 | { 7 | public NamespacesException() 8 | { 9 | } 10 | 11 | public NamespacesException(string message) 12 | : base(message) 13 | { 14 | } 15 | 16 | public NamespacesException(string message, Exception innerException) 17 | : base(message, innerException) 18 | { 19 | } 20 | 21 | protected NamespacesException(SerializationInfo info, StreamingContext context) 22 | : base(info, context) 23 | { 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Model/IsolatedModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace WalletConnectSharp.Common.Model 5 | { 6 | /// 7 | /// A mock module that represents nothing. Useful for creating an empty 8 | /// context 9 | /// 10 | public sealed class IsolatedModule : IModule 11 | { 12 | private static HashSet activeModules = new HashSet(); 13 | 14 | private Guid _guid; 15 | 16 | public string Name 17 | { 18 | get 19 | { 20 | return $"isolated-module-{Context}"; 21 | } 22 | } 23 | 24 | public string Context 25 | { 26 | get 27 | { 28 | return $"IM-{_guid.ToString()}"; 29 | } 30 | } 31 | 32 | public IsolatedModule() 33 | { 34 | do 35 | { 36 | _guid = Guid.NewGuid(); 37 | } while (activeModules.Contains(_guid)); 38 | 39 | activeModules.Add(_guid); 40 | } 41 | 42 | public void Dispose() { } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Model/Relay/Relay.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Common.Model.Relay 2 | { 3 | /// 4 | /// Constants for the Relay API 5 | /// 6 | public static class RelayConstants 7 | { 8 | /// 9 | /// The protocol string 10 | /// 11 | public const string Protocol = "wc"; 12 | 13 | /// 14 | /// The current version of the Relay protocol 15 | /// 16 | public const int Version = 2; 17 | } 18 | } -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/SDKConstants.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace WalletConnectSharp.Common 4 | { 5 | /// 6 | /// Class defining SDK constants 7 | /// 8 | public static class SDKConstants 9 | { 10 | /// 11 | /// The current version of the SDK 12 | /// 13 | public static readonly string SDK_VERSION = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "2.0.0-undefined"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Utils/Constants.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Common.Utils 2 | { 3 | /// 4 | /// Core constants used by the Core module 5 | /// 6 | public static class Constants 7 | { 8 | /// 9 | /// The core protocol string 10 | /// 11 | public const string CORE_PROTOCOL = "wc"; 12 | 13 | /// 14 | /// The core protocol version 15 | /// 16 | public const int CORE_VERSION = 2; 17 | 18 | /// 19 | /// The core context string 20 | /// 21 | public const string CORE_CONTEXT = "core"; 22 | 23 | /// 24 | /// The core storage prefix 25 | /// 26 | public static readonly string CORE_STORAGE_PREFIX = CORE_PROTOCOL + "@" + CORE_VERSION + ":" + CORE_CONTEXT + ":"; 27 | } 28 | } -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Utils/DictionaryComparer.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Common.Utils 2 | { 3 | public class DictionaryComparer : 4 | IEqualityComparer> 5 | { 6 | private IEqualityComparer valueComparer; 7 | public DictionaryComparer(IEqualityComparer valueComparer = null) 8 | { 9 | this.valueComparer = valueComparer ?? EqualityComparer.Default; 10 | } 11 | public bool Equals(Dictionary x, Dictionary y) 12 | { 13 | if (x.Count != y.Count) 14 | return false; 15 | if (x.Keys.Except(y.Keys).Any()) 16 | return false; 17 | if (y.Keys.Except(x.Keys).Any()) 18 | return false; 19 | foreach (var pair in x) 20 | if (!valueComparer.Equals(pair.Value, y[pair.Key])) 21 | return false; 22 | return true; 23 | } 24 | 25 | public int GetHashCode(Dictionary obj) 26 | { 27 | throw new NotImplementedException(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Utils/EventUtils.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Common.Utils; 2 | 3 | public class EventUtils 4 | { 5 | /// 6 | /// Invoke the given event handler once and then unsubscribe it. 7 | /// Use with abstract events. Otherwise, use the extension method as it is more efficient. 8 | /// 9 | /// 10 | /// 11 | /// EventUtils.ListenOnce((_, _) => WCLogger.Log("Resubscribed")), 12 | /// h => this.Subscriber.Resubscribed += h, 13 | /// h => this.Subscriber.Resubscribed -= h 14 | /// ); 15 | /// 16 | /// 17 | public static Action ListenOnce( 18 | EventHandler handler, 19 | Action subscribe, 20 | Action unsubscribe) 21 | { 22 | EventHandler internalHandler = null; 23 | internalHandler = (sender, args) => 24 | { 25 | unsubscribe(internalHandler); 26 | handler(sender, args); 27 | }; 28 | 29 | subscribe(internalHandler); 30 | 31 | return () => unsubscribe(internalHandler); 32 | } 33 | 34 | /// 35 | /// Invoke the given event handler once and then unsubscribe it. 36 | /// Use with abstract events. Otherwise, use the extension method as it is more efficient. 37 | /// 38 | public static Action ListenOnce( 39 | EventHandler handler, 40 | Action> subscribe, 41 | Action> unsubscribe) 42 | { 43 | EventHandler internalHandler = null; 44 | internalHandler = (sender, args) => 45 | { 46 | unsubscribe(internalHandler); 47 | handler(sender, args); 48 | }; 49 | 50 | subscribe(internalHandler); 51 | 52 | return () => unsubscribe(internalHandler); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Utils/HashUtils.cs: -------------------------------------------------------------------------------- 1 | using System.Security.Cryptography; 2 | using System.Text; 3 | 4 | namespace WalletConnectSharp.Common.Utils 5 | { 6 | /// 7 | /// Helper class for generating hashes 8 | /// 9 | public static class HashUtils 10 | { 11 | /// 12 | /// Generate a SHA256 hash of the given message 13 | /// 14 | /// The message to hash 15 | /// A SHA256 hash of the given message 16 | public static string HashMessage(string message) 17 | { 18 | using (SHA256 sha256 = SHA256.Create()) 19 | { 20 | byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(message)); 21 | 22 | return bytes.ToHex(); 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Utils/ListComparer.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Common.Utils 2 | { 3 | public class ListComparer : IEqualityComparer> 4 | { 5 | private IEqualityComparer valueComparer; 6 | public ListComparer(IEqualityComparer valueComparer = null) 7 | { 8 | this.valueComparer = valueComparer ?? EqualityComparer.Default; 9 | } 10 | 11 | public bool Equals(List x, List y) 12 | { 13 | return x.SetEquals(y, valueComparer); 14 | } 15 | 16 | public int GetHashCode(List obj) 17 | { 18 | throw new NotImplementedException(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Utils/RpcPayloadId.cs: -------------------------------------------------------------------------------- 1 | using System.Security.Cryptography; 2 | using System.Text; 3 | using Newtonsoft.Json; 4 | 5 | namespace WalletConnectSharp.Common.Utils; 6 | 7 | /// 8 | /// A static class that can generate random JSONRPC ids using the current time as a source of randomness 9 | /// 10 | public static class RpcPayloadId 11 | { 12 | private static readonly Random rng = new Random(); 13 | private static readonly DateTime UnixEpoch = 14 | new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); 15 | 16 | /// 17 | /// Generate a new random JSON-RPC id. The clock is used as a source of randomness 18 | /// 19 | /// A random JSON-RPC id 20 | public static long Generate() 21 | { 22 | var date = (long)((DateTime.UtcNow - UnixEpoch).TotalMilliseconds) * (10L * 10L * 10L); 23 | var extra = (long)Math.Floor(rng.NextDouble() * (10.0 * 10.0 * 10.0)); 24 | return date + extra; 25 | } 26 | 27 | public static long GenerateFromDataHash(object data) 28 | { 29 | using var sha256 = SHA256.Create(); 30 | var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data))); 31 | 32 | // Take the first 6 bytes to stay within JavaScript's safe integer range 33 | long id = 0; 34 | for (var i = 0; i < 6; i++) 35 | { 36 | id = (id << 8) | hash[i]; 37 | } 38 | 39 | // Ensure the id is positive 40 | id &= 0x7FFFFFFFFFFFFF; 41 | 42 | return id; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Utils/TypeSafety.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Common.Utils; 4 | 5 | public static class TypeSafety 6 | { 7 | private static JsonSerializerSettings Settings = 8 | new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }; 9 | 10 | public static void EnsureTypeSerializerSafe(T testObject) 11 | { 12 | // unwrapping and rewrapping the object 13 | // to / from JSON should tell us 14 | // if it's serializer safe, since 15 | // we are using the serializer to test 16 | UnsafeJsonRewrap(testObject, Settings); 17 | } 18 | 19 | public static TR UnsafeJsonRewrap(this T source, JsonSerializerSettings settings = null) 20 | { 21 | var json = settings == null ? 22 | JsonConvert.SerializeObject(source) : 23 | JsonConvert.SerializeObject(source, settings); 24 | 25 | return JsonConvert.DeserializeObject(json); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/Utils/UrlUtils.cs: -------------------------------------------------------------------------------- 1 | using System.Net; 2 | using System.Text; 3 | using System.Text.RegularExpressions; 4 | 5 | namespace WalletConnectSharp.Common.Utils 6 | { 7 | /// 8 | /// A helper class for URLs 9 | /// 10 | public static class UrlUtils 11 | { 12 | /// 13 | /// Parse query strings encoded parameters and return a dictionary 14 | /// 15 | /// The query string to parse 16 | public static Dictionary ParseQs(string queryString) 17 | { 18 | return Regex 19 | .Matches(queryString, "([^?=&]+)(=([^&]*))?", RegexOptions.None, TimeSpan.FromMilliseconds(100)) 20 | .ToDictionary(x => x.Groups[1].Value, x => x.Groups[3].Value); 21 | } 22 | 23 | /// 24 | /// Convert a dictionary to a query string 25 | /// 26 | /// A dictionary to convert to a query string 27 | /// A query string containing all parameters from the dictionary 28 | public static string StringifyQs(Dictionary @params) 29 | { 30 | var sb = new StringBuilder(); 31 | foreach (var kvp in @params) 32 | { 33 | if (kvp.Value == null) 34 | { 35 | continue; 36 | } 37 | 38 | sb.Append(sb.Length == 0 ? "?" : "&"); 39 | sb.Append($"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}"); 40 | } 41 | 42 | return sb.ToString(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Common/WalletConnectSharp.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(DefaultTargetFrameworks) 5 | $(DefaultVersion) 6 | $(DefaultVersion) 7 | $(DefaultVersion) 8 | WalletConnect.Common 9 | WalletConnectSharp.Common 10 | pedrouid, gigajuwels, edkek 11 | A port of the TypeScript SDK to C#. A complete implementation of the WalletConnect v2 protocol that can be used to connect to external wallets or connect a wallet to an external Dapp 12 | Copyright (c) WalletConnect 2023 13 | https://walletconnect.org/ 14 | icon.png 15 | https://github.com/WalletConnect/WalletConnectSharp 16 | git 17 | walletconnect wallet web3 ether ethereum blockchain evm 18 | true 19 | Apache-2.0 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Encoder/ArrayHelpers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | using System.Linq; 4 | 5 | namespace Merkator.Tools 6 | { 7 | internal class ArrayHelpers 8 | { 9 | public static T[] ConcatArrays(params T[][] arrays) 10 | { 11 | Contract.Requires(arrays != null); 12 | Contract.Requires(Contract.ForAll(arrays, (arr) => arr != null)); 13 | Contract.Ensures(Contract.Result() != null); 14 | Contract.Ensures(Contract.Result().Length == arrays.Sum(arr => arr.Length)); 15 | 16 | var result = new T[arrays.Sum(arr => arr.Length)]; 17 | int offset = 0; 18 | for (int i = 0; i < arrays.Length; i++) 19 | { 20 | var arr = arrays[i]; 21 | Buffer.BlockCopy(arr, 0, result, offset, arr.Length); 22 | offset += arr.Length; 23 | } 24 | return result; 25 | } 26 | 27 | public static T[] ConcatArrays(T[] arr1, T[] arr2) 28 | { 29 | Contract.Requires(arr1 != null); 30 | Contract.Requires(arr2 != null); 31 | Contract.Ensures(Contract.Result() != null); 32 | Contract.Ensures(Contract.Result().Length == arr1.Length + arr2.Length); 33 | 34 | var result = new T[arr1.Length + arr2.Length]; 35 | Buffer.BlockCopy(arr1, 0, result, 0, arr1.Length); 36 | Buffer.BlockCopy(arr2, 0, result, arr1.Length, arr2.Length); 37 | return result; 38 | } 39 | 40 | public static T[] SubArray(T[] arr, int start, int length) 41 | { 42 | Contract.Requires(arr != null); 43 | Contract.Requires(start >= 0); 44 | Contract.Requires(length >= 0); 45 | Contract.Requires(start + length <= arr.Length); 46 | Contract.Ensures(Contract.Result() != null); 47 | Contract.Ensures(Contract.Result().Length == length); 48 | 49 | var result = new T[length]; 50 | Buffer.BlockCopy(arr, start, result, 0, length); 51 | return result; 52 | } 53 | 54 | public static T[] SubArray(T[] arr, int start) 55 | { 56 | Contract.Requires(arr != null); 57 | Contract.Requires(start >= 0); 58 | Contract.Requires(start <= arr.Length); 59 | Contract.Ensures(Contract.Result() != null); 60 | Contract.Ensures(Contract.Result().Length == arr.Length - start); 61 | 62 | return SubArray(arr, start, arr.Length - start); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Encoder/Bases.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Crypto.Encoder 2 | { 3 | internal static class Bases 4 | { 5 | public static Codec Base10 = new Codec( 6 | "base10", 7 | "9", 8 | new BaseX( 9 | "0123456789", 10 | "base10" 11 | ) 12 | ); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Encoder/Codec.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace WalletConnectSharp.Crypto.Encoder 4 | { 5 | internal class Codec 6 | { 7 | public string Name { get; } 8 | public string Prefix { get; } 9 | public Func Encoder { get; } 10 | public Func Decoder { get; } 11 | 12 | public Codec(string name, string prefix, BaseX baseX) : this(name, prefix, baseX.Encode, baseX.Decode) 13 | { 14 | 15 | } 16 | 17 | public Codec(string name, string prefix, Func encoder, Func decoder) 18 | { 19 | this.Name = name; 20 | this.Prefix = prefix; 21 | this.Encoder = encoder; 22 | this.Decoder = decoder; 23 | } 24 | 25 | public string Encode(byte[] bytes) 26 | { 27 | return $"{Encoder(bytes)}"; 28 | } 29 | 30 | public byte[] Decode(string source) 31 | { 32 | if (source[0] != Prefix[0]) 33 | { 34 | source = Prefix + source; 35 | } 36 | 37 | return Decoder(source.Substring(1)); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Models/DecodeOptions.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Crypto.Models 4 | { 5 | /// 6 | /// Class representing options for decoding data 7 | /// 8 | public class DecodeOptions 9 | { 10 | /// 11 | /// The public key that received this encoded message 12 | /// 13 | [JsonProperty("receiverPublicKey")] 14 | public string ReceiverPublicKey; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Models/EncodeOptions.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Crypto.Models 4 | { 5 | /// 6 | /// A class representing the options for encoding 7 | /// 8 | public class EncodeOptions 9 | { 10 | /// 11 | /// The envelope type to use 12 | /// 13 | [JsonProperty("type")] 14 | public int Type; 15 | 16 | /// 17 | /// The public key that is sending the encoded message 18 | /// 19 | [JsonProperty("senderPublicKey")] 20 | public string SenderPublicKey; 21 | 22 | /// 23 | /// The public key that is receiving the encoded message 24 | /// 25 | [JsonProperty("receiverPublicKey")] 26 | public string ReceiverPublicKey; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Models/EncodingParams.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Crypto.Models 4 | { 5 | /// 6 | /// A class representing the parameters of an encoded message, all parameters 7 | /// are raw byte encoded 8 | /// 9 | public class EncodingParams 10 | { 11 | /// 12 | /// The envelope type as raw bytes 13 | /// 14 | [JsonProperty("type")] 15 | public byte[] Type; 16 | 17 | /// 18 | /// The sealed encoded message as raw bytes 19 | /// 20 | [JsonProperty("sealed")] 21 | public byte[] Sealed; 22 | 23 | /// 24 | /// The IV of the encoded message as raw bytes 25 | /// 26 | [JsonProperty("iv")] 27 | public byte[] Iv; 28 | 29 | /// 30 | /// The public key of the sender as raw bytes 31 | /// 32 | [JsonProperty("senderPublicKey")] 33 | public byte[] SenderPublicKey; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Models/EncodingValidation.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Crypto.Models 4 | { 5 | /// 6 | /// A class representing the encoding parameters to validate 7 | /// 8 | public class EncodingValidation 9 | { 10 | /// 11 | /// The envelope type to validate 12 | /// 13 | [JsonProperty("type")] 14 | public int Type; 15 | 16 | /// 17 | /// The sender public key to validate 18 | /// 19 | [JsonProperty("senderPublicKey")] 20 | public string SenderPublicKey; 21 | 22 | /// 23 | /// The receiver public key to validate 24 | /// 25 | [JsonProperty("receiverPublicKey")] 26 | public string ReceiverPublicKey; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Models/EncryptParams.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Crypto.Models 4 | { 5 | /// 6 | /// A class representing the encrypt parameters 7 | /// 8 | public class EncryptParams 9 | { 10 | /// 11 | /// The message to encrypt 12 | /// 13 | [JsonProperty("message")] 14 | public string Message; 15 | 16 | /// 17 | /// The Sym key to use for encrypting 18 | /// 19 | [JsonProperty("symKey")] 20 | public string SymKey; 21 | 22 | /// 23 | /// The envelope type to use when encrypting 24 | /// 25 | [JsonProperty("type")] 26 | public int Type; 27 | 28 | /// 29 | /// The IV to use for the encryption 30 | /// 31 | [JsonProperty("iv")] 32 | public string Iv; 33 | 34 | /// 35 | /// The public key of the sender of this encrypted message 36 | /// 37 | [JsonProperty("senderPublicKey")] 38 | public string SenderPublicKey; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Models/IridiumJWTData.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Crypto.Models 4 | { 5 | /// 6 | /// Iridium JWT data that is encoded when signing JWT tokens 7 | /// 8 | public class IridiumJWTData 9 | { 10 | /// 11 | /// The Iridium JWT header data 12 | /// 13 | [JsonProperty("header")] 14 | public IridiumJWTHeader Header; 15 | 16 | /// 17 | /// The Iridium JWT payload 18 | /// 19 | [JsonProperty("payload")] 20 | public IridiumJWTPayload Payload; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Models/IridiumJWTDecoded.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Crypto.Models 4 | { 5 | 6 | public class IridiumJWTDecoded : IridiumJWTSigned 7 | { 8 | [JsonProperty("data")] 9 | public byte[] Data; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Models/IridiumJWTHeader.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Crypto.Models 4 | { 5 | /// 6 | /// The header data for an Iridium JWT header 7 | /// 8 | [Serializable] 9 | public class IridiumJWTHeader 10 | { 11 | /// 12 | /// The default header to use 13 | /// 14 | public static readonly IridiumJWTHeader DEFAULT = new IridiumJWTHeader() 15 | { 16 | Alg = "EdDSA", 17 | Typ = "JWT" 18 | }; 19 | 20 | /// 21 | /// The encoding algorithm to use 22 | /// 23 | [JsonProperty("alg")] public string Alg; 24 | 25 | /// 26 | /// The encoding type to use 27 | /// 28 | [JsonProperty("typ")] public string Typ; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Models/IridiumJWTPayload.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Crypto.Models 4 | { 5 | /// 6 | /// The data for an Iridium JWT payload 7 | /// 8 | [Serializable] 9 | public class IridiumJWTPayload 10 | { 11 | /// 12 | /// The iss value 13 | /// 14 | [JsonProperty("iss")] public string Iss; 15 | 16 | /// 17 | /// The sub value 18 | /// 19 | [JsonProperty("sub")] public string Sub; 20 | 21 | /// 22 | /// The aud value 23 | /// 24 | [JsonProperty("aud")] public string Aud; 25 | 26 | /// 27 | /// The iat value 28 | /// 29 | [JsonProperty("iat")] public long Iat; 30 | 31 | /// 32 | /// The exp value 33 | /// 34 | [JsonProperty("exp")] public long Exp; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/Models/IridiumJWTSigned.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Crypto.Models 4 | { 5 | /// 6 | /// Represents a signed Iridium JWT token 7 | /// 8 | public class IridiumJWTSigned : IridiumJWTData 9 | { 10 | [JsonProperty("signature")] 11 | public byte[] Signature; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Crypto/WalletConnectSharp.Crypto.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(DefaultTargetFrameworks) 5 | $(DefaultVersion) 6 | $(DefaultVersion) 7 | $(DefaultVersion) 8 | WalletConnect.Crypto 9 | WalletConnectSharp.Crypto 10 | pedrouid, gigajuwels, edkek 11 | A port of the TypeScript SDK to C#. A complete implementation of the WalletConnect v2 protocol that can be used to connect to external wallets or connect a wallet to an external Dapp 12 | Copyright (c) WalletConnect 2023 13 | https://walletconnect.org/ 14 | icon.png 15 | https://github.com/WalletConnect/WalletConnectSharp 16 | git 17 | walletconnect wallet web3 ether ethereum blockchain evm 18 | true 19 | Apache-2.0 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network.Websocket/WalletConnectSharp.Network.Websocket.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(DefaultTargetFrameworks) 5 | $(DefaultVersion) 6 | $(DefaultVersion) 7 | $(DefaultVersion) 8 | WalletConnect.Network.Websocket 9 | WalletConnectSharp.Network.Websocket 10 | pedrouid, gigajuwels, edkek 11 | A port of the TypeScript SDK to C#. A complete implementation of the WalletConnect v2 protocol that can be used to connect to external wallets or connect a wallet to an external Dapp 12 | Copyright (c) WalletConnect 2023 13 | https://walletconnect.org/ 14 | icon.png 15 | https://github.com/WalletConnect/WalletConnectSharp 16 | git 17 | walletconnect wallet web3 ether ethereum blockchain evm 18 | true 19 | Apache-2.0 20 | 21 | 22 | 23 | TRACE 24 | WC_DEF_WEBSOCKET 25 | 26 | 27 | 28 | TRACE 29 | WC_DEF_WEBSOCKET 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network.Websocket/WebsocketConnectionBuilder.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Network.Interfaces; 2 | 3 | namespace WalletConnectSharp.Network.Websocket 4 | { 5 | public class WebsocketConnectionBuilder : IConnectionBuilder 6 | { 7 | public Task CreateConnection(string url) 8 | { 9 | return Task.FromResult(new WebsocketConnection(url)); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Interfaces/IBaseJsonRpcProvider.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace WalletConnectSharp.Network 3 | { 4 | /// 5 | /// Represents a base interface for JsonRpcProvider 6 | /// 7 | public interface IBaseJsonRpcProvider 8 | { 9 | /// 10 | /// Gets the current IJsonRpcConnection this provider is using 11 | /// 12 | IJsonRpcConnection Connection { get; } 13 | 14 | /// 15 | /// Connect this provider using already defined connection parameters. 16 | /// 17 | /// A task that is establishing the connection 18 | Task Connect(); 19 | 20 | /// 21 | /// Disconnect this provider directly 22 | /// 23 | /// A task that is ending the connection 24 | Task Disconnect(); 25 | 26 | /// 27 | /// Send a Json RPC request with a parameter field of type T, and decode a response with the type of TR. 28 | /// 29 | /// The json rpc request to send 30 | /// The current context 31 | /// The type of the parameter field in the json rpc request 32 | /// The type of the parameter field in the json rpc response 33 | /// The decoded response for the request 34 | Task Request(IRequestArguments requestArgs, object context = null); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Interfaces/IConnectionBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Network.Interfaces 2 | { 3 | public interface IConnectionBuilder 4 | { 5 | Task CreateConnection(string url); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Interfaces/IJsonRpcError.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Network.Models; 3 | 4 | namespace WalletConnectSharp.Network 5 | { 6 | /// 7 | /// A JSON RPC response that may include an error 8 | /// 9 | public interface IJsonRpcError : IJsonRpcPayload 10 | { 11 | /// 12 | /// The error for this JSON RPC response or null if no error is present 13 | /// 14 | Error Error { get; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Interfaces/IJsonRpcPayload.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Network 4 | { 5 | /// 6 | /// An interface describing the fields every JSON RPC request/response/error must inlude 7 | /// 8 | public interface IJsonRpcPayload 9 | { 10 | /// 11 | /// The unique id for this JSON RPC payload 12 | /// 13 | long Id { get; } 14 | 15 | /// 16 | /// The version of this JSON RPC payload 17 | /// 18 | string JsonRPC { get; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Interfaces/IJsonRpcProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using WalletConnectSharp.Common; 4 | using WalletConnectSharp.Network.Models; 5 | 6 | namespace WalletConnectSharp.Network 7 | { 8 | /// 9 | /// The interface that represents a JSON RPC provider 10 | /// 11 | public interface IJsonRpcProvider : IBaseJsonRpcProvider, IModule 12 | { 13 | event EventHandler PayloadReceived; 14 | 15 | event EventHandler Connected; 16 | 17 | event EventHandler Disconnected; 18 | 19 | event EventHandler ErrorReceived; 20 | 21 | event EventHandler RawMessageReceived; 22 | 23 | /// 24 | /// Connect this provider to the given URL 25 | /// 26 | /// The URL to connect to 27 | /// A task that is establishing the connection 28 | Task Connect(string connection); 29 | 30 | /// 31 | /// Connect this provider using the providing connection object 32 | /// 33 | /// The connection object this provider should use 34 | /// A task that is establishing the connection 35 | Task Connect(IJsonRpcConnection connection); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Interfaces/IJsonRpcRequest.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Network 4 | { 5 | /// 6 | /// An interface that describes a JSON RPC request with the given parameter type T 7 | /// 8 | /// The type of the parameter in the JSON RPC request 9 | public interface IJsonRpcRequest : IRequestArguments, IJsonRpcPayload { } 10 | } -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Interfaces/IJsonRpcResult.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Network 4 | { 5 | /// 6 | /// An interface that represents a JSON RPC response to a JSON RPC request with the given result 7 | /// type of T. This interface also includes an error field if the JSON RPC response is an error 8 | /// 9 | /// The type of the response field in this JSON RPC response 10 | public interface IJsonRpcResult : IJsonRpcError 11 | { 12 | /// 13 | /// The result data of the response to the request 14 | /// 15 | T Result { get; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Interfaces/IRelayUrlBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Network 2 | { 3 | public interface IRelayUrlBuilder 4 | { 5 | public string FormatRelayRpcUrl(string relayUrl, string protocol, string version, string projectId, 6 | string auth); 7 | 8 | public string BuildUserAgent(string protocol, string version); 9 | 10 | public (string name, string version) GetOsInfo(); 11 | 12 | public (string name, string version) GetSdkInfo(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Interfaces/IRequestArguments.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Network 4 | { 5 | /// 6 | /// An interface that represents a generic request with the parameter type of T 7 | /// 8 | /// The type of the parameter for this request 9 | public interface IRequestArguments 10 | { 11 | /// 12 | /// The method for this request 13 | /// 14 | string Method { get; } 15 | 16 | /// 17 | /// The parameter for this request 18 | /// 19 | T Params { get; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Models/BaseJsonRpcRequest.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Network.Models 2 | { 3 | /// 4 | /// A base class for creating custom JSON RPC request objects with a given parameter type T 5 | /// 6 | /// The parameter type for this JSON RPC request 7 | public abstract class BaseJsonRpcRequest : IRequestArguments 8 | { 9 | /// 10 | /// The method for this JSON RPC Request 11 | /// 12 | public abstract string Method { get; } 13 | 14 | /// 15 | /// The parameters for this JSON RPC request 16 | /// 17 | public T Params { get; protected set; } 18 | 19 | protected BaseJsonRpcRequest() 20 | { 21 | } 22 | 23 | protected BaseJsonRpcRequest(T @params) 24 | { 25 | Params = @params; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Models/JsonRpcError.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Network.Models 4 | { 5 | /// 6 | /// A Json RPC response containing an error 7 | /// 8 | public class JsonRpcError : IJsonRpcError 9 | { 10 | /// 11 | /// The id field 12 | /// 13 | [JsonProperty("id")] 14 | private long _id; 15 | 16 | [JsonIgnore] 17 | public long Id => _id; 18 | 19 | [JsonProperty("jsonrpc")] 20 | private string _jsonRpc = "2.0"; 21 | 22 | /// 23 | /// The jsonrpc field 24 | /// 25 | [JsonIgnore] 26 | public string JsonRPC 27 | { 28 | get 29 | { 30 | return _jsonRpc; 31 | } 32 | } 33 | 34 | 35 | [JsonProperty("error", NullValueHandling = NullValueHandling.Ignore)] 36 | private Error _error; 37 | 38 | /// 39 | /// The error field 40 | /// 41 | [JsonIgnore] 42 | public Error Error => _error; 43 | 44 | /// 45 | /// Create a blank JSON rpc error response 46 | /// 47 | public JsonRpcError() 48 | { 49 | } 50 | 51 | /// 52 | /// Create a JSON rpc error response with the given id and ErrorResponse 53 | /// 54 | /// The id of the response 55 | /// The error value 56 | public JsonRpcError(long id, Error error) 57 | { 58 | _id = id; 59 | _error = error; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Models/RequestArguments.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Network.Models 4 | { 5 | /// 6 | /// A class that holds request arguments that can be 7 | /// placed inside a Json RPC request. Does not represent 8 | /// a full Json RPC request 9 | /// 10 | /// The type of the parameter in this request 11 | public class RequestArguments : IRequestArguments 12 | { 13 | [JsonProperty("method")] 14 | private string _method; 15 | 16 | [JsonProperty("params")] 17 | private T _params; 18 | 19 | /// 20 | /// The method to use 21 | /// 22 | [JsonIgnore] 23 | public string Method 24 | { 25 | get => _method; 26 | set => _method = value; 27 | } 28 | 29 | /// 30 | /// The method for this request 31 | /// 32 | [JsonIgnore] 33 | public T Params 34 | { 35 | get => _params; 36 | set => _params = value; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Models/RpcOptionsAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace WalletConnectSharp.Network.Models 5 | { 6 | /// 7 | /// An attribute that defines the RPC options for this class. These options 8 | /// are used when this class type is used as a parameter or result of a Json RPC 9 | /// request/response. This cannot be used directly, use either 10 | /// * RpcRequestOptions 11 | /// * RpcResponseOptions 12 | /// 13 | public abstract class RpcOptionsAttribute : Attribute 14 | { 15 | /// 16 | /// The TTL (time to live) 17 | /// 18 | public long TTL { get; } 19 | 20 | /// 21 | /// The Tag 22 | /// 23 | public int Tag { get; } 24 | 25 | /// 26 | /// Create a new RpcOptions attribute with the given ttl and tag parameters 27 | /// 28 | /// Time to live 29 | /// The tag 30 | protected RpcOptionsAttribute(long ttl, int tag) 31 | { 32 | TTL = ttl; 33 | Tag = tag; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Models/RpcRequestOptionsAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace WalletConnectSharp.Network.Models 4 | { 5 | /// 6 | /// An attribute that defines the RPC options for this class (or struct). These options 7 | /// are used when this class type is used as a parameter of a Json RPC 8 | /// request. 9 | /// 10 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] 11 | public class RpcRequestOptionsAttribute : RpcOptionsAttribute 12 | { 13 | /// 14 | /// Returns the first RpcOptionsAttribute found on the given type T. 15 | /// If no attribute is found, then null is returned 16 | /// 17 | /// The type to inspect for RpcOptionsAttribute 18 | /// The first RpcOptionsAttribute found on the given type T 19 | public static RpcRequestOptionsAttribute GetOptionsForType() 20 | { 21 | return GetOptionsForType(typeof(T)); 22 | } 23 | 24 | /// 25 | /// Returns the first RpcOptionsAttribute found on the given type t. 26 | /// If no attribute is found, then null is returned 27 | /// 28 | /// The type to inspect for RpcOptionsAttribute 29 | /// The first RpcOptionsAttribute found on the given type t 30 | public static RpcRequestOptionsAttribute GetOptionsForType(Type t) 31 | { 32 | var attribute = t.GetCustomAttributes(typeof(RpcRequestOptionsAttribute), true).Cast().FirstOrDefault(); 33 | 34 | return attribute; 35 | } 36 | 37 | public RpcRequestOptionsAttribute(long ttl, int tag) : base(ttl, tag) 38 | { 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Models/RpcResponseOptionsAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace WalletConnectSharp.Network.Models 4 | { 5 | /// 6 | /// An attribute that defines the RPC options for this class (or struct). These options 7 | /// are used when this class type is used as a result of a Json RPC 8 | /// response. 9 | /// 10 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] 11 | public class RpcResponseOptionsAttribute : RpcOptionsAttribute 12 | { 13 | /// 14 | /// Returns the first RpcOptionsAttribute found on the given type T. 15 | /// If no attribute is found, then null is returned 16 | /// 17 | /// The type to inspect for RpcOptionsAttribute 18 | /// The first RpcOptionsAttribute found on the given type T 19 | public static RpcResponseOptionsAttribute GetOptionsForType() 20 | { 21 | return GetOptionsForType(typeof(T)); 22 | } 23 | 24 | /// 25 | /// Returns the first RpcOptionsAttribute found on the given type t. 26 | /// If no attribute is found, then null is returned 27 | /// 28 | /// The type to inspect for RpcOptionsAttribute 29 | /// The first RpcOptionsAttribute found on the given type t 30 | public static RpcResponseOptionsAttribute GetOptionsForType(Type t) 31 | { 32 | var attribute = t.GetCustomAttributes(typeof(RpcResponseOptionsAttribute), true).Cast().FirstOrDefault(); 33 | 34 | return attribute; 35 | } 36 | 37 | public RpcResponseOptionsAttribute(long ttl, int tag) : base(ttl, tag) 38 | { 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/Utils/RelayUrlBuilder.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Common; 2 | using WalletConnectSharp.Common.Utils; 3 | 4 | namespace WalletConnectSharp.Network; 5 | 6 | public class RelayUrlBuilder : IRelayUrlBuilder 7 | { 8 | public virtual string FormatRelayRpcUrl(string relayUrl, string protocol, string version, string projectId, 9 | string auth) 10 | { 11 | var splitUrl = relayUrl.Split("?"); 12 | var ua = BuildUserAgent(protocol, version); 13 | 14 | var currentParameters = UrlUtils.ParseQs(splitUrl.Length > 1 ? splitUrl[1] : ""); 15 | 16 | currentParameters.Add("auth", auth); 17 | currentParameters.Add("projectId", projectId); 18 | currentParameters.Add("ua", ua); 19 | 20 | var hasOrigin = TryGetOrigin(out var origin); 21 | if (hasOrigin) 22 | currentParameters.Add("origin", origin); 23 | 24 | var formattedParameters = UrlUtils.StringifyQs(currentParameters); 25 | 26 | return splitUrl[0] + formattedParameters; 27 | } 28 | 29 | public virtual string BuildUserAgent(string protocol, string version) 30 | { 31 | var (os, osVersion) = GetOsInfo(); 32 | var (sdkName, sdkVersion) = GetSdkInfo(); 33 | 34 | return $"{protocol}-{version}/{sdkName}-{sdkVersion}/{os}-{osVersion}"; 35 | } 36 | 37 | public virtual (string name, string version) GetOsInfo() 38 | { 39 | var name = Environment.OSVersion.Platform.ToString().ToLowerInvariant(); 40 | var version = Environment.OSVersion.Version.ToString().ToLowerInvariant(); 41 | return (name, version); 42 | } 43 | 44 | public virtual (string name, string version) GetSdkInfo() 45 | { 46 | return ("csharp", SDKConstants.SDK_VERSION); 47 | } 48 | 49 | protected virtual bool TryGetOrigin(out string origin) 50 | { 51 | origin = null; 52 | return false; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Network/WalletConnectSharp.Network.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(DefaultTargetFrameworks) 5 | $(DefaultVersion) 6 | $(DefaultVersion) 7 | $(DefaultVersion) 8 | WalletConnect.Network 9 | WalletConnectSharp.Network 10 | pedrouid, gigajuwels, edkek 11 | A port of the TypeScript SDK to C#. A complete implementation of the WalletConnect v2 protocol that can be used to connect to external wallets or connect a wallet to an external Dapp 12 | Copyright (c) WalletConnect 2023 13 | https://walletconnect.org/ 14 | icon.png 15 | https://github.com/WalletConnect/WalletConnectSharp 16 | git 17 | walletconnect wallet web3 ether ethereum blockchain evm 18 | true 19 | Apache-2.0 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Core Modules/WalletConnectSharp.Storage/WalletConnectSharp.Storage.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(DefaultTargetFrameworks) 5 | $(DefaultVersion) 6 | $(DefaultVersion) 7 | $(DefaultVersion) 8 | true 9 | WalletConnect.Storage 10 | WalletConnectSharp.Storage 11 | pedrouid, gigajuwels, edkek 12 | A port of the TypeScript SDK to C#. A complete implementation of the WalletConnect v2 protocol that can be used to connect to external wallets or connect a wallet to an external Dapp 13 | Copyright (c) WalletConnect 2023 14 | https://walletconnect.org/ 15 | icon.png 16 | https://github.com/WalletConnect/WalletConnectSharp 17 | git 18 | walletconnect wallet web3 ether ethereum blockchain evm 19 | Apache-2.0 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 2.4.3 5 | net6.0;net7.0;net8.0;netstandard2.1; 6 | true 7 | 8 | 9 | latest 10 | true 11 | enable 12 | 13 | 14 | 15 | $(DefaultVersion) 16 | $(DefaultVersion) 17 | $(DefaultVersion) 18 | 19 | -------------------------------------------------------------------------------- /Directory.Packages.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | true 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | all 15 | runtime; build; native; contentfiles; analyzers; buildtransitive 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deprecated - WalletConnectSharp 2 | 3 | WalletConnect Inc is now Reown. As part of this transition, we are deprecating a number of repositories/packages across our supported platforms, and transitioning to their equivalents published under the Reown organization. 4 | 5 | This repository is now considered deprecated and will reach End-of-Life on February 17th 2025. For more details, including migration guides please see: https://docs.reown.com/advanced/walletconnect-deprecations 6 | 7 | --- 8 | 9 | WalletConnectSharp is an implementation of the [WalletConnect](https://walletconnect.org/) protocol v2 using .NET. This library implements the [WalletConnect Technical Specification](https://docs.walletconnect.org/tech-spec) in .NET to allow C# dApps makers and wallet makers to add support for the open [WalletConnect](https://walletconnect.org/) protocol. 10 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Auth.Tests/SignatureTest.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Auth.Internals; 2 | using WalletConnectSharp.Auth.Models; 3 | using WalletConnectSharp.Tests.Common; 4 | using Xunit; 5 | 6 | namespace WalletConnectSharp.Auth.Tests; 7 | 8 | public class SignatureTest 9 | { 10 | public const string ChainId = "eip155:1"; 11 | public const string Address = "0x2faf83c542b68f1b4cdc0e770e8cb9f567b08f71"; 12 | 13 | private readonly string _projectId = TestValues.TestProjectId; 14 | 15 | private readonly string _reconstructedMessage = @"localhost wants you to sign in with your Ethereum account: 16 | 0x2faf83c542b68f1b4cdc0e770e8cb9f567b08f71 17 | 18 | URI: http://localhost:3000/ 19 | Version: 1 20 | Chain ID: 1 21 | Nonce: 1665443015700 22 | Issued At: 2022-10-10T23:03:35.700Z 23 | Expiration Time: 2022-10-11T23:03:35.700Z".Replace("\r", ""); 24 | 25 | [Fact] [Trait("Category", "integration")] 26 | public async Task TestValidEip1271Signature() 27 | { 28 | var signature = new Cacao.CacaoSignature.EIP1271CacaoSignature( 29 | "0xc1505719b2504095116db01baaf276361efd3a73c28cf8cc28dabefa945b8d536011289ac0a3b048600c1e692ff173ca944246cf7ceb319ac2262d27b395c82b1c"); 30 | 31 | var isValid = 32 | await SignatureUtils.VerifySignature(Address, _reconstructedMessage, signature, ChainId, _projectId); 33 | 34 | Assert.True(isValid); 35 | } 36 | 37 | [Fact] [Trait("Category", "integration")] 38 | public async Task TestBadEip1271Signature() 39 | { 40 | var signature = new Cacao.CacaoSignature.EIP1271CacaoSignature( 41 | "0xdead5719b2504095116db01baaf276361efd3a73c28cf8cc28dabefa945b8d536011289ac0a3b048600c1e692ff173ca944246cf7ceb319ac2262d27b395c82b1c"); 42 | 43 | var isValid = 44 | await SignatureUtils.VerifySignature(Address, _reconstructedMessage, signature, ChainId, _projectId); 45 | 46 | Assert.False(isValid); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Auth.Tests/WalletConnectSharp.Auth.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | 2.0.0 6 | 2.0.0 7 | 2.0.0 8 | 9 | 10 | 11 | 12 | 13 | 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | all 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Crypto.Tests/CryptoFixture.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using WalletConnectSharp.Storage; 4 | 5 | namespace WalletConnectSharp.Crypto.Tests 6 | { 7 | public class CryptoFixture : IDisposable 8 | { 9 | public Crypto PeerA { get; private set; } 10 | 11 | public Crypto PeerB { get; private set; } 12 | 13 | private TaskCompletionSource _initCompleted = new TaskCompletionSource(); 14 | 15 | public CryptoFixture() 16 | { 17 | PeerA = new Crypto(); 18 | PeerB = new Crypto(); 19 | 20 | Init(); 21 | } 22 | 23 | private async void Init() 24 | { 25 | await Task.WhenAll(PeerA.Init(), PeerB.Init()); 26 | 27 | _initCompleted.SetResult(true); 28 | } 29 | 30 | public Task WaitForModulesReady() 31 | { 32 | return _initCompleted.Task; 33 | } 34 | 35 | public void Dispose() 36 | { 37 | PeerA.Storage.Clear(); 38 | PeerB.Storage.Clear(); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Crypto.Tests/CryptoTests.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Common.Model.Relay; 2 | using WalletConnectSharp.Crypto.Models; 3 | using WalletConnectSharp.Crypto.Tests.Models; 4 | using WalletConnectSharp.Network.Models; 5 | using Xunit; 6 | 7 | namespace WalletConnectSharp.Crypto.Tests 8 | { 9 | public class CryptoTests : IClassFixture 10 | { 11 | private CryptoFixture _cryptoFixture; 12 | 13 | public Crypto PeerA 14 | { 15 | get 16 | { 17 | return _cryptoFixture.PeerA; 18 | } 19 | } 20 | 21 | public Crypto PeerB 22 | { 23 | get 24 | { 25 | return _cryptoFixture.PeerB; 26 | } 27 | } 28 | 29 | public CryptoTests(CryptoFixture cryptoFixture) 30 | { 31 | this._cryptoFixture = cryptoFixture; 32 | } 33 | 34 | [Fact, Trait("Category", "unit")] 35 | public async Task TestEncodeDecode() 36 | { 37 | await _cryptoFixture.WaitForModulesReady(); 38 | 39 | var api = RelayProtocols.DefaultProtocol; 40 | var message = new JsonRpcRequest(api.Subscribe, new TopicData() 41 | { 42 | Topic = "test" 43 | }); 44 | 45 | var keyA = await PeerA.GenerateKeyPair(); 46 | var keyB = await PeerB.GenerateKeyPair(); 47 | 48 | Assert.NotEqual(keyA, keyB); 49 | Assert.False(await PeerA.HasKeys(keyB)); 50 | Assert.False(await PeerB.HasKeys(keyA)); 51 | 52 | var symKeyA = await PeerA.GenerateSharedKey(keyA, keyB); 53 | var symKeyB = await PeerB.GenerateSharedKey(keyB, keyA); 54 | 55 | Assert.Equal(symKeyA, symKeyB); 56 | 57 | var encoded = await PeerA.Encode(symKeyA, message); 58 | var decoded = await PeerB.Decode>(symKeyB, encoded); 59 | 60 | Assert.Equal(message.Params.Topic, decoded.Params.Topic); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Crypto.Tests/Models/TopicData.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Crypto.Tests.Models 4 | { 5 | public class TopicData 6 | { 7 | [JsonProperty("topic")] 8 | public string Topic; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Crypto.Tests/WalletConnectSharp.Crypto.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | 2.0.0 6 | 2.0.0 7 | 2.0.0 8 | 9 | 10 | 11 | 12 | 13 | 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | all 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Examples/IExample.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace WalletConnectSharp.Examples 4 | { 5 | public interface IExample 6 | { 7 | string Name { get; } 8 | 9 | Task Execute(string[] args); 10 | } 11 | } -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Examples/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | 5 | namespace WalletConnectSharp.Examples 6 | { 7 | public class Program 8 | { 9 | private static readonly IExample[] Examples = new IExample[] 10 | { 11 | new SimpleExample(), 12 | new BiDirectional() 13 | }; 14 | 15 | private static void ShowHelp() 16 | { 17 | Console.WriteLine("Please specify which example to run"); 18 | foreach (var e in Examples) 19 | { 20 | Console.WriteLine(" - " + e.Name); 21 | } 22 | } 23 | 24 | public static async Task Main(string[] args) 25 | { 26 | if (args.Length == 0) 27 | { 28 | ShowHelp(); 29 | return; 30 | } 31 | 32 | var name = args[0]; 33 | var exampleArgs = args.Skip(1).ToArray(); 34 | 35 | var example = Examples.FirstOrDefault(e => e.Name.ToLower() == name); 36 | 37 | if (example == null) 38 | { 39 | ShowHelp(); 40 | return; 41 | } 42 | 43 | await example.Execute(exampleArgs); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Examples/SimpleExample.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Core; 2 | using WalletConnectSharp.Sign; 3 | using WalletConnectSharp.Sign.Models; 4 | using WalletConnectSharp.Sign.Models.Engine; 5 | 6 | namespace WalletConnectSharp.Examples 7 | { 8 | public class SimpleExample : IExample 9 | { 10 | public string Name 11 | { 12 | get { return "simple_example"; } 13 | } 14 | 15 | public async Task Execute(string[] args) 16 | { 17 | var options = new SignClientOptions() 18 | { 19 | ProjectId = "ef21cf313a63dbf63f2e9e04f3614029", 20 | Metadata = new Metadata() 21 | { 22 | Description = "An example project to showcase WalletConnectSharpv2", 23 | Icons = new[] { "https://walletconnect.com/meta/favicon.ico" }, 24 | Name = "WalletConnectSharpv2 Example", 25 | Url = "https://walletconnect.com" 26 | } 27 | }; 28 | 29 | var client = await WalletConnectSignClient.Init(options); 30 | 31 | var connectData = await client.Connect(new ConnectOptions() 32 | { 33 | RequiredNamespaces = new RequiredNamespaces() 34 | { 35 | { 36 | "eip155", new ProposedNamespace() 37 | { 38 | Methods = new[] 39 | { 40 | "eth_sendTransaction", 41 | "eth_signTransaction", 42 | "eth_sign", 43 | "personal_sign", 44 | "eth_signTypedData", 45 | }, 46 | Chains = new[] 47 | { 48 | "eip155:1" 49 | }, 50 | Events = new[] 51 | { 52 | "chainChanged", "accountsChanged" 53 | } 54 | } 55 | } 56 | } 57 | }); 58 | 59 | Console.WriteLine(connectData.Uri); 60 | 61 | await connectData.Approval; 62 | 63 | Console.WriteLine("Connected"); 64 | 65 | while (true) 66 | { 67 | await Task.Delay(2000); 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Examples/WalletConnectSharp.Examples.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | 2.0.0 6 | 2.0.0 7 | 2.0.0 8 | Exe 9 | false 10 | WalletConnect.Examples 11 | WalletConnectSharp.Examples 12 | pedrouid, gigajuwels, edkek 13 | A port of the TypeScript SDK to C#. A complete implementation of the WalletConnect v2 protocol that can be used to connect to external wallets or connect a wallet to an external Dapp 14 | Copyright (c) WalletConnect 2023 15 | https://walletconnect.org/ 16 | https://github.com/WalletConnect/WalletConnectSharp 17 | git 18 | walletconnect wallet web3 ether ethereum blockchain evm 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Network.Tests/Models/TopicData.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Network.Tests.Models 4 | { 5 | public class TopicData 6 | { 7 | [JsonProperty("topic")] 8 | public string Topic; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Network.Tests/WalletConnectSharp.Network.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | 2.0.0 6 | 2.0.0 7 | 2.0.0 8 | 9 | 10 | 11 | 12 | 13 | 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | all 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Sign.Test/WalletConnectSharp.Sign.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | 2.0.0 6 | 2.0.0 7 | 2.0.0 8 | 9 | 10 | 11 | 12 | 13 | 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | all 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Storage.Test/WalletConnectSharp.Storage.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | false 6 | 7 | net8.0 8 | 9 | 2.0.0 10 | 11 | 2.0.0 12 | 13 | 2.0.0 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Tests.Common/CryptoWalletFixture.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | using NBitcoin; 3 | using Nethereum.HdWallet; 4 | 5 | namespace WalletConnectSharp.Tests.Common 6 | { 7 | public class CryptoWalletFixture 8 | { 9 | private readonly Wallet _wallet; 10 | private readonly string _iss; 11 | 12 | public string WalletAddress 13 | { 14 | get 15 | { 16 | return _wallet.GetAddresses(1)[0]; 17 | } 18 | } 19 | 20 | public Wallet CryptoWallet 21 | { 22 | get 23 | { 24 | return _wallet; 25 | } 26 | } 27 | 28 | public string Iss 29 | { 30 | get 31 | { 32 | return _iss; 33 | } 34 | } 35 | 36 | public CryptoWalletFixture() 37 | { 38 | this._wallet = new Wallet(Wordlist.English, WordCount.Twelve); 39 | this._iss = $"did:pkh:eip155:1:{this.WalletAddress}"; 40 | } 41 | 42 | public Task SignMessage(string message) 43 | { 44 | return _wallet 45 | .GetAccount(WalletAddress) 46 | .AccountSigningService 47 | .PersonalSign 48 | .SendRequestAsync( 49 | Encoding.UTF8.GetBytes(message) 50 | ); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Tests.Common/TempFolder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | namespace WalletConnectSharp.Tests.Common 5 | { 6 | public class TempFolder : IDisposable 7 | { 8 | private static readonly Random _Random = new Random(); 9 | 10 | public DirectoryInfo Folder { get; } 11 | 12 | public TempFolder(string prefix = "TempFolder") 13 | { 14 | string folderName; 15 | 16 | lock (_Random) 17 | { 18 | folderName = prefix + _Random.Next(1000000000); 19 | } 20 | 21 | Folder = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), folderName)); 22 | } 23 | 24 | public void Dispose() 25 | { 26 | Directory.Delete(Folder.FullName, true); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Tests.Common/TestValues.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Tests.Common 2 | { 3 | public static class TestValues 4 | { 5 | private const string DefaultProjectId = "ef21cf313a63dbf63f2e9e04f3614029"; 6 | private static readonly string EnvironmentProjectId = Environment.GetEnvironmentVariable("PROJECT_ID"); 7 | 8 | public static readonly string TestProjectId = !string.IsNullOrWhiteSpace(EnvironmentProjectId) 9 | ? EnvironmentProjectId 10 | : DefaultProjectId; 11 | 12 | private const string DefaultRelayUrl = "wss://relay.walletconnect.org"; 13 | 14 | private static readonly string EnvironmentRelayUrl = Environment.GetEnvironmentVariable("RELAY_ENDPOINT"); 15 | 16 | public static readonly string TestRelayUrl = 17 | !string.IsNullOrWhiteSpace(EnvironmentRelayUrl) ? EnvironmentRelayUrl : DefaultRelayUrl; 18 | 19 | private static readonly string EnvironmentClientCount = Environment.GetEnvironmentVariable("CLIENTS"); 20 | 21 | public static readonly int ClientCount = !string.IsNullOrWhiteSpace(EnvironmentClientCount) 22 | ? int.Parse(EnvironmentClientCount) 23 | : 200; 24 | 25 | private static readonly string EnvironmentMessageCount = 26 | Environment.GetEnvironmentVariable("MESSAGES_PER_CLIENT"); 27 | 28 | public static readonly int MessagesPerClient = !string.IsNullOrWhiteSpace(EnvironmentMessageCount) 29 | ? int.Parse(EnvironmentMessageCount) 30 | : 1000; 31 | 32 | private static readonly string EnvironmentHeartbeatInterval = 33 | Environment.GetEnvironmentVariable("HEARTBEAT_INTERVAL"); 34 | 35 | public static readonly int HeartbeatInterval = !string.IsNullOrWhiteSpace(EnvironmentHeartbeatInterval) 36 | ? int.Parse(EnvironmentHeartbeatInterval) 37 | : 3000; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Tests.Common/TwoClientsFixture.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Tests.Common; 2 | 3 | public abstract class TwoClientsFixture where TClient : IDisposable 4 | { 5 | public TClient ClientA { get; protected set; } 6 | public TClient ClientB { get; protected set; } 7 | 8 | 9 | public TwoClientsFixture(bool initNow = true) 10 | { 11 | if (initNow) 12 | Init(); 13 | } 14 | 15 | public abstract Task Init(); 16 | 17 | public async Task WaitForClientsReady() 18 | { 19 | while (ClientA == null || ClientB == null) 20 | await Task.Delay(10); 21 | } 22 | 23 | public virtual async Task DisposeAndReset() 24 | { 25 | if (!Equals(ClientA, default(TClient))) 26 | { 27 | ClientA.Dispose(); 28 | ClientA = default; 29 | } 30 | 31 | if (!Equals(ClientB, default(TClient))) 32 | { 33 | ClientB.Dispose(); 34 | ClientB = default; 35 | } 36 | 37 | await Task.Delay(500); 38 | 39 | await Init(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Tests.Common/UtilExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Tests.Common; 2 | 3 | public static class UtilExtensions 4 | { 5 | public static IEnumerable NextStrings( 6 | this Random rnd, 7 | string allowedChars, 8 | (int Min, int Max)length, 9 | int count) 10 | { 11 | ISet usedRandomStrings = new HashSet(); 12 | (int min, int max) = length; 13 | char[] chars = new char[max]; 14 | int setLength = allowedChars.Length; 15 | 16 | while (count-- > 0) 17 | { 18 | int stringLength = rnd.Next(min, max + 1); 19 | 20 | for (int i = 0; i < stringLength; ++i) 21 | { 22 | chars[i] = allowedChars[rnd.Next(setLength)]; 23 | } 24 | 25 | string randomString = new string(chars, 0, stringLength); 26 | 27 | if (usedRandomStrings.Add(randomString)) 28 | { 29 | yield return randomString; 30 | } 31 | else 32 | { 33 | count++; 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Tests.Common/WalletConnectSharp.Tests.Common.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | 2.0.0 6 | 2.0.0 7 | 2.0.0 8 | false 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Tests/WalletConnectSharp.Web3Wallet.Tests/WalletConnectSharp.Web3Wallet.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | 2.0.0 6 | 2.0.0 7 | 2.0.0 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | runtime; build; native; contentfiles; analyzers; buildtransitive 16 | all 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Interfaces/IAuthClient.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Auth.Models; 2 | using WalletConnectSharp.Common; 3 | using WalletConnectSharp.Core; 4 | using WalletConnectSharp.Core.Interfaces; 5 | 6 | namespace WalletConnectSharp.Auth.Interfaces; 7 | 8 | [Obsolete("WalletConnectSharp is now considered deprecated and will reach End-of-Life on February 17th 2025. For more details, including migration guides please see: https://docs.reown.com")] 9 | public interface IAuthClient : IModule, IAuthClientEvents 10 | { 11 | string Protocol { get; } 12 | int Version { get; } 13 | 14 | ICore Core { get; set; } 15 | Metadata Metadata { get; set; } 16 | string ProjectId { get; set; } 17 | IStore AuthKeys { get; set; } 18 | IStore PairingTopics { get; set; } 19 | IStore Requests { get; set; } 20 | 21 | IAuthEngine Engine { get; } 22 | 23 | AuthOptions Options { get; } 24 | 25 | IDictionary PendingRequests { get; } 26 | 27 | Task Request(RequestParams @params, string topic = null); 28 | 29 | Task Respond(Message message, string iss); 30 | 31 | Task> AuthHistory(); 32 | 33 | string FormatMessage(Cacao.CacaoPayload cacao); 34 | 35 | string FormatMessage(Cacao.CacaoRequestPayload cacao, string iss); 36 | 37 | internal bool OnAuthRequest(AuthRequest request); 38 | 39 | internal bool OnAuthResponse(AuthErrorResponse errorResponse); 40 | 41 | internal bool OnAuthResponse(AuthResponse response); 42 | } 43 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Interfaces/IAuthClientEvents.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Auth.Models; 2 | 3 | namespace WalletConnectSharp.Auth.Interfaces; 4 | 5 | public interface IAuthClientEvents 6 | { 7 | event EventHandler AuthRequested; 8 | event EventHandler AuthResponded; 9 | event EventHandler AuthError; 10 | } 11 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Interfaces/IAuthEngine.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Auth.Models; 2 | using WalletConnectSharp.Common; 3 | 4 | namespace WalletConnectSharp.Auth.Interfaces; 5 | 6 | public interface IAuthEngine : IModule 7 | { 8 | IAuthClient Client { get; } 9 | 10 | IDictionary PendingRequests { get; } 11 | 12 | Task Init(); 13 | 14 | Task Request(RequestParams @params, string topic = null); 15 | 16 | Task Respond(Message message, string iss); 17 | 18 | string FormatMessage(Cacao.CacaoPayload cacao); 19 | } 20 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Internals/AuthEngineValidations.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Auth.Interfaces; 2 | using WalletConnectSharp.Auth.Models; 3 | using WalletConnectSharp.Common.Model.Errors; 4 | using WalletConnectSharp.Common.Utils; 5 | using WalletConnectSharp.Core; 6 | using WalletConnectSharp.Core.Interfaces; 7 | 8 | namespace WalletConnectSharp.Auth.Controllers; 9 | 10 | public partial class AuthEngine : IAuthEngine 11 | { 12 | public const long MinExpiry = Clock.FIVE_MINUTES; 13 | public const long MaxExpiry = Clock.SEVEN_DAYS; 14 | 15 | internal bool IsValidRequest(RequestParams @params) 16 | { 17 | var validAudience = Utils.IsValidUrl(@params.Aud); 18 | // TODO: From typescript 19 | // FIXME: disabling this temporarily since it's failing expected values like `chainId: "1"` 20 | // const validChainId = isValidChainId(params.chainId); 21 | var domainInAud = @params.Aud.Contains(@params.Domain); 22 | var hasNonce = !string.IsNullOrWhiteSpace(@params.Nonce); 23 | var hasValidType = @params.Type == "eip4361"; 24 | var expiry = @params.Expiry; 25 | if (expiry != null && !Utils.IsValidRequestExpiry(expiry.Value, MinExpiry, MaxExpiry)) 26 | { 27 | throw new ArgumentException($"Request expiry: {expiry}. Expiry must be a number (in seconds) between {MinExpiry} and {MaxExpiry}"); 28 | } 29 | 30 | return validAudience && domainInAud && hasNonce && hasValidType; 31 | } 32 | 33 | internal PendingRequest GetPendingRequest(IStore pendingResponses, long id) 34 | { 35 | return pendingResponses.Values.OfType().FirstOrDefault(request => request.Id == id); 36 | } 37 | 38 | internal bool IsValidRespond(Message @params, IStore pendingResponses) 39 | { 40 | if (@params.Id == null) 41 | return false; 42 | 43 | var validId = GetPendingRequest(pendingResponses, (long)@params.Id); 44 | 45 | return validId != null; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Internals/CryptoUtils.cs: -------------------------------------------------------------------------------- 1 | using System.Security.Cryptography; 2 | 3 | namespace WalletConnectSharp.Auth.Internals 4 | { 5 | public class CryptoUtils 6 | { 7 | private static readonly char[] ALPHANUMERIC = 8 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray(); 9 | 10 | public static string GenerateNonce() 11 | { 12 | int bits = 96; 13 | int charsetLength = ALPHANUMERIC.Length; 14 | int length = (int)Math.Ceiling(bits / (Math.Log10(charsetLength) / Math.Log(2))); 15 | 16 | string @out = ""; 17 | int maxByte = 256 - (256 % charsetLength); 18 | 19 | using (var rng = RandomNumberGenerator.Create()) 20 | { 21 | while (length > 0) 22 | { 23 | byte[] buf = new byte[(int)Math.Ceiling(length * 256.0 / maxByte)]; 24 | 25 | rng.GetBytes(buf); 26 | 27 | for (int i = 0; i < buf.Length && length > 0; i++) 28 | { 29 | var randomByte = buf[i]; 30 | if (randomByte < maxByte) 31 | { 32 | @out += ALPHANUMERIC[randomByte % charsetLength]; 33 | length--; 34 | } 35 | } 36 | } 37 | } 38 | 39 | return @out; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Internals/IssDidUtils.cs: -------------------------------------------------------------------------------- 1 | using System.Globalization; 2 | 3 | namespace WalletConnectSharp.Auth.Internals 4 | { 5 | public static class IssDidUtils 6 | { 7 | public static string[] ExtractDidAddressSegments(string iss) 8 | { 9 | if (string.IsNullOrWhiteSpace(iss)) 10 | return null; 11 | 12 | return iss.Split(":"); 13 | } 14 | 15 | public static string DidChainId(string iss) 16 | { 17 | if (string.IsNullOrWhiteSpace(iss)) 18 | return null; 19 | 20 | return ExtractDidAddressSegments(iss)[3]; 21 | } 22 | 23 | public static string NamespacedDidChainId(string iss) 24 | { 25 | if (string.IsNullOrWhiteSpace(iss)) 26 | return null; 27 | 28 | var segments = ExtractDidAddressSegments(iss); 29 | 30 | return $"{segments[2]}:{segments[3]}"; 31 | } 32 | 33 | public static string DidAddress(string iss) 34 | { 35 | if (string.IsNullOrWhiteSpace(iss)) 36 | return null; 37 | 38 | var segments = ExtractDidAddressSegments(iss); 39 | 40 | if (segments.Length == 0) 41 | return null; 42 | 43 | return segments[segments.Length - 1]; 44 | } 45 | 46 | public static string ToISOString(this DateTime dateTime) 47 | { 48 | return dateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK", CultureInfo.InvariantCulture); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/AuthData.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Interfaces; 3 | 4 | namespace WalletConnectSharp.Auth.Models; 5 | 6 | public class AuthData : IKeyHolder 7 | { 8 | [JsonProperty("responseTopic")] 9 | public string ResponseTopic; 10 | 11 | [JsonProperty("publicKey")] 12 | public string PublicKey; 13 | 14 | public string Key 15 | { 16 | get 17 | { 18 | return ResponseTopic; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/AuthErrorResponse.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Network.Models; 3 | 4 | namespace WalletConnectSharp.Auth.Models; 5 | 6 | public class AuthErrorResponse : TopicMessage 7 | { 8 | [JsonProperty("params")] 9 | public Error Error; 10 | } 11 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/AuthOptions.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Core; 2 | using WalletConnectSharp.Core.Interfaces; 3 | using WalletConnectSharp.Core.Models; 4 | 5 | namespace WalletConnectSharp.Auth.Models; 6 | 7 | public class AuthOptions : CoreOptions 8 | { 9 | public Metadata Metadata; 10 | 11 | public ICore Core { get; set; } 12 | } 13 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/AuthPayload.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Auth.Models; 4 | 5 | public class AuthPayload 6 | { 7 | [JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)] 8 | public string? Type; 9 | 10 | [JsonProperty("chainId")] 11 | public string ChainId; 12 | 13 | [JsonProperty("domain")] 14 | public string Domain; 15 | 16 | [JsonProperty("aud")] 17 | public string Aud; 18 | 19 | [JsonProperty("nonce")] 20 | public string Nonce; 21 | 22 | [JsonProperty("nbf", NullValueHandling = NullValueHandling.Ignore)] 23 | public string Nbf; 24 | 25 | [JsonProperty("exp", NullValueHandling = NullValueHandling.Ignore)] 26 | public string Exp; 27 | 28 | [JsonProperty("statement", NullValueHandling = NullValueHandling.Ignore)] 29 | public string Statement; 30 | 31 | [JsonProperty("requestId", NullValueHandling = NullValueHandling.Ignore)] 32 | public string RequestId; 33 | 34 | [JsonProperty("resources", NullValueHandling = NullValueHandling.Ignore)] 35 | public string[] Resources; 36 | } 37 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/AuthRequest.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Models.Verify; 3 | 4 | namespace WalletConnectSharp.Auth.Models; 5 | 6 | public class AuthRequest : TopicMessage 7 | { 8 | [JsonProperty("params")] 9 | public AuthRequestData Parameters; 10 | 11 | [JsonProperty("verifyContext")] 12 | public VerifiedContext VerifyContext { get; set; } 13 | } 14 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/AuthRequestData.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Models.Verify; 3 | 4 | namespace WalletConnectSharp.Auth.Models; 5 | 6 | public class AuthRequestData 7 | { 8 | [JsonProperty("cacaoPayload")] 9 | public Cacao.CacaoRequestPayload CacaoPayload; 10 | 11 | [JsonProperty("requester")] 12 | public Requester Requester { get; set; } 13 | } 14 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/AuthResponse.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Network.Models; 3 | 4 | namespace WalletConnectSharp.Auth.Models; 5 | 6 | public class AuthResponse : TopicMessage 7 | { 8 | [JsonProperty("params")] 9 | public JsonRpcResponse Response; 10 | } 11 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/ErrorResponse.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Network.Models; 3 | 4 | namespace WalletConnectSharp.Auth.Models; 5 | 6 | public class ErrorResponse : Message 7 | { 8 | [JsonProperty("error")] 9 | public Error Error; 10 | } 11 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/Message.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Interfaces; 3 | 4 | namespace WalletConnectSharp.Auth.Models; 5 | 6 | public class Message : IKeyHolder 7 | { 8 | [JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)] 9 | public long? Id; 10 | 11 | public long Key 12 | { 13 | get 14 | { 15 | if (Id != null) 16 | return (long)Id; 17 | throw new KeyNotFoundException("Id Key for message instance is null: " + this.ToString()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/PairingData.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Interfaces; 3 | 4 | namespace WalletConnectSharp.Auth.Models; 5 | 6 | public class PairingData : IKeyHolder 7 | { 8 | [JsonProperty("topic")] 9 | public string Topic; 10 | 11 | [JsonProperty("pairingTopic")] 12 | public string PairingTopic { get; set; } 13 | 14 | public string Key 15 | { 16 | get 17 | { 18 | return PairingTopic; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/PayloadParams.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Auth.Models; 4 | 5 | public class PayloadParams : AuthPayload 6 | { 7 | [JsonProperty("version")] 8 | public string Version; 9 | 10 | [JsonProperty("iat")] 11 | public string Iat { get; set; } 12 | } 13 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/PendingRequest.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Auth.Models; 4 | 5 | public class PendingRequest : Message 6 | { 7 | [JsonProperty("pairingTopic")] 8 | public string PairingTopic; 9 | 10 | [JsonProperty("requester")] 11 | public Requester Requester { get; set; } 12 | 13 | [JsonProperty("cacaoPayload")] 14 | public Cacao.CacaoRequestPayload CacaoPayload { get; set; } 15 | } 16 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/RequestParams.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Auth.Models; 4 | 5 | public class RequestParams : AuthPayload 6 | { 7 | [JsonProperty("expiry", NullValueHandling = NullValueHandling.Ignore)] 8 | public long? Expiry; 9 | 10 | public RequestParams() { } 11 | 12 | public RequestParams(AuthPayload payload) 13 | { 14 | this.Aud = payload.Aud; 15 | this.Domain = payload.Domain; 16 | this.Exp = payload.Exp; 17 | this.Nbf = payload.Nbf; 18 | this.Nonce = payload.Nonce; 19 | this.Resources = payload.Resources; 20 | this.Statement = payload.Statement; 21 | this.Type = payload.Type; 22 | this.ChainId = payload.ChainId; 23 | this.RequestId = payload.RequestId; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/RequestUri.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Auth.Models; 4 | 5 | public class RequestUri 6 | { 7 | [JsonProperty("id")] 8 | public long Id; 9 | 10 | [JsonProperty("uri")] 11 | public string Uri { get; set; } 12 | } 13 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/Requester.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core; 3 | 4 | namespace WalletConnectSharp.Auth.Models; 5 | 6 | public class Requester 7 | { 8 | [JsonProperty("metadata")] 9 | public Metadata Metadata; 10 | 11 | [JsonProperty("publicKey")] 12 | public string PublicKey; 13 | } 14 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/ResultResponse.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Auth.Models; 4 | 5 | public class ResultResponse : Message 6 | { 7 | [JsonProperty("signature")] 8 | public Cacao.CacaoSignature Signature; 9 | } 10 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/Models/TopicMessage.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Auth.Models; 4 | 5 | public class TopicMessage : Message 6 | { 7 | [JsonProperty("topic")] 8 | public string Topic; 9 | } 10 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/WalletConnectSharp.Auth.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(DefaultTargetFrameworks) 5 | $(DefaultVersion) 6 | $(DefaultVersion) 7 | $(DefaultVersion) 8 | WalletConnect.Auth 9 | WalletConnectSharp.Auth 10 | pedrouid, gigajuwels 11 | A port of the TypeScript SDK to C#. A complete implementation of the WalletConnect v2 protocol that can be used to connect to external wallets or connect a wallet to an external Dapp 12 | Copyright (c) WalletConnect 2023 13 | https://walletconnect.org/ 14 | icon.png 15 | https://github.com/WalletConnect/WalletConnectSharp 16 | git 17 | walletconnect sign wallet web3 ether ethereum blockchain evm 18 | true 19 | Apache-2.0 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /WalletConnectSharp.Auth/WcAuthRequest.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Auth.Models; 3 | using WalletConnectSharp.Common.Utils; 4 | using WalletConnectSharp.Network.Models; 5 | 6 | namespace WalletConnectSharp.Auth; 7 | 8 | [RpcMethod("wc_authRequest")] 9 | [RpcRequestOptions(Clock.ONE_DAY, 3000)] 10 | [RpcResponseOptions(Clock.ONE_DAY, 3001)] 11 | public class WcAuthRequest 12 | { 13 | [JsonProperty("payloadParams")] 14 | public PayloadParams Payload; 15 | 16 | [JsonProperty("requester")] 17 | public Requester Requester { get; set; } 18 | } 19 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Controllers/PairingStore.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Core.Interfaces; 2 | using WalletConnectSharp.Core.Models; 3 | using WalletConnectSharp.Core.Models.Pairing; 4 | using WalletConnectSharp.Sign.Models; 5 | 6 | namespace WalletConnectSharp.Core.Controllers 7 | { 8 | /// 9 | /// A module for storing 10 | /// data. This will be used 11 | /// for storing pairing data 12 | /// 13 | public class PairingStore : Store, IPairingStore 14 | { 15 | /// 16 | /// Create a new instance of this module 17 | /// 18 | /// The instance that will be used for 19 | public PairingStore(ICore core) : base(core, "pairing", WalletConnectCore.STORAGE_PREFIX) 20 | { 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Core.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Core 2 | { 3 | [Obsolete("WalletConnectSharp is now considered deprecated and will reach End-of-Life on February 17th 2025. For more details, including migration guides please see: https://docs.reown.com")] 4 | public class Core : WalletConnectCore { } 5 | } 6 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Interfaces/IHeartBeat.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Common; 2 | 3 | namespace WalletConnectSharp.Core.Interfaces 4 | { 5 | /// 6 | /// The HeartBeat module emits a pulse event at a specific interval simulating 7 | /// a heartbeat. It can be used as an setInterval replacement or timing actions 8 | /// 9 | public interface IHeartBeat : IModule 10 | { 11 | event EventHandler OnPulse; 12 | 13 | /// 14 | /// The interval (in milliseconds) the Pulse event gets emitted/triggered 15 | /// 16 | public int Interval { get; } 17 | 18 | /// 19 | /// Initialize the heartbeat module. This will start the pulse event and 20 | /// will continuously emit the pulse event at the configured interval. If the 21 | /// HeartBeatCancellationToken is cancelled, then the interval will be halted. 22 | /// 23 | /// 24 | public Task InitAsync(CancellationToken cancellationToken = default); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Interfaces/IJsonRpcHistoryFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace WalletConnectSharp.Core.Interfaces 4 | { 5 | /// 6 | /// The interface for a factory that creates/tracks instances of for the given 7 | /// types T and TR. This is used to track the history of requests and responses for a given session. This factory 8 | /// is NOT a singleton, rather each ICore instance should have its own instance of this factory. The 9 | /// instance the factory returns must act as a singleton for the given 10 | /// ICore context. The factory implementation must be context-aware, meaning that it must be able to separate different 11 | /// singleton instances of for different ICore instances. 12 | /// 13 | public interface IJsonRpcHistoryFactory 14 | { 15 | /// 16 | /// The ICore instance this factory is for 17 | /// 18 | ICore Core { get; } 19 | 20 | /// 21 | /// Get the singleton instance of for the given types T and TR. 22 | /// 23 | /// The request type of the history to keep track 24 | /// The response type of the history to keep track 25 | /// A new or existing singleton instance for the given type T and TR. 26 | /// If no singleton instance exists in the current ICore context, then a new instance will be created. 27 | Task> JsonRpcHistoryOfType(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Interfaces/IKeyHolder.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Interfaces 4 | { 5 | /// 6 | /// An interface that represents a type that has a "key" field. A "key" field is a TKey value that is used to identify the object. 7 | /// Common types for TKey include string or long. 8 | /// 9 | /// The type of key this object uses. 10 | public interface IKeyHolder 11 | { 12 | /// 13 | /// The key field of this data element 14 | /// 15 | [JsonIgnore] 16 | public TKey Key { get; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Interfaces/IPairingStore.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Core.Models; 2 | using WalletConnectSharp.Core.Models.Pairing; 3 | using WalletConnectSharp.Sign.Models; 4 | 5 | namespace WalletConnectSharp.Core.Interfaces 6 | { 7 | /// 8 | /// A interface for a module 9 | /// that stores data. 10 | /// 11 | public interface IPairingStore : IStore { } 12 | } 13 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Interfaces/IPublisher.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Common; 2 | using WalletConnectSharp.Core.Models.Publisher; 3 | using WalletConnectSharp.Core.Models.Relay; 4 | 5 | namespace WalletConnectSharp.Core.Interfaces 6 | { 7 | /// 8 | /// An interface for the Publisher module. The Publisher module is responsible for sending messages to the 9 | /// WalletConnect relay server. 10 | /// 11 | public interface IPublisher : IModule 12 | { 13 | event EventHandler OnPublishedMessage; 14 | 15 | /// 16 | /// The IRelayer instance this publisher is using to publish messages 17 | /// 18 | public IRelayer Relayer { get; } 19 | 20 | /// 21 | /// Publish a new message to the relayer. This will be sent to the peer that is connected to the relayer. 22 | /// 23 | /// The topic to publish the message in 24 | /// The message to publish 25 | /// (optional) PublishOptions specifying TTL the Tag. 26 | /// 27 | public Task Publish(string topic, string message, PublishOptions opts = null); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Interfaces/ISubscriberMap.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Core.Interfaces 2 | { 3 | /// 4 | /// An interface that represents a mapping of topics to a list of subscription ids 5 | /// 6 | public interface ISubscriberMap 7 | { 8 | /// 9 | /// An array of topics in this mapping 10 | /// 11 | public string[] Topics { get; } 12 | 13 | /// 14 | /// Add an subscription id to the given topic 15 | /// 16 | /// The topic to add the subscription id to 17 | /// The subscription id to add 18 | public void Set(string topic, string id); 19 | 20 | /// 21 | /// Get an array of all subscription ids in a given topic 22 | /// 23 | /// The topic to get subscription ids for 24 | /// An array of subscription ids in a given topic 25 | public string[] Get(string topic); 26 | 27 | /// 28 | /// Determine whether a subscription id exists in a given topic 29 | /// 30 | /// The topic to check in 31 | /// The subscription id to check for 32 | /// True if the subscription id is in the topic, false otherwise 33 | public bool Exists(string topic, string id); 34 | 35 | /// 36 | /// Delete subscription id from a topic. If no subscription id is given, 37 | /// then all subscription ids in the given topic are removed. 38 | /// 39 | /// The topic to remove from 40 | /// The subscription id to remove, if set to null then all ids are removed from the topic 41 | public void Delete(string topic, string id = null); 42 | 43 | /// 44 | /// Clear all entries in the mapping 45 | /// 46 | public void Clear(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Metadata.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Models; 3 | 4 | namespace WalletConnectSharp.Core 5 | { 6 | /// 7 | /// A class that holds Metadata for either peer in a given Session. Includes things such 8 | /// as Name of peer, Description, urls and images. 9 | /// 10 | [Serializable] 11 | public class Metadata 12 | { 13 | /// 14 | /// The name of this peer 15 | /// 16 | [JsonProperty("name")] public string Name; 17 | 18 | /// 19 | /// The description for this peer 20 | /// 21 | [JsonProperty("description")] public string Description; 22 | 23 | /// 24 | /// The URL of the software this peer represents 25 | /// 26 | [JsonProperty("url")] public string Url; 27 | 28 | /// 29 | /// The URL of image icons of the software this peer represents 30 | /// 31 | [JsonProperty("icons")] public string[] Icons; 32 | 33 | [JsonProperty("redirect")] public RedirectData Redirect; 34 | 35 | [JsonProperty("verifyUrl")] public string VerifyUrl; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/BatchFetchMessageRequest.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Network.Models; 3 | 4 | namespace WalletConnectSharp.Core.Models; 5 | 6 | public class BatchFetchMessageRequest 7 | { 8 | [JsonProperty("topics")] 9 | public string[] Topics; 10 | } 11 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/BatchFetchMessagesResponse.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models; 4 | 5 | public class BatchFetchMessagesResponse 6 | { 7 | public class ReceivedMessage 8 | { 9 | [JsonProperty("topic")] 10 | public string Topic; 11 | 12 | [JsonProperty("message")] 13 | public string Message; 14 | 15 | [JsonProperty("publishedAt")] 16 | public long PublishedAt; 17 | 18 | [JsonProperty("tag")] 19 | public long Tag; 20 | } 21 | 22 | [JsonProperty("messages")] 23 | public ReceivedMessage[] Messages; 24 | 25 | [JsonProperty("hasMore")] 26 | public bool HasMore; 27 | } 28 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/DisposeHandlerToken.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Core.Models; 2 | 3 | public class DisposeHandlerToken : IDisposable 4 | { 5 | private readonly Action _onDispose; 6 | 7 | public DisposeHandlerToken(Action onDispose) 8 | { 9 | if (onDispose == null) 10 | throw new ArgumentException("onDispose must be non-null"); 11 | this._onDispose = onDispose; 12 | } 13 | 14 | public void Dispose() 15 | { 16 | Dispose(true); 17 | GC.SuppressFinalize(this); 18 | } 19 | 20 | protected virtual void Dispose(bool disposing) 21 | { 22 | if (Disposed) return; 23 | 24 | if (disposing) 25 | { 26 | this._onDispose(); 27 | } 28 | 29 | Disposed = true; 30 | } 31 | 32 | protected bool Disposed; 33 | } 34 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Eth/EthCall.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Eth; 4 | 5 | public class EthCall 6 | { 7 | [JsonProperty("to")] public string To; 8 | 9 | [JsonProperty("data")] public string Data; 10 | } 11 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Expirer/Expiration.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Expirer 4 | { 5 | /// 6 | /// A class that represents a specific expiration date for a specific target. 7 | /// 8 | public class Expiration 9 | { 10 | /// 11 | /// The target this expiration is for 12 | /// The format for the Target string can be either 13 | /// * id:123 14 | /// * topic:my_topic_string 15 | /// 16 | [JsonProperty("target")] public string Target; 17 | 18 | /// 19 | /// The expiration date, as a unix timestamp (seconds) 20 | /// 21 | [JsonProperty("expiry")] public long Expiry; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Expirer/ExpirerEventArgs.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Expirer 4 | { 5 | /// 6 | /// The event args that is passed to all events triggered 7 | /// 8 | public class ExpirerEventArgs 9 | { 10 | /// 11 | /// The target this expiration is for 12 | /// 13 | [JsonProperty("target")] 14 | public string Target; 15 | 16 | /// 17 | /// The expiration data for this event 18 | /// 19 | [JsonProperty("expiration")] 20 | public Expiration Expiration; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Expirer/ExpirerTarget.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Expirer 4 | { 5 | /// 6 | /// A class that converts a >string to either a ID (long) or a Topic (string). Either the ID or Topic 7 | /// will be non-null while the other will be null. 8 | /// The format for the can be either 9 | /// * id:123 10 | /// * topic:my_topic_string 11 | /// 12 | public class ExpirerTarget 13 | { 14 | /// 15 | /// The resulting ID from the given . If the did not include an Id, then 16 | /// this field will be null 17 | /// 18 | [JsonProperty("id")] 19 | public long? Id; 20 | 21 | /// 22 | /// The resulting Topic from the given . If the did not include a Topic, then 23 | /// this field will be null 24 | /// 25 | [JsonProperty("topic")] 26 | public string Topic; 27 | 28 | /// 29 | /// Create a new instance of this class with a given . The given will 30 | /// be converted and stored to either the ID field or Topic field 31 | /// 32 | /// The to convert 33 | /// If the format for the given is invalid 34 | public ExpirerTarget(string target) 35 | { 36 | var values = target.Split(':'); 37 | if (values.Length != 2) 38 | { 39 | throw new FormatException($"Invalid target format: {target}. Expected format: 'type:value'."); 40 | } 41 | 42 | var (type, value) = (values[0], values[1]); 43 | 44 | switch (type) 45 | { 46 | case "topic": 47 | Topic = value; 48 | break; 49 | case "id" when long.TryParse(value, out var id): 50 | Id = id; 51 | break; 52 | case "id": 53 | throw new FormatException($"Cannot parse id {value} as a long."); 54 | default: 55 | throw new FormatException($"Invalid target type: {type}. Expected 'id' or 'topic'."); 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/History/JsonRpcRecord.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Network; 3 | using WalletConnectSharp.Network.Models; 4 | 5 | namespace WalletConnectSharp.Core.Models.History 6 | { 7 | /// 8 | /// A class representing a single JSON RPC history record containing the Id, Topic, Request, Response and ChainId. 9 | /// If no Response is set, then the record hasn't been resolved yet 10 | /// 11 | /// The type of the request parameter 12 | /// The type of the response parameter 13 | public class JsonRpcRecord 14 | { 15 | /// 16 | /// The id of the JSON RPC request 17 | /// 18 | [JsonProperty("id")] 19 | public long Id; 20 | 21 | /// 22 | /// The topic the request was sent in 23 | /// 24 | [JsonProperty("topic")] 25 | public string Topic; 26 | 27 | /// 28 | /// The request data for this JSON RPC record 29 | /// 30 | [JsonProperty("request")] 31 | public IRequestArguments Request; 32 | 33 | /// 34 | /// The chainId this request is intended for 35 | /// 36 | [JsonProperty("chainId")] 37 | public string ChainId { get; set; } 38 | 39 | /// 40 | /// The response data for this JSON RPC record. If no Response data is set, then this request is 41 | /// still pending 42 | /// 43 | [JsonProperty("response")] 44 | public IJsonRpcResult Response; 45 | 46 | /// 47 | /// This constructor is required for the JSON deserializer to be able 48 | /// to identify concrete classes to use when deserializing the interface properties. 49 | /// 50 | public JsonRpcRecord(IJsonRpcRequest request) 51 | { 52 | Request = request; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/History/RequestEvent.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Newtonsoft.Json; 3 | using WalletConnectSharp.Network; 4 | using WalletConnectSharp.Network.Models; 5 | 6 | namespace WalletConnectSharp.Core.Models.History 7 | { 8 | /// 9 | /// A class representing a pending JSON RPC request event. 10 | /// 11 | /// The type of request 12 | public class RequestEvent 13 | { 14 | /// 15 | /// The topic the request was sent in 16 | /// 17 | [JsonProperty("topic")] 18 | public string Topic; 19 | 20 | /// 21 | /// The request parameters sent 22 | /// 23 | [JsonProperty("request")] 24 | public IRequestArguments Request; 25 | 26 | /// 27 | /// The chainId this request is intended for 28 | /// 29 | [JsonProperty("chainId")] 30 | public string ChainId; 31 | 32 | /// 33 | /// A helper function to create a new RequestEvent from a 34 | /// 35 | /// The pending 36 | /// The response type expected for this request 37 | /// A new RequestEvent based on the given 38 | public static RequestEvent FromPending(JsonRpcRecord pending) 39 | { 40 | return new RequestEvent() 41 | { 42 | Topic = pending.Topic, 43 | Request = pending.Request, 44 | ChainId = pending.ChainId, 45 | }; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/MessageHandler/RequestEventArgs.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Core.Models.Verify; 2 | using WalletConnectSharp.Network.Models; 3 | 4 | namespace WalletConnectSharp.Sign.Models 5 | { 6 | /// 7 | /// Event args that are used in and 8 | /// when a new response of the given type TR is received. 9 | /// Stores information about the current request, such as the topic, and the request. 10 | /// 11 | /// These event args can also set the corresponding Response or Error, which will be sent automatically after 12 | /// the event has finished propagating 13 | /// 14 | /// The request type 15 | /// The response type 16 | public class RequestEventArgs 17 | { 18 | /// 19 | /// The topic this request came from 20 | /// 21 | public string Topic { get; } 22 | 23 | /// 24 | /// The request data for this event 25 | /// 26 | public JsonRpcRequest Request { get; } 27 | 28 | /// 29 | /// The current response to send when this event finishes propagating. You can set this value 30 | /// to send a response when this event completes. 31 | /// If the field is non-null, then this field will not be sent and the 32 | /// will be sent instead 33 | /// 34 | public TR Response; 35 | 36 | /// 37 | /// The current error to send when this event finishes propagating. You can set this value 38 | /// to send an Error response when this event completes. 39 | /// This value will always override if the value is non-null 40 | /// 41 | public Error Error; 42 | 43 | public VerifiedContext VerifiedContext; 44 | 45 | internal RequestEventArgs(string topic, JsonRpcRequest request, VerifiedContext context) 46 | { 47 | Topic = topic; 48 | Request = request; 49 | VerifiedContext = context; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/MessageHandler/ResponseEventArgs.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Network.Models; 2 | 3 | namespace WalletConnectSharp.Sign.Models 4 | { 5 | /// 6 | /// Event args that are used in and 7 | /// when a new response of the given type TR is received. 8 | /// Stores information about the current response, such as the topic, and the response itself. 9 | /// 10 | /// The response type 11 | public class ResponseEventArgs 12 | { 13 | /// 14 | /// The topic this response came from 15 | /// 16 | public string Topic { get; } 17 | 18 | /// 19 | /// The response data 20 | /// 21 | public JsonRpcResponse Response { get; } 22 | 23 | internal ResponseEventArgs(JsonRpcResponse response, string topic) 24 | { 25 | Response = response; 26 | Topic = topic; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Pairing/CreatePairingData.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Pairing 4 | { 5 | /// 6 | /// A class that represents a new pairing. Includes the pairing topic 7 | /// and the URI the wallet should use to pair & retrieve the session proposal 8 | /// 9 | public class CreatePairingData 10 | { 11 | /// 12 | /// The new pairing topic 13 | /// 14 | [JsonProperty("topic")] 15 | public string Topic; 16 | 17 | /// 18 | /// The URI the wallet should use to pair & retrieve the session proposal 19 | /// 20 | [JsonProperty("uri")] 21 | public string Uri; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Pairing/Methods/PairingDelete.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Common.Utils; 2 | using WalletConnectSharp.Network.Models; 3 | 4 | namespace WalletConnectSharp.Core.Models.Pairing.Methods 5 | { 6 | /// 7 | /// A class that represents the request wc_pairingDelete. This is used to delete a pairing 8 | /// 9 | [RpcMethod("wc_pairingDelete")] 10 | [RpcRequestOptions(Clock.ONE_DAY, 1000)] 11 | [RpcResponseOptions(Clock.ONE_DAY, 1001)] 12 | public class PairingDelete : Error 13 | { 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Pairing/Methods/PairingPing.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Common.Utils; 2 | using WalletConnectSharp.Network.Models; 3 | 4 | namespace WalletConnectSharp.Core.Models.Pairing.Methods 5 | { 6 | /// 7 | /// A class that represents the request wc_pairingPing. Used to ping a pairing 8 | /// request 9 | /// 10 | [RpcMethod("wc_pairingPing")] 11 | [RpcRequestOptions(Clock.THIRTY_SECONDS, 1002)] 12 | [RpcResponseOptions(Clock.THIRTY_SECONDS, 1003)] 13 | public class PairingPing : Dictionary 14 | { 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Pairing/PairingEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Pairing 4 | { 5 | /// 6 | /// The event that is emitted when any pairing event occurs. Some examples 7 | /// include 8 | /// * Pairing Ping 9 | /// * Pairing Delete 10 | /// 11 | public class PairingEvent 12 | { 13 | /// 14 | /// The ID of the JSON Rpc request that triggered this session event 15 | /// 16 | [JsonProperty("id")] 17 | public long Id; 18 | 19 | /// 20 | /// The topic of the session this event took place in 21 | /// 22 | [JsonProperty("topic")] 23 | public string Topic; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Pairing/PairingStruct.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Interfaces; 3 | using WalletConnectSharp.Core.Models.Relay; 4 | 5 | namespace WalletConnectSharp.Core.Models.Pairing 6 | { 7 | /// 8 | /// A struct that stores pairing data, including the topic the pairing took place, whether 9 | /// the pairing is active or not, when the pairing expires and who the pairing was with 10 | /// 11 | public struct PairingStruct : IKeyHolder 12 | { 13 | /// 14 | /// The topic the pairing took place in 15 | /// 16 | [JsonProperty("topic")] 17 | public string Topic; 18 | 19 | /// 20 | /// This is the key field, mapped to the Topic. Implemented for 21 | /// so this struct can be stored using 22 | /// 23 | [JsonIgnore] 24 | public string Key 25 | { 26 | get 27 | { 28 | return Topic; 29 | } 30 | } 31 | 32 | /// 33 | /// When this pairing expires 34 | /// 35 | [JsonProperty("expiry")] 36 | public long? Expiry; 37 | 38 | /// 39 | /// Relay protocol options for this pairing 40 | /// 41 | [JsonProperty("relay")] 42 | public ProtocolOptions Relay; 43 | 44 | /// 45 | /// Whether this pairing is active or not 46 | /// 47 | [JsonProperty("active")] 48 | public bool? Active; 49 | 50 | /// 51 | /// The metadata of the peer this pairing is with 52 | /// 53 | [JsonProperty("peerMetadata")] 54 | public Metadata PeerMetadata; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Pairing/UriParameters.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Models.Relay; 3 | 4 | namespace WalletConnectSharp.Core.Models.Pairing 5 | { 6 | /// 7 | /// A class that holds parameters from a parsed session proposal URI. This can be 8 | /// retrieved from 9 | /// 10 | public class UriParameters 11 | { 12 | /// 13 | /// The protocol being used for this session (as a protocol string) 14 | /// 15 | [JsonProperty("protocol")] 16 | public string Protocol; 17 | 18 | /// 19 | /// The protocol version being used for this session 20 | /// 21 | [JsonProperty("version")] 22 | public int Version; 23 | 24 | /// 25 | /// The pairing topic that should be used to retrieve the session proposal 26 | /// 27 | [JsonProperty("topic")] 28 | public string Topic; 29 | 30 | /// 31 | /// The sym key used to encrypt the session proposal 32 | /// 33 | [JsonProperty("symKey")] 34 | public string SymKey; 35 | 36 | /// 37 | /// Any protocol options that should be used when pairing / approving the session 38 | /// 39 | [JsonProperty("relay")] 40 | public ProtocolOptions Relay; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Publisher/PublishParams.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Models.Relay; 3 | 4 | namespace WalletConnectSharp.Core.Models.Publisher 5 | { 6 | /// 7 | /// A class that holds the parameters of a publish 8 | /// 9 | public class PublishParams 10 | { 11 | /// 12 | /// The topic to publish the message to 13 | /// 14 | [JsonProperty("topic")] 15 | public string Topic; 16 | 17 | /// 18 | /// The message to publish in the set topic 19 | /// 20 | [JsonProperty("message")] 21 | public string Message; 22 | 23 | /// 24 | /// The required PublishOptions to use when publishing 25 | /// 26 | [JsonProperty("opts")] 27 | public PublishOptions Options; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/RedirectData.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models 4 | { 5 | [Serializable] 6 | public class RedirectData 7 | { 8 | [JsonProperty("native")] public string Native; 9 | 10 | [JsonProperty("universal")] public string Universal; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Relay/DecodedMessageEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Network.Models; 3 | 4 | namespace WalletConnectSharp.Core.Models.Relay 5 | { 6 | /// 7 | /// Represents a that has been deserialized into a 8 | /// 9 | public class DecodedMessageEvent : MessageEvent 10 | { 11 | /// 12 | /// The deserialized payload that was decoded from the Message property 13 | /// 14 | [JsonProperty("payload")] 15 | public JsonRpcPayload Payload; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Relay/MessageEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Relay 4 | { 5 | /// 6 | /// A class that represents a message that has been sent / received inside 7 | /// a specific topic 8 | /// 9 | public class MessageEvent 10 | { 11 | /// 12 | /// The topic the message was sent / received in 13 | /// 14 | [JsonProperty("topic")] 15 | public string Topic; 16 | 17 | /// 18 | /// The message that was sent / received 19 | /// 20 | [JsonProperty("message")] 21 | public string Message; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Relay/ProtocolOptionHolder.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Relay 4 | { 5 | /// 6 | /// An abstract class that simply holds ProtocolOptions under the Relay property 7 | /// 8 | [Serializable] 9 | public abstract class ProtocolOptionHolder 10 | { 11 | /// 12 | /// The relay protocol options to use for this event 13 | /// 14 | [JsonProperty("relay")] 15 | public ProtocolOptions Relay; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Relay/ProtocolOptions.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Relay 4 | { 5 | /// 6 | /// Protocol options to use when communicating with the relay server 7 | /// 8 | public class ProtocolOptions 9 | { 10 | /// 11 | /// The protocol to use when communicating with the relay server 12 | /// 13 | [JsonProperty("protocol")] 14 | public string Protocol; 15 | 16 | /// 17 | /// Additional protocol data 18 | /// 19 | [JsonProperty("data", NullValueHandling = NullValueHandling.Ignore)] 20 | public string Data; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Relay/PublishOptions.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Relay 4 | { 5 | /// 6 | /// A class that represents options when publishing messages 7 | /// 8 | [Serializable] 9 | public class PublishOptions : ProtocolOptionHolder 10 | { 11 | /// 12 | /// Time To Live value for the message being published. 13 | /// 14 | [JsonProperty("ttl")] 15 | public long TTL; 16 | 17 | /// 18 | /// A Tag for the message 19 | /// 20 | [JsonProperty("tag")] 21 | public long Tag; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Relay/RelayPublishRequest.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics.CodeAnalysis; 2 | using Newtonsoft.Json; 3 | 4 | namespace WalletConnectSharp.Core.Models.Relay 5 | { 6 | /// 7 | /// The parameters for publishing a message to the relay server under a specific topic 8 | /// 9 | [Serializable] 10 | public class RelayPublishRequest 11 | { 12 | /// 13 | /// The topic to publish the message under 14 | /// 15 | [JsonProperty("topic")] 16 | public string Topic; 17 | 18 | /// 19 | /// The message to publish to the relay server 20 | /// 21 | [JsonProperty("message")] 22 | public string Message; 23 | 24 | /// 25 | /// Time To Live. How long the message will remain on the relay server without being 26 | /// consumed (by a subscriber to the topic) before it's deleted 27 | /// 28 | [JsonProperty("ttl")] public long TTL; 29 | 30 | /// 31 | /// A tag for the message to identify it 32 | /// 33 | [JsonProperty("tag")] public long Tag; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Relay/RelayerOptions.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Interfaces; 3 | using WalletConnectSharp.Network; 4 | 5 | namespace WalletConnectSharp.Core.Models.Relay 6 | { 7 | /// 8 | /// The options for configuring the Relayer module 9 | /// 10 | public class RelayerOptions 11 | { 12 | /// 13 | /// The ICore instance the Relayer should use. An ICore module is required as the Relayer 14 | /// module requires the core modules to function properly 15 | /// 16 | [JsonProperty("core")] public ICore Core; 17 | 18 | /// 19 | /// The URL of the Relay server to connect to. This should not include any auth information, the Relayer module 20 | /// will construct it's own auth token using the project ID specified 21 | /// 22 | [JsonProperty("relayUrl")] public string RelayUrl; 23 | 24 | /// 25 | /// The project ID to use for Relay authentication 26 | /// 27 | [JsonProperty("projectId")] 28 | public string ProjectId { get; set; } 29 | 30 | /// 31 | /// How long the should wait before throwing a during 32 | /// the connection phase. If this field is null, then the timeout will be infinite. 33 | /// 34 | public TimeSpan ConnectionTimeout { get; set; } = TimeSpan.FromSeconds(30); 35 | 36 | /// 37 | /// The interval at which the Relayer should request new (unsent) messages from the Relay server. If 38 | /// this field is null, then the Relayer will never ask for new messages, so all new messages will only 39 | /// come directly from Relay server 40 | /// 41 | public TimeSpan MessageFetchInterval { get; set; } = TimeSpan.FromSeconds(15); 42 | 43 | 44 | /// 45 | /// The module to use for building the Relay RPC URL. 46 | /// 47 | public IRelayUrlBuilder RelayUrlBuilder { get; set; } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Relay/SubscribeOptions.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Core.Models.Relay 2 | { 3 | /// 4 | /// Options for subscribing to a topic 5 | /// 6 | public class SubscribeOptions : ProtocolOptionHolder 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Relay/UnsubscribeOptions.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Relay 4 | { 5 | /// 6 | /// Options for unsubscribing to a subscription with the given id 7 | /// 8 | public class UnsubscribeOptions : ProtocolOptionHolder 9 | { 10 | /// 11 | /// The id of the subscription to unsubscribe from 12 | /// 13 | [JsonProperty("id")] 14 | public string Id; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Subscriber/ActiveSubscription.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Subscriber 4 | { 5 | /// 6 | /// Represents an active subscription with the given subscription id 7 | /// 8 | public class ActiveSubscription : PendingSubscription 9 | { 10 | /// 11 | /// The id of the subscription 12 | /// 13 | [JsonProperty("id")] 14 | public string Id; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Subscriber/BatchSubscribeParams.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Subscriber 4 | { 5 | public class BatchSubscribeParams 6 | { 7 | [JsonProperty("topics")] 8 | public string[] Topics; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Subscriber/DeletedSubscription.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Network.Models; 3 | 4 | namespace WalletConnectSharp.Core.Models.Subscriber 5 | { 6 | /// 7 | /// Represents a deleted subscription. 8 | /// 9 | public class DeletedSubscription : ActiveSubscription 10 | { 11 | /// 12 | /// The reason why the subscription was deleted 13 | /// 14 | [JsonProperty("reason")] 15 | public Error Reason; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Subscriber/JsonRpcSubscriberParams.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Subscriber 4 | { 5 | /// 6 | /// The parameters for a JSON-RPC Subscribe method call 7 | /// 8 | public class JsonRpcSubscriberParams 9 | { 10 | /// 11 | /// The topic to subscribe to 12 | /// 13 | [JsonProperty("topic")] 14 | public string Topic; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Subscriber/JsonRpcSubscriptionParams.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Subscriber 4 | { 5 | /// 6 | /// The JSON RPC parameters for a subscription message, containing the id of the 7 | /// subscription the message came from and the message data itself 8 | /// 9 | public class JsonRpcSubscriptionParams 10 | { 11 | /// 12 | /// The id of the subscription the message came from 13 | /// 14 | [JsonProperty("id")] 15 | public string Id; 16 | 17 | /// 18 | /// The message data 19 | /// 20 | [JsonProperty("data")] 21 | public MessageData Data; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Subscriber/JsonRpcUnsubscribeParams.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Subscriber 4 | { 5 | /// 6 | /// The JSON RPC Request to unsubscribe to a given subscription Id and topic 7 | /// 8 | public class JsonRpcUnsubscribeParams 9 | { 10 | /// 11 | /// The subscription id to unsubscribe from 12 | /// 13 | [JsonProperty("id")] 14 | public string Id; 15 | 16 | /// 17 | /// The topic the subscription exists in 18 | /// 19 | [JsonProperty("topic")] 20 | public string Topic; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Subscriber/MessageData.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Subscriber 4 | { 5 | /// 6 | /// The data for a specific message, containing the message and the topic the message 7 | /// came from 8 | /// 9 | public class MessageData 10 | { 11 | /// 12 | /// The topic the message came from 13 | /// 14 | [JsonProperty("topic")] 15 | public string Topic; 16 | 17 | /// 18 | /// The message as a string 19 | /// 20 | [JsonProperty("message")] 21 | public string Message { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Subscriber/PendingSubscription.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Models.Relay; 3 | 4 | namespace WalletConnectSharp.Core.Models.Subscriber 5 | { 6 | /// 7 | /// Represents a subscription that's pending 8 | /// 9 | public class PendingSubscription : SubscribeOptions 10 | { 11 | /// 12 | /// The topic that will be subscribed to 13 | /// 14 | [JsonProperty("topic")] 15 | public string Topic; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Verify/Validation.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Core.Models.Verify; 2 | 3 | public enum Validation 4 | { 5 | Unknown, 6 | Valid, 7 | Invalid, 8 | } 9 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Verify/VerifiedContext.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Core.Models.Verify; 4 | 5 | public class VerifiedContext 6 | { 7 | [JsonProperty("origin")] 8 | public string Origin; 9 | 10 | [JsonProperty("validation")] 11 | private string _validation; 12 | 13 | public string ValidationString => _validation; 14 | 15 | public Validation Validation 16 | { 17 | get 18 | { 19 | return FromString(); 20 | } 21 | set 22 | { 23 | 24 | _validation = AsString(value); 25 | } 26 | } 27 | 28 | [JsonProperty("verifyUrl")] 29 | public string VerifyUrl { get; set; } 30 | 31 | private Validation FromString() 32 | { 33 | switch (ValidationString.ToLowerInvariant()) 34 | { 35 | case "VALID": 36 | return Validation.Valid; 37 | case "INVALID": 38 | return Validation.Invalid; 39 | default: 40 | return Validation.Unknown; 41 | } 42 | } 43 | 44 | private string AsString(Validation str) 45 | { 46 | switch (str) 47 | { 48 | case Validation.Invalid: 49 | return "INVALID"; 50 | case Validation.Valid: 51 | return "VALID"; 52 | default: 53 | return "UNKNOWN"; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Models/Verify/Verifier.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Common.Logging; 3 | 4 | namespace WalletConnectSharp.Core.Models.Verify; 5 | 6 | public sealed class Verifier : IDisposable 7 | { 8 | private const string VerifyServer = "https://verify.walletconnect.com"; 9 | 10 | private readonly HttpClient _client = new() 11 | { 12 | Timeout = TimeSpan.FromSeconds(5) 13 | }; 14 | 15 | public async Task Resolve(string attestationId) 16 | { 17 | try 18 | { 19 | var url = $"{VerifyServer}/attestation/{attestationId}"; 20 | var results = await _client.GetStringAsync(url); 21 | 22 | var verifiedContext = JsonConvert.DeserializeObject(results); 23 | 24 | return verifiedContext != null ? verifiedContext.Origin : string.Empty; 25 | } 26 | catch 27 | { 28 | return string.Empty; 29 | } 30 | } 31 | 32 | public void Dispose() 33 | { 34 | _client?.Dispose(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/Utils.cs: -------------------------------------------------------------------------------- 1 | using System.Text.RegularExpressions; 2 | 3 | namespace WalletConnectSharp.Core; 4 | 5 | public static class Utils 6 | { 7 | private const string SessionIdPattern = @"^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$"; 8 | private static readonly Regex SessionIdRegex = new(SessionIdPattern, RegexOptions.None, TimeSpan.FromSeconds(1)); 9 | 10 | public static bool IsValidUrl(string url) 11 | { 12 | if (string.IsNullOrWhiteSpace(url)) return false; 13 | 14 | try 15 | { 16 | _ = new Uri(url); 17 | return true; 18 | } 19 | catch (Exception e) 20 | { 21 | return false; 22 | } 23 | } 24 | 25 | public static bool IsValidChainId(string chainId) 26 | { 27 | return SessionIdRegex.IsMatch(chainId); 28 | } 29 | 30 | public static bool IsValidAccountId(string account) 31 | { 32 | if (string.IsNullOrWhiteSpace(account) || !account.Contains(':')) 33 | { 34 | return false; 35 | } 36 | 37 | var split = account.Split(":"); 38 | if (split.Length != 3) 39 | { 40 | return false; 41 | } 42 | 43 | var chainId = split[0] + ":" + split[1]; 44 | return !string.IsNullOrWhiteSpace(split[2]) && IsValidChainId(chainId); 45 | } 46 | 47 | public static bool IsValidRequestExpiry(long expiry, long min, long max) 48 | { 49 | return expiry <= max && expiry >= min; 50 | } 51 | 52 | public static IEnumerable> Batch(this IEnumerable source, int size) 53 | { 54 | TSource[] bucket = null; 55 | var count = 0; 56 | 57 | foreach (var item in source) 58 | { 59 | if (bucket == null) 60 | bucket = new TSource[size]; 61 | 62 | bucket[count++] = item; 63 | if (count != size) 64 | continue; 65 | 66 | yield return bucket; 67 | 68 | bucket = null; 69 | count = 0; 70 | } 71 | 72 | if (bucket != null && count > 0) 73 | yield return bucket.Take(count).ToArray(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /WalletConnectSharp.Core/WalletConnectSharp.Core.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(DefaultTargetFrameworks) 5 | $(DefaultVersion) 6 | $(DefaultVersion) 7 | $(DefaultVersion) 8 | WalletConnect.Core 9 | WalletConnectSharp.Core 10 | pedrouid, gigajuwels 11 | A port of the TypeScript SDK to C#. A complete implementation of the WalletConnect v2 protocol that can be used to connect to external wallets or connect a wallet to an external Dapp 12 | Copyright (c) WalletConnect 2023 13 | https://walletconnect.org/ 14 | https://github.com/WalletConnect/WalletConnectSharp 15 | git 16 | walletconnect wallet web3 ether ethereum blockchain evm 17 | icon.png 18 | true 19 | Apache-2.0 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Controllers/PendingRequests.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Core.Controllers; 2 | using WalletConnectSharp.Core.Interfaces; 3 | using WalletConnectSharp.Sign.Interfaces; 4 | using WalletConnectSharp.Sign.Models; 5 | 6 | namespace WalletConnectSharp.Sign.Controllers; 7 | 8 | public class PendingRequests : Store, IPendingRequests 9 | { 10 | public PendingRequests(ICore core) : base(core, $"request", WalletConnectSignClient.StoragePrefix) 11 | { 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Controllers/Proposal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using WalletConnectSharp.Core.Controllers; 3 | using WalletConnectSharp.Core.Interfaces; 4 | using WalletConnectSharp.Sign.Interfaces; 5 | using WalletConnectSharp.Sign.Models; 6 | 7 | namespace WalletConnectSharp.Sign.Controllers 8 | { 9 | /// 10 | /// A module for storing 11 | /// data. This will be used 12 | /// for storing proposal data 13 | /// 14 | public class Proposal : Store, IProposal 15 | { 16 | /// 17 | /// Create a new instance of this module 18 | /// 19 | /// The instance that will be used for 20 | public Proposal(ICore core) : base(core, "proposal", WalletConnectSignClient.StoragePrefix) 21 | { 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Controllers/Session.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using WalletConnectSharp.Core.Controllers; 3 | using WalletConnectSharp.Core.Interfaces; 4 | using WalletConnectSharp.Sign.Interfaces; 5 | using WalletConnectSharp.Sign.Models; 6 | 7 | namespace WalletConnectSharp.Sign.Controllers 8 | { 9 | /// 10 | /// A module for storing 11 | /// data. This will be used 12 | /// for storing session data 13 | /// 14 | public class Session : Store, ISession 15 | { 16 | /// 17 | /// Create a new instance of this module 18 | /// 19 | /// The instance that will be used for 20 | public Session(ICore core) : base(core, "session", WalletConnectSignClient.StoragePrefix) 21 | { 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Interfaces/IAddressProvider.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Common; 2 | using WalletConnectSharp.Sign.Models; 3 | 4 | namespace WalletConnectSharp.Sign.Interfaces; 5 | 6 | public interface IAddressProvider : IModule 7 | { 8 | event EventHandler DefaultsLoaded; 9 | 10 | bool HasDefaultSession { get; } 11 | 12 | SessionStruct DefaultSession { get; set; } 13 | 14 | string DefaultNamespace { get; } 15 | 16 | string DefaultChainId { get; } 17 | 18 | ISession Sessions { get; } 19 | 20 | Task SetDefaultNamespaceAsync(string @namespace); 21 | 22 | Task SetDefaultChainIdAsync(string chainId); 23 | 24 | Caip25Address CurrentAddress(string chainId = null, SessionStruct session = default); 25 | 26 | IEnumerable AllAddresses(string @namespace = null, SessionStruct session = default); 27 | 28 | public Task LoadDefaultsAsync(); 29 | } 30 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Interfaces/IEngine.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using WalletConnectSharp.Core.Models; 4 | using WalletConnectSharp.Core.Models.Pairing; 5 | using WalletConnectSharp.Core.Models.Relay; 6 | using WalletConnectSharp.Network.Models; 7 | using WalletConnectSharp.Sign.Models; 8 | using WalletConnectSharp.Sign.Models.Engine; 9 | using WalletConnectSharp.Sign.Models.Engine.Methods; 10 | 11 | namespace WalletConnectSharp.Sign.Interfaces 12 | { 13 | /// 14 | /// An interface that represents the Engine for running the Sign client. This interface 15 | /// is an sub-type of and represents the actual Engine. This is 16 | /// different than the Sign client. 17 | /// 18 | public interface IEngine : IEngineAPI, IDisposable 19 | { 20 | /// 21 | /// The this Engine is using 22 | /// 23 | ISignClient Client { get; } 24 | 25 | /// 26 | /// Initialize the Engine. This loads any persistant state and connects to the WalletConnect 27 | /// relay server 28 | /// 29 | /// 30 | Task Init(); 31 | 32 | /// 33 | /// Parse a session proposal URI and return all information in the URI. 34 | /// 35 | /// The URI to parse 36 | /// The parameters parsed from the URI 37 | UriParameters ParseUri(string uri); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Interfaces/IPendingRequests.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Core.Interfaces; 2 | using WalletConnectSharp.Sign.Models; 3 | 4 | namespace WalletConnectSharp.Sign.Interfaces 5 | { 6 | public interface IPendingRequests : IStore 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Interfaces/IProposal.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Core.Interfaces; 2 | using WalletConnectSharp.Sign.Models; 3 | 4 | namespace WalletConnectSharp.Sign.Interfaces 5 | { 6 | /// 7 | /// A interface for a module 8 | /// that stores data. 9 | /// 10 | public interface IProposal : IStore { } 11 | } 12 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Interfaces/ISession.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Core.Interfaces; 2 | using WalletConnectSharp.Sign.Models; 3 | 4 | namespace WalletConnectSharp.Sign.Interfaces 5 | { 6 | /// 7 | /// A interface for a module 8 | /// that stores data. 9 | /// 10 | public interface ISession : IStore { } 11 | } 12 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Interfaces/ISignClient.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Common; 2 | using WalletConnectSharp.Core; 3 | using WalletConnectSharp.Core.Interfaces; 4 | using WalletConnectSharp.Sign.Models; 5 | 6 | namespace WalletConnectSharp.Sign.Interfaces 7 | { 8 | /// 9 | /// An interface for the Sign Client. This includes modules the Sign Client will use, the ICore module 10 | /// this Sign Client is using, as well as public facing Engine functions and properties. 11 | /// 12 | [Obsolete("WalletConnectSharp is now considered deprecated and will reach End-of-Life on February 17th 2025. For more details, including migration guides please see: https://docs.reown.com")] 13 | public interface ISignClient : IModule, IEngineAPI 14 | { 15 | /// 16 | /// The Metadata this Sign Client is broadcasting with 17 | /// 18 | Metadata Metadata { get; } 19 | 20 | /// 21 | /// The module that holds the logic for handling the default session & chain, and for fetching the current address 22 | /// for any session or the default session. 23 | /// 24 | IAddressProvider AddressProvider { get; } 25 | 26 | /// 27 | /// The module this Sign Client is using 28 | /// 29 | ICore Core { get; } 30 | 31 | /// 32 | /// The module this Sign Client is using 33 | /// 34 | IEngine Engine { get; } 35 | 36 | /// 37 | /// The module this Sign Client is using to store Session data 38 | /// 39 | ISession Session { get; } 40 | 41 | /// 42 | /// The module this Sign Client is using to store Proposal data 43 | /// 44 | IProposal Proposal { get; } 45 | 46 | IPendingRequests PendingRequests { get; } 47 | 48 | /// 49 | /// The options this Sign Client was initialized with 50 | /// 51 | SignClientOptions Options { get; } 52 | 53 | /// 54 | /// The protocol (represented as a string) this Sign Client is using 55 | /// 56 | string Protocol { get; } 57 | 58 | /// 59 | /// The version of this Sign Client implementation 60 | /// 61 | int Version { get; } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Interfaces/IWcMethod.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace WalletConnectSharp.Sign.Interfaces 4 | { 5 | /// 6 | /// An interface that is used to represent specific 7 | /// WalletConnect requests. 8 | /// 9 | public interface IWcMethod 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Caip25Address.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Sign.Models; 2 | 3 | public struct Caip25Address 4 | { 5 | public string Address; 6 | public string ChainId; 7 | } 8 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/DefaultsLoadingEventArgs.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Sign.Controllers; 2 | 3 | namespace WalletConnectSharp.Sign.Models; 4 | 5 | public class DefaultsLoadingEventArgs : EventArgs 6 | { 7 | public DefaultsLoadingEventArgs(in AddressProvider.DefaultData defaults) 8 | { 9 | Defaults = defaults; 10 | } 11 | 12 | public AddressProvider.DefaultData Defaults { get; } 13 | } 14 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/ApproveParams.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Sign.Models.Engine 4 | { 5 | /// 6 | /// A class that represents parameters to approve a session. Includes the id of the session proposal, 7 | /// the enabled namespaces and what relay protocol will be used in the session 8 | /// 9 | public class ApproveParams 10 | { 11 | /// 12 | /// The id of the session proposal to approve 13 | /// 14 | [JsonProperty("id")] 15 | public long Id; 16 | 17 | /// 18 | /// The enabled namespaces in this session 19 | /// 20 | [JsonProperty("namespaces")] 21 | public Namespaces Namespaces; 22 | 23 | /// 24 | /// The relay protocol that will be used in this session (as a protocol string) 25 | /// 26 | [JsonProperty("relayProtocol")] 27 | public string RelayProtocol; 28 | 29 | /// 30 | /// Custom session properties for this approval 31 | /// 32 | [JsonProperty("sessionProperties")] 33 | public Dictionary SessionProperties; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/ConnectedData.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Sign.Models.Engine; 2 | 3 | /// 4 | /// A class that representing a pending session proposal. Includes a URI that can be given to a 5 | /// wallet app out-of-band and an Approval Task that can be awaited. 6 | /// 7 | public class ConnectedData(string uri, string pairingTopic, Task approval) 8 | { 9 | /// 10 | /// The URI that can be used to retrieve the submitted session proposal. This should be shared 11 | /// SECURELY out-of-band to a wallet supporting the SDK. 12 | /// 13 | public string Uri { get; private set; } = uri; 14 | 15 | /// 16 | /// Pairing is a topic encrypted by a symmetric key shared through a URI between two clients with 17 | /// the sole purpose of communicating session proposals. 18 | /// 19 | public string PairingTopic { get; private set; } = pairingTopic; 20 | 21 | /// 22 | /// A task that will resolve to an approved session. If the session proposal is rejected, then this 23 | /// task will throw an exception. 24 | /// 25 | public Task Approval { get; private set; } = approval; 26 | } 27 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Events/EmitEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Sign.Models.Engine.Methods; 3 | 4 | namespace WalletConnectSharp.Sign.Models.Engine.Events 5 | { 6 | /// 7 | /// The event data emitted when a session event is triggered 8 | /// 9 | /// The type of the event data in the session event1 10 | public class EmitEvent : SessionEvent 11 | { 12 | /// 13 | /// The wc_sessionEvent request that triggered this event 14 | /// 15 | [JsonProperty("params")] 16 | public SessionEvent Params; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Events/EventData.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using Newtonsoft.Json.Linq; 3 | 4 | namespace WalletConnectSharp.Sign.Models.Engine.Events 5 | { 6 | /// 7 | /// The event to emit to a session, containing the event data T. 8 | /// 9 | /// The type of data inside this event 10 | public class EventData 11 | { 12 | /// 13 | /// The name of the event 14 | /// 15 | [JsonProperty("name")] 16 | public virtual string Name { get; set; } 17 | 18 | /// 19 | /// The event data associated with this event 20 | /// 21 | [JsonProperty("data")] 22 | public virtual T Data { get; set; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Events/SessionEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Sign.Models.Engine.Events 4 | { 5 | /// 6 | /// The event that is emitted when any session event occurs. Some examples 7 | /// include 8 | /// * Session Extended 9 | /// * Session Ping 10 | /// * PairingStore Ping 11 | /// * Session Delete 12 | /// * PairingStore Delete 13 | /// * Generic Session Request 14 | /// * Generic Session Event Emitted 15 | /// 16 | public class SessionEvent 17 | { 18 | /// 19 | /// The ID of the JSON Rpc request that triggered this session event 20 | /// 21 | [JsonProperty("id")] 22 | public long Id; 23 | 24 | /// 25 | /// The topic of the session this event took place in 26 | /// 27 | [JsonProperty("topic")] 28 | public string Topic; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Events/SessionProposalEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Models.Verify; 3 | 4 | namespace WalletConnectSharp.Sign.Models.Engine.Events 5 | { 6 | public class SessionProposalEvent 7 | { 8 | [JsonProperty("id")] 9 | public long Id; 10 | 11 | [JsonProperty("params")] 12 | public ProposalStruct Proposal; 13 | 14 | [JsonProperty("verifyContext")] 15 | public VerifiedContext VerifiedContext; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Events/SessionRequestEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Network; 3 | 4 | namespace WalletConnectSharp.Sign.Models.Engine.Events 5 | { 6 | /// 7 | /// An event that is emitted when a session request is received of type T. 8 | /// 9 | /// The type of request data 10 | public class SessionRequestEvent : SessionEvent 11 | { 12 | /// 13 | /// The chainId this request should be performed in 14 | /// 15 | [JsonProperty("chainId")] 16 | public string ChainId; 17 | 18 | /// 19 | /// The request arguments of this session request 20 | /// 21 | [JsonProperty("request")] 22 | public IRequestArguments Request; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Events/SessionUpdateEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Sign.Models.Engine.Methods; 3 | 4 | namespace WalletConnectSharp.Sign.Models.Engine.Events 5 | { 6 | /// 7 | /// An event that is emitted when a session is updated. 8 | /// 9 | public class SessionUpdateEvent : SessionEvent 10 | { 11 | /// 12 | /// The wc_sessionUpdate request that triggered this event 13 | /// 14 | [JsonProperty("params")] 15 | public SessionUpdate Params; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Methods/SessionDelete.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Common.Utils; 3 | using WalletConnectSharp.Network.Models; 4 | using WalletConnectSharp.Sign.Interfaces; 5 | 6 | namespace WalletConnectSharp.Sign.Models.Engine.Methods 7 | { 8 | /// 9 | /// A class that represents the request wc_sessionDelete. Used to delete 10 | /// a session 11 | /// 12 | [RpcMethod("wc_sessionDelete")] 13 | [RpcRequestOptions(Clock.ONE_DAY, 1112)] 14 | [RpcResponseOptions(Clock.ONE_DAY, 1113)] 15 | public class SessionDelete : Error, IWcMethod 16 | { 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Methods/SessionEvent.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Common.Utils; 3 | using WalletConnectSharp.Network.Models; 4 | using WalletConnectSharp.Sign.Interfaces; 5 | using WalletConnectSharp.Sign.Models.Engine.Events; 6 | 7 | namespace WalletConnectSharp.Sign.Models.Engine.Methods 8 | { 9 | /// 10 | /// A class that represents the request wc_sessionEvent. Used to emit a generic 11 | /// event 12 | /// 13 | [RpcMethod("wc_sessionEvent")] 14 | [RpcRequestOptions(Clock.FIVE_MINUTES, 1110)] 15 | [RpcResponseOptions(Clock.FIVE_MINUTES, 1111)] 16 | public class SessionEvent : IWcMethod 17 | { 18 | /// 19 | /// The chainId this event took place in 20 | /// 21 | [JsonProperty("chainId")] 22 | public string ChainId; 23 | 24 | [JsonProperty("topic")] 25 | public string Topic; 26 | 27 | /// 28 | /// The event data 29 | /// 30 | [JsonProperty("event")] 31 | public EventData Event; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Methods/SessionExtend.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using WalletConnectSharp.Common.Utils; 3 | using WalletConnectSharp.Network.Models; 4 | using WalletConnectSharp.Sign.Interfaces; 5 | 6 | namespace WalletConnectSharp.Sign.Models.Engine.Methods 7 | { 8 | /// 9 | /// A class that represents the request wc_sessionExtend. Used to extend a session 10 | /// 11 | [RpcMethod("wc_sessionExtend")] 12 | [RpcRequestOptions(Clock.ONE_DAY, 1106)] 13 | [RpcResponseOptions(Clock.ONE_DAY, 1107)] 14 | public class SessionExtend : Dictionary, IWcMethod 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Methods/SessionPing.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using WalletConnectSharp.Common.Utils; 3 | using WalletConnectSharp.Network.Models; 4 | using WalletConnectSharp.Sign.Interfaces; 5 | 6 | namespace WalletConnectSharp.Sign.Models.Engine.Methods 7 | { 8 | /// 9 | /// A class that represents the request wc_sessionPing. Used to ping a session 10 | /// 11 | [RpcMethod("wc_sessionPing")] 12 | [RpcRequestOptions(Clock.THIRTY_SECONDS, 1114)] 13 | [RpcResponseOptions(Clock.THIRTY_SECONDS, 1115)] 14 | public class SessionPing : Dictionary, IWcMethod 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Methods/SessionPropose.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Common.Utils; 3 | using WalletConnectSharp.Core.Models.Relay; 4 | using WalletConnectSharp.Network.Models; 5 | using WalletConnectSharp.Sign.Interfaces; 6 | 7 | namespace WalletConnectSharp.Sign.Models.Engine.Methods 8 | { 9 | /// 10 | /// A class that represents the request wc_sessionPropose. Used to propose a new session 11 | /// to be connected to. MUST include a and the who 12 | /// is proposing the session 13 | /// 14 | [RpcMethod("wc_sessionPropose")] 15 | [RpcRequestOptions(Clock.FIVE_MINUTES, 1100)] 16 | public class SessionPropose : IWcMethod 17 | { 18 | /// 19 | /// Protocol options that should be used during the session 20 | /// 21 | [JsonProperty("relays")] 22 | public ProtocolOptions[] Relays; 23 | 24 | /// 25 | /// The required namespaces this session will require 26 | /// 27 | [JsonProperty("requiredNamespaces")] 28 | public RequiredNamespaces RequiredNamespaces; 29 | 30 | /// 31 | /// The optional namespaces for this session 32 | /// 33 | [JsonProperty("optionalNamespaces")] 34 | public Dictionary OptionalNamespaces; 35 | 36 | /// 37 | /// Custom session properties for this session 38 | /// 39 | [JsonProperty("sessionProperties")] 40 | public Dictionary SessionProperties; 41 | 42 | /// 43 | /// The that created this session proposal 44 | /// 45 | [JsonProperty("proposer")] 46 | public Participant Proposer; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Methods/SessionProposeResponse.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Common.Utils; 3 | using WalletConnectSharp.Core.Models.Relay; 4 | using WalletConnectSharp.Network.Models; 5 | 6 | namespace WalletConnectSharp.Sign.Models.Engine.Methods 7 | { 8 | /// 9 | /// A class that represents the response to wc_sessionPropose. Used to approve a session proposal 10 | /// 11 | [RpcResponseOptions(Clock.FIVE_MINUTES, 1101)] 12 | public class SessionProposeResponse 13 | { 14 | /// 15 | /// The protocol options that should be used in this session 16 | /// 17 | [JsonProperty("relay")] 18 | public ProtocolOptions Relay; 19 | 20 | /// 21 | /// The public key of the responder to this session proposal 22 | /// 23 | [JsonProperty("responderPublicKey")] 24 | public string ResponderPublicKey; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Methods/SessionProposeResponseAutoReject.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Common.Utils; 3 | using WalletConnectSharp.Core.Models.Relay; 4 | using WalletConnectSharp.Network.Models; 5 | 6 | namespace WalletConnectSharp.Sign.Models.Engine.Methods; 7 | 8 | /// 9 | /// A class that represents the response to wc_sessionPropose. Used to approve a session proposal 10 | /// 11 | [RpcResponseOptions(Clock.FIVE_MINUTES, 1121)] 12 | public class SessionProposeResponseAutoReject 13 | { 14 | /// 15 | /// The protocol options that should be used in this session 16 | /// 17 | [JsonProperty("relay")] 18 | public ProtocolOptions Relay; 19 | 20 | /// 21 | /// The public key of the responder to this session proposal 22 | /// 23 | [JsonProperty("responderPublicKey")] 24 | public string ResponderPublicKey; 25 | } 26 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Methods/SessionProposeResponseReject.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Common.Utils; 3 | using WalletConnectSharp.Core.Models.Relay; 4 | using WalletConnectSharp.Network.Models; 5 | 6 | namespace WalletConnectSharp.Sign.Models.Engine.Methods; 7 | 8 | /// 9 | /// A class that represents the response to wc_sessionPropose. Used to reject a session proposal. 10 | /// 11 | [RpcResponseOptions(Clock.FIVE_MINUTES, 1120)] 12 | public class SessionProposeResponseReject 13 | { 14 | /// 15 | /// The protocol options that should be used in this session 16 | /// 17 | [JsonProperty("relay")] 18 | public ProtocolOptions Relay; 19 | 20 | /// 21 | /// The public key of the responder to this session proposal 22 | /// 23 | [JsonProperty("responderPublicKey")] 24 | public string ResponderPublicKey; 25 | } 26 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Methods/SessionRequest.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Common.Utils; 3 | using WalletConnectSharp.Network.Models; 4 | using WalletConnectSharp.Sign.Interfaces; 5 | 6 | namespace WalletConnectSharp.Sign.Models.Engine.Methods 7 | { 8 | /// 9 | /// A class that represents the request wc_sessionRequest. Used to send a generic JSON RPC request to the 10 | /// peer in this session. 11 | /// 12 | [RpcMethod("wc_sessionRequest")] 13 | [RpcRequestOptions(Clock.ONE_DAY, 1108)] 14 | [RpcResponseOptions(Clock.ONE_DAY, 1109)] 15 | public class SessionRequest : IWcMethod 16 | { 17 | /// 18 | /// The chainId this request should be performed in 19 | /// 20 | [JsonProperty("chainId")] 21 | public string ChainId; 22 | 23 | /// 24 | /// The JSON RPC request to send to the peer 25 | /// 26 | [JsonProperty("request")] 27 | public JsonRpcRequest Request; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Methods/SessionSettle.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Common.Utils; 3 | using WalletConnectSharp.Core.Models.Relay; 4 | using WalletConnectSharp.Network.Models; 5 | using WalletConnectSharp.Sign.Interfaces; 6 | 7 | namespace WalletConnectSharp.Sign.Models.Engine.Methods 8 | { 9 | /// 10 | /// A class that represents the request wc_sessionSettle. Used to approve and settle a proposed session. 11 | /// 12 | [RpcMethod("wc_sessionSettle")] 13 | [RpcRequestOptions(Clock.FIVE_MINUTES, 1102)] 14 | [RpcResponseOptions(Clock.FIVE_MINUTES, 1103)] 15 | public class SessionSettle : IWcMethod 16 | { 17 | /// 18 | /// Pairing topic for this session 19 | /// 20 | [JsonProperty("pairingTopic")] 21 | [Obsolete("This isn't a standard property of the Sign API. Other Sign implementations may not support this property whcih could lead to unexpected behavior.")] 22 | public string PairingTopic; 23 | 24 | /// 25 | /// The protocol options that should be used in this session 26 | /// 27 | [JsonProperty("relay")] 28 | public ProtocolOptions Relay; 29 | 30 | /// 31 | /// All namespaces that are enabled in this session 32 | /// 33 | [JsonProperty("namespaces")] 34 | public Namespaces Namespaces; 35 | 36 | /// 37 | /// When this session will expire 38 | /// 39 | [JsonProperty("expiry")] 40 | public long Expiry; 41 | 42 | /// 43 | /// The controlling in this session. In most cases, this is the dApp. 44 | /// 45 | [JsonProperty("controller")] 46 | public Participant Controller; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/Methods/SessionUpdate.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Common.Utils; 3 | using WalletConnectSharp.Network.Models; 4 | using WalletConnectSharp.Sign.Interfaces; 5 | 6 | namespace WalletConnectSharp.Sign.Models.Engine.Methods 7 | { 8 | /// 9 | /// A class that represents the request wc_sessionUpdate. Used to update the enabled 10 | /// in this session 11 | /// 12 | [RpcMethod("wc_sessionUpdate")] 13 | [RpcRequestOptions(Clock.ONE_DAY, 1104)] 14 | [RpcResponseOptions(Clock.ONE_DAY, 1105)] 15 | public class SessionUpdate : IWcMethod 16 | { 17 | /// 18 | /// The updated namespaces that are enabled for this session 19 | /// 20 | [JsonProperty("namespaces")] 21 | public Namespaces Namespaces; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Engine/RejectParams.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Network.Models; 3 | 4 | namespace WalletConnectSharp.Sign.Models.Engine 5 | { 6 | /// 7 | /// A class that represents parameters for rejecting a session proposal. Contains the id 8 | /// of the session proposal to reject and the reason the session 9 | /// proposal was rejected 10 | /// 11 | public class RejectParams 12 | { 13 | /// 14 | /// The id of the session proposal to reject 15 | /// 16 | [JsonProperty("id")] 17 | public long Id; 18 | 19 | /// 20 | /// The reason the session proposal was rejected, as an 21 | /// 22 | [JsonProperty("reason")] 23 | public Error Reason; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Namespaces.cs: -------------------------------------------------------------------------------- 1 | namespace WalletConnectSharp.Sign.Models 2 | { 3 | /// 4 | /// A dictionary of Namespaces based on a chainId key. The chainId 5 | /// should follow CAIP-2 format 6 | /// chain_id: namespace + ":" + reference 7 | /// namespace: [-a-z0-9]{3,8} 8 | /// reference: [-_a-zA-Z0-9]{1,32} 9 | /// 10 | public class Namespaces : SortedDictionary 11 | { 12 | public Namespaces() : base() { } 13 | 14 | public Namespaces(Namespaces namespaces) : base(namespaces) 15 | { 16 | } 17 | 18 | public Namespaces(RequiredNamespaces requiredNamespaces) 19 | { 20 | WithProposedNamespaces(requiredNamespaces); 21 | } 22 | 23 | public Namespaces(Dictionary proposedNamespaces) 24 | { 25 | WithProposedNamespaces(proposedNamespaces); 26 | } 27 | 28 | public Namespaces WithNamespace(string chainNamespace, Namespace nm) 29 | { 30 | Add(chainNamespace, nm); 31 | return this; 32 | } 33 | 34 | public Namespace At(string chainNamespace) 35 | { 36 | return this[chainNamespace]; 37 | } 38 | 39 | public Namespaces WithProposedNamespaces(IDictionary proposedNamespaces) 40 | { 41 | foreach (var (chainNamespace, requiredNamespace) in proposedNamespaces) 42 | { 43 | Add(chainNamespace, new Namespace(requiredNamespace)); 44 | } 45 | 46 | return this; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/Participant.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core; 3 | using WalletConnectSharp.Core.Models; 4 | using WalletConnectSharp.Core.Models.Pairing; 5 | 6 | namespace WalletConnectSharp.Sign.Models 7 | { 8 | /// 9 | /// A class that represents a participant. This store the public key and additional metadata 10 | /// about the participant 11 | /// 12 | public class Participant 13 | { 14 | /// 15 | /// The public key of this participant, encoded as a hex string 16 | /// 17 | [JsonProperty("publicKey")] 18 | public string PublicKey; 19 | 20 | /// 21 | /// The metadata for this participant 22 | /// 23 | [JsonProperty("metadata")] 24 | public Metadata Metadata; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/PendingRequestStruct.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Core.Interfaces; 2 | using WalletConnectSharp.Sign.Models.Engine.Methods; 3 | 4 | namespace WalletConnectSharp.Sign.Models; 5 | 6 | public struct PendingRequestStruct : IKeyHolder 7 | { 8 | public long Id; 9 | 10 | public string Topic; 11 | 12 | public long Key 13 | { 14 | get 15 | { 16 | return Id; 17 | } 18 | } 19 | 20 | // Specify object here, so we can store any type 21 | // We don't care about type-safety for these pending 22 | // requests 23 | public SessionRequest Parameters; 24 | } 25 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/RequiredNamespaces.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using WalletConnectSharp.Common.Utils; 3 | 4 | namespace WalletConnectSharp.Sign.Models 5 | { 6 | /// 7 | /// A dictionary of RequiredNamespace based on a chainId key. The chainId 8 | /// should follow CAIP-2 format 9 | /// chain_id: namespace + ":" + reference 10 | /// namespace: [-a-z0-9]{3,8} 11 | /// reference: [-_a-zA-Z0-9]{1,32} 12 | /// 13 | public class RequiredNamespaces : SortedDictionary 14 | { 15 | public RequiredNamespaces WithProposedNamespace(string chainNamespace, ProposedNamespace proposedNamespace) 16 | { 17 | Add(chainNamespace, proposedNamespace); 18 | return this; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/Models/SignClientOptions.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core; 3 | using WalletConnectSharp.Core.Interfaces; 4 | using WalletConnectSharp.Core.Models; 5 | using WalletConnectSharp.Core.Models.Pairing; 6 | 7 | namespace WalletConnectSharp.Sign.Models 8 | { 9 | /// 10 | /// Options for setting up the class. Includes 11 | /// options from 12 | /// 13 | public class SignClientOptions : CoreOptions 14 | { 15 | /// 16 | /// The instance the should use. If 17 | /// left null, then a new Core module will be created and initialized 18 | /// 19 | [JsonProperty("core")] 20 | public ICore Core; 21 | 22 | /// 23 | /// The Metadata the should broadcast with 24 | /// 25 | [JsonProperty("metadata")] 26 | public Metadata Metadata; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /WalletConnectSharp.Sign/WalletConnectSharp.Sign.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(DefaultTargetFrameworks) 5 | $(DefaultVersion) 6 | $(DefaultVersion) 7 | $(DefaultVersion) 8 | WalletConnect.Sign 9 | WalletConnectSharp.Sign 10 | pedrouid, gigajuwels 11 | A port of the TypeScript SDK to C#. A complete implementation of the WalletConnect v2 protocol that can be used to connect to external wallets or connect a wallet to an external Dapp 12 | Copyright (c) WalletConnect 2023 13 | https://walletconnect.org/ 14 | icon.png 15 | https://github.com/WalletConnect/WalletConnectSharp 16 | git 17 | walletconnect sign wallet web3 ether ethereum blockchain evm 18 | true 19 | Apache-2.0 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /WalletConnectSharp.Web3Wallet/Interfaces/IWeb3Wallet.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Common; 2 | using WalletConnectSharp.Core; 3 | using WalletConnectSharp.Core.Interfaces; 4 | 5 | namespace WalletConnectSharp.Web3Wallet.Interfaces; 6 | 7 | [Obsolete("WalletConnectSharp is now considered deprecated and will reach End-of-Life on February 17th 2025. For more details, including migration guides please see: https://docs.reown.com")] 8 | public interface IWeb3Wallet : IModule, IWeb3WalletApi 9 | { 10 | IWeb3WalletEngine Engine { get; } 11 | 12 | ICore Core { get; } 13 | 14 | Metadata Metadata { get; } 15 | } 16 | -------------------------------------------------------------------------------- /WalletConnectSharp.Web3Wallet/Interfaces/IWeb3WalletApi.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Auth; 2 | using WalletConnectSharp.Auth.Interfaces; 3 | using WalletConnectSharp.Auth.Models; 4 | using WalletConnectSharp.Network.Models; 5 | using WalletConnectSharp.Sign.Models; 6 | using WalletConnectSharp.Sign.Models.Engine.Events; 7 | 8 | namespace WalletConnectSharp.Web3Wallet.Interfaces; 9 | 10 | public interface IWeb3WalletApi : IAuthClientEvents 11 | { 12 | event EventHandler SessionExpired; 13 | 14 | event EventHandler SessionProposed; 15 | 16 | event EventHandler SessionConnected; 17 | 18 | event EventHandler SessionConnectionErrored; 19 | 20 | event EventHandler SessionUpdated; 21 | 22 | event EventHandler SessionExtended; 23 | 24 | event EventHandler SessionPinged; 25 | 26 | event EventHandler SessionDeleted; 27 | 28 | IDictionary ActiveSessions { get; } 29 | 30 | IDictionary PendingSessionProposals { get; } 31 | 32 | PendingRequestStruct[] PendingSessionRequests { get; } 33 | 34 | IDictionary PendingAuthRequests { get; } 35 | 36 | Task Pair(string uri, bool activatePairing = false); 37 | 38 | Task ApproveSession(long id, Namespaces namespaces, string relayProtocol = null); 39 | 40 | Task ApproveSession(ProposalStruct proposal, params string[] approvedAddresses); 41 | 42 | Task RejectSession(long id, Error reason); 43 | 44 | Task RejectSession(ProposalStruct proposal, Error reason); 45 | 46 | 47 | Task RejectSession(ProposalStruct proposal, string reason); 48 | 49 | Task UpdateSession(string topic, Namespaces namespaces); 50 | 51 | Task ExtendSession(string topic); 52 | 53 | Task RespondSessionRequest(string topic, JsonRpcResponse response); 54 | 55 | Task EmitSessionEvent(string topic, EventData eventData, string chainId); 56 | 57 | Task DisconnectSession(string topic, Error reason); 58 | 59 | Task RespondAuthRequest(ResultResponse results, string iss); 60 | 61 | Task RespondAuthRequest(AuthErrorResponse error, string iss); 62 | 63 | Task RespondAuthRequest(AuthRequest request, Error error, string iss); 64 | 65 | Task RespondAuthRequest(AuthRequest request, string signature, string iss, bool eip191 = true); 66 | 67 | string FormatMessage(Cacao.CacaoRequestPayload payload, string iss); 68 | } 69 | -------------------------------------------------------------------------------- /WalletConnectSharp.Web3Wallet/Interfaces/IWeb3WalletEngine.cs: -------------------------------------------------------------------------------- 1 | using WalletConnectSharp.Auth; 2 | using WalletConnectSharp.Auth.Interfaces; 3 | using WalletConnectSharp.Auth.Models; 4 | using WalletConnectSharp.Network.Models; 5 | using WalletConnectSharp.Sign.Interfaces; 6 | using WalletConnectSharp.Sign.Models; 7 | 8 | namespace WalletConnectSharp.Web3Wallet.Interfaces; 9 | 10 | public interface IWeb3WalletEngine : IWeb3WalletApi 11 | { 12 | ISignClient SignClient { get; } 13 | 14 | IAuthClient AuthClient { get; } 15 | 16 | IWeb3Wallet Client { get; } 17 | 18 | Task Init(); 19 | } 20 | -------------------------------------------------------------------------------- /WalletConnectSharp.Web3Wallet/Models/BaseEventArgs.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace WalletConnectSharp.Web3Wallet.Models; 4 | 5 | public class BaseEventArgs : EventArgs 6 | { 7 | [JsonProperty("id")] 8 | public long Id; 9 | 10 | [JsonProperty("topic")] 11 | public string Topic { get; set; } 12 | 13 | [JsonProperty("params")] 14 | public T Parameters { get; set; } 15 | } 16 | -------------------------------------------------------------------------------- /WalletConnectSharp.Web3Wallet/Models/SessionRequestEventArgs.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using WalletConnectSharp.Core.Models.Verify; 3 | using WalletConnectSharp.Sign.Models.Engine.Methods; 4 | 5 | namespace WalletConnectSharp.Web3Wallet.Models; 6 | 7 | public class SessionRequestEventArgs : BaseEventArgs> 8 | { 9 | [JsonProperty("verifyContext")] 10 | public VerifiedContext VerifyContext; 11 | } 12 | -------------------------------------------------------------------------------- /WalletConnectSharp.Web3Wallet/WalletConnectSharp.Web3Wallet.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(DefaultTargetFrameworks) 5 | $(DefaultVersion) 6 | $(DefaultVersion) 7 | $(DefaultVersion) 8 | WalletConnect.Web3Wallet 9 | WalletConnectSharp.Web3Wallet 10 | pedrouid, gigajuwels 11 | A port of the TypeScript SDK to C#. A complete implementation of the WalletConnect v2 protocol that can be used to connect to external wallets or connect a wallet to an external Dapp 12 | Copyright (c) WalletConnect 2023 13 | https://walletconnect.org/ 14 | icon.png 15 | https://github.com/WalletConnect/WalletConnectSharp 16 | git 17 | web3wallet walletconnect sign wallet web3 ether ethereum blockchain evm 18 | true 19 | Apache-2.0 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /WalletConnectSharpV2.sln.DotSettings: -------------------------------------------------------------------------------- 1 |  2 | True -------------------------------------------------------------------------------- /global.json: -------------------------------------------------------------------------------- 1 | { 2 | "sdk": { 3 | "version": "8.0.100", 4 | "rollForward": "latestMajor", 5 | "allowPrerelease": false 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /resources/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WalletConnect/WalletConnectSharp/d04c4d3a5ac1ebdfd2adde4090e760bfed51553f/resources/icon.png --------------------------------------------------------------------------------