This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
middleware: tunnel electrum through the noise channel
- Loading branch information
Showing
8 changed files
with
146 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package electrum | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
) | ||
|
||
// Electrum makes a connection to an Electrum server and proxies messages. | ||
type Electrum struct { | ||
connection net.Conn | ||
onMessageReceived func([]byte) | ||
} | ||
|
||
// NewElectrum creates a new Electrum instance and tries to connect to the server. | ||
func NewElectrum(address string, onMessageReceived func([]byte)) (*Electrum, error) { | ||
connection, err := net.Dial("tcp", address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
electrum := &Electrum{ | ||
connection: connection, | ||
onMessageReceived: onMessageReceived, | ||
} | ||
go electrum.read() | ||
return electrum, nil | ||
} | ||
|
||
func (electrum *Electrum) read() { | ||
reader := bufio.NewReader(electrum.connection) | ||
for { | ||
line, err := reader.ReadBytes(byte('\n')) | ||
if err != nil { | ||
log.Error(fmt.Sprintf("electrum read error: %v", err)) | ||
break | ||
} | ||
electrum.onMessageReceived(line) | ||
} | ||
} | ||
|
||
// Send sends a raw message to the Electrum server. | ||
func (electrum *Electrum) Send(msg []byte) error { | ||
_, err := electrum.connection.Write(msg) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters