Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
a4c8e947
Commit
a4c8e947
authored
Apr 10, 2015
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
whisper: make the test app runnable & do something inside
parent
fc1d1f9a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
12 deletions
+65
-12
main.go
whisper/main.go
+65
-12
No files found.
whisper/main.go
View file @
a4c8e947
// +build none
// +build none
// Contains a simple whisper peer setup and self messaging to allow playing
// around with the protocol and API without a fancy client implementation.
package
main
package
main
import
(
import
(
"fmt"
"fmt"
"log"
"log"
"os"
"os"
"time"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/whisper"
"github.com/ethereum/go-ethereum/whisper"
)
)
func
main
()
{
func
main
()
{
logger
.
AddLogSystem
(
logger
.
NewStdLogSystem
(
os
.
Stdout
,
log
.
LstdFlags
,
logger
.
InfoLevel
))
logger
.
AddLogSystem
(
logger
.
NewStdLogSystem
(
os
.
Stdout
,
log
.
LstdFlags
,
logger
.
InfoLevel
))
pub
,
_
:=
secp256k1
.
GenerateKeyPair
()
// Generate the peer identity
key
,
err
:=
crypto
.
GenerateKey
()
whisper
:=
whisper
.
New
()
if
err
!=
nil
{
fmt
.
Printf
(
"Failed to generate peer key: %v.
\n
"
,
err
)
os
.
Exit
(
-
1
)
}
name
:=
common
.
MakeName
(
"whisper-go"
,
"1.0"
)
shh
:=
whisper
.
New
()
srv
:=
p2p
.
Server
{
// Create an Ethereum peer to communicate through
server
:=
p2p
.
Server
{
PrivateKey
:
key
,
MaxPeers
:
10
,
MaxPeers
:
10
,
Identity
:
p2p
.
NewSimpleClientIdentity
(
"whisper-go"
,
"1.0"
,
""
,
string
(
pub
)),
Name
:
name
,
Protocols
:
[]
p2p
.
Protocol
{
shh
.
Protocol
()},
ListenAddr
:
":30300"
,
ListenAddr
:
":30300"
,
NAT
:
p2p
.
UPNP
(),
NAT
:
nat
.
Any
(),
Protocols
:
[]
p2p
.
Protocol
{
whisper
.
Protocol
()},
}
}
if
err
:=
srv
.
Start
();
err
!=
nil
{
fmt
.
Println
(
"Starting Ethereum peer..."
)
fmt
.
Println
(
"could not start server:"
,
err
)
if
err
:=
server
.
Start
();
err
!=
nil
{
fmt
.
Printf
(
"Failed to start Ethereum peer: %v.
\n
"
,
err
)
os
.
Exit
(
1
)
os
.
Exit
(
1
)
}
}
select
{}
// Send a message to self to check that something works
payload
:=
fmt
.
Sprintf
(
"Hello world, this is %v. In case you're wondering, the time is %v"
,
name
,
time
.
Now
())
if
err
:=
selfSend
(
shh
,
[]
byte
(
payload
));
err
!=
nil
{
fmt
.
Printf
(
"Failed to self message: %v.
\n
"
,
err
)
os
.
Exit
(
-
1
)
}
}
// SendSelf wraps a payload into a Whisper envelope and forwards it to itself.
func
selfSend
(
shh
*
whisper
.
Whisper
,
payload
[]
byte
)
error
{
ok
:=
make
(
chan
struct
{})
// Start watching for self messages, output any arrivals
id
:=
shh
.
NewIdentity
()
shh
.
Watch
(
whisper
.
Filter
{
To
:
&
id
.
PublicKey
,
Fn
:
func
(
msg
*
whisper
.
Message
)
{
fmt
.
Printf
(
"Message received: %s, signed with 0x%x.
\n
"
,
string
(
msg
.
Payload
),
msg
.
Signature
)
close
(
ok
)
},
})
// Wrap the payload and encrypt it
msg
:=
whisper
.
NewMessage
(
payload
)
envelope
,
err
:=
msg
.
Seal
(
whisper
.
DefaultPow
,
whisper
.
Opts
{
Ttl
:
whisper
.
DefaultTtl
,
From
:
id
,
To
:
&
id
.
PublicKey
,
})
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to seal message: %v"
,
err
)
}
// Dump the message into the system and wait for it to pop back out
if
err
:=
shh
.
Send
(
envelope
);
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to send self-message: %v"
,
err
)
}
select
{
case
<-
ok
:
case
<-
time
.
After
(
time
.
Second
)
:
return
fmt
.
Errorf
(
"failed to receive message in time"
)
}
return
nil
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment