@@ -414,4 +414,173 @@ For advanced configuration options, you can provide additional options to the Ch
414
414
415
415
- [ InterchainJS Documentation] ( https://docs.hyperweb.io/interchainjs )
416
416
- [ Chain Registry Documentation] ( https://github.com/hyperweb-io/chain-registry )
417
- - [ Example Applications] ( https://github.com/hyperweb-io/interchain-kit/tree/main/examples )
417
+ - [ Example Applications] ( https://github.com/hyperweb-io/interchain-kit/tree/main/examples )
418
+
419
+ ## Signing Arbitrary Messages with Cosmos Wallet
420
+
421
+ Interchain Kit allows you to sign arbitrary messages using a Cosmos Wallet:
422
+
423
+ ``` tsx
424
+ import {
425
+ CosmosWallet ,
426
+ ExtensionWallet ,
427
+ isInstanceOf ,
428
+ MultiChainWallet ,
429
+ } from " @interchain-kit/core" ;
430
+ import { useChain } from " @interchain-kit/react" ;
431
+ import { useState } from " react" ;
432
+
433
+ const CosmosWalletExample: React .FC = () => {
434
+ // Connect to a specific chain
435
+ const { wallet, connect, address, disconnect, chain } =
436
+ useChain (" osmosistestnet" );
437
+
438
+ const [message, setMessage] = useState (" " );
439
+ const [signature, setSignature] = useState (" " );
440
+
441
+ const handleConnect = async () => {
442
+ connect ();
443
+ };
444
+
445
+ const handleDisconnect = async () => {
446
+ disconnect ();
447
+ };
448
+
449
+ const handleSignArbitrary = async () => {
450
+
451
+ // get specific wallet to use its own methods
452
+
453
+ const cosmosWallet = wallet .getWalletOfType (CosmosWallet );
454
+
455
+ if (cosmosWallet ) {
456
+ const signed = await cosmosWallet .signArbitrary (
457
+ chain .chainId as string ,
458
+ address ,
459
+ message
460
+ );
461
+
462
+ setSignature (signed .signature );
463
+ }
464
+ };
465
+
466
+ return (
467
+ <div >
468
+ <h1 >Cosmos Wallet</h1 >
469
+ { address ? (
470
+ <div >
471
+ <p >Connected Address: { address } </p >
472
+ <button onClick = { handleDisconnect } >Disconnect</button >
473
+ </div >
474
+ ) : (
475
+ <button onClick = { handleConnect } >Connect Wallet</button >
476
+ )}
477
+
478
+ <div >
479
+ <h2 >Sign Arbitrary Message</h2 >
480
+ <textarea
481
+ rows = { 4 }
482
+ cols = { 50 }
483
+ placeholder = " Enter a message to sign"
484
+ value = { message }
485
+ onChange = { (e ) => setMessage (e .target .value )}
486
+ />
487
+ <br />
488
+ <button onClick = { handleSignArbitrary } >
489
+ Sign Message
490
+ </button >
491
+ { signature && (
492
+ <div >
493
+ <h3 >Signature:</h3 >
494
+ <pre >{ signature } </pre >
495
+ </div >
496
+ )}
497
+ </div >
498
+ </div >
499
+ );
500
+ };
501
+ ```
502
+
503
+ This example demonstrates how to:
504
+ 1 . Connect to a specific chain using ` useChain `
505
+ 2 . Get a Cosmos wallet instance using ` wallet.getWalletOfType(CosmosWallet) `
506
+ 3 . Sign arbitrary messages with ` cosmosWallet.signArbitrary() `
507
+ 4 . Display the resulting signature to the user
508
+
509
+ ## Signing Messages with Ethereum Wallet
510
+
511
+ Interchain Kit also allows you to sign messages using an Ethereum Wallet:
512
+
513
+ ``` tsx
514
+ import { EthereumWallet } from " @interchain-kit/core" ;
515
+ import { useChain } from " @interchain-kit/react" ;
516
+ import React , { useState } from " react" ;
517
+
518
+ const EthereumWalletExample: React .FC = () => {
519
+ const [message, setMessage] = useState <string >(" " );
520
+ const [signature, setSignature] = useState <string | null >(null );
521
+
522
+ // Connect to Ethereum chain
523
+ const { connect, address, disconnect, wallet } = useChain (" ethereum" );
524
+
525
+ const connectWallet = async () => {
526
+ connect ();
527
+ };
528
+
529
+ const disconnectWallet = async () => {
530
+ disconnect ();
531
+ setSignature (null );
532
+ };
533
+
534
+ const signMessage = async () => {
535
+
536
+ // get specific wallet to use its own methods
537
+
538
+ const ethereumWallet = wallet .getWalletOfType (EthereumWallet );
539
+ if (ethereumWallet ) {
540
+ try {
541
+ const signedMessage = await ethereumWallet .signMessage (message );
542
+ setSignature (signedMessage );
543
+ } catch (error ) {
544
+ console .error (" Error signing message:" , error );
545
+ }
546
+ }
547
+ };
548
+
549
+ return (
550
+ <div >
551
+ <h2 >Ethereum Sign Message Example</h2 >
552
+ { ! address ? (
553
+ <button onClick = { connectWallet } >Connect Wallet</button >
554
+ ) : (
555
+ <button onClick = { disconnectWallet } >Disconnect Wallet</button >
556
+ )}
557
+ <div >
558
+ <div >
559
+ <strong >Connected:</strong > { address }
560
+ </div >
561
+ <textarea
562
+ rows = { 3 }
563
+ style = { { width: " 100%" , marginTop: 16 }}
564
+ value = { message }
565
+ onChange = { (e ) => setMessage (e .target .value )}
566
+ />
567
+ <button style = { { marginTop: 16 }} onClick = { signMessage } >
568
+ Sign Message
569
+ </button >
570
+ </div >
571
+ { signature && (
572
+ <div style = { { marginTop: 16 }} >
573
+ <strong >Signature:</strong >
574
+ <pre >{ signature } </pre >
575
+ </div >
576
+ )}
577
+ </div >
578
+ );
579
+ };
580
+ ```
581
+
582
+ This example demonstrates how to:
583
+ 1 . Connect to the Ethereum chain using ` useChain("ethereum") `
584
+ 2 . Get an Ethereum wallet instance using ` wallet.getWalletOfType(EthereumWallet) `
585
+ 3 . Sign messages with ` ethereumWallet.signMessage() `
586
+ 4 . Display the resulting signature to the user
0 commit comments