Created
April 11, 2019 08:41
-
-
Save yenda/c602bfc48803e052f63688cd881d99b4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package feed | |
import ("fmt" | |
"encoding/json" | |
"reflect" | |
"github.com/ethereum/go-ethereum/crypto" | |
"github.com/ethereum/go-ethereum/swarm/storage/feed" | |
"github.com/ethereum/go-ethereum/swarm/storage/feed/lookup") | |
func newBobSigner() *GenericSigner { | |
privKey, _ := crypto.HexToECDSA("accedeaccedeaccedeaccedeaccedeaccedeaccedeaccedeaccedeaccedecaca") | |
return NewGenericSigner(privKey) | |
} | |
func newCharlieSigner() *GenericSigner { | |
privKey, _ := crypto.HexToECDSA("facadefacadefacadefacadefacadefacadefacadefacadefacadefacadefaca") | |
return NewGenericSigner(privKey) | |
} | |
func areEqualJSON(s1, s2 string) (bool, error) { | |
//credit for the trick: turtlemonvh https://gist.github.com/turtlemonvh/e4f7404e28387fadb8ad275a99596f67 | |
var o1 interface{} | |
var o2 interface{} | |
err := json.Unmarshal([]byte(s1), &o1) | |
if err != nil { | |
return false, fmt.Errorf("Error mashalling string 1 :: %s", err.Error()) | |
} | |
err = json.Unmarshal([]byte(s2), &o2) | |
if err != nil { | |
return false, fmt.Errorf("Error mashalling string 2 :: %s", err.Error()) | |
} | |
return reflect.DeepEqual(o1, o2), nil | |
} | |
// TestEncodingDecodingUpdateRequests ensures that requests are serialized properly | |
// while also checking cryptographically that only the owner of a feed can update it. | |
func TestEncodingDecodingUpdateRequests() { | |
charlie := newCharlieSigner() //Charlie | |
bob := newBobSigner() //Bob | |
// Create a feed to our good guy Charlie's name | |
topic, _ := NewTopic("a good topic name", nil) | |
firstRequest := NewFirstRequest(topic) | |
firstRequest.User = charlie.Address() | |
// We now encode the create message to simulate we send it over the wire | |
messageRawData, err := firstRequest.MarshalJSON() | |
// ... the message arrives and is decoded... | |
var recoveredFirstRequest Request | |
// We now assume that the feed ypdate was created and propagated. | |
const expectedSignature = "0x7235b27a68372ddebcf78eba48543fa460864b0b0e99cb533fcd3664820e603312d29426dd00fb39628f5299480a69bf6e462838d78de49ce0704c754c9deb2601" | |
const expectedJSON = `{"feed":{"topic":"0x6120676f6f6420746f706963206e616d65000000000000000000000000000000","user":"0x876a8936a7cd0b79ef0735ad0896c1afe278781c"},"epoch":{"time":1000,"level":1},"protocolVersion":0,"data":"0x5468697320686f75722773207570646174653a20537761726d2039392e3020686173206265656e2072656c656173656421"}` | |
//Put together an unsigned update request that we will serialize to send it to the signer. | |
data := []byte("This hour's update: Swarm 99.0 has been released!") | |
request := &Request{ | |
Update: Update{ | |
ID: ID{ | |
Epoch: lookup.Epoch{ | |
Time: 1000, | |
Level: 1, | |
}, | |
Feed: firstRequest.Update.Feed, | |
}, | |
data: data, | |
}, | |
} | |
messageRawData, err = request.MarshalJSON() | |
equalJSON, err := areEqualJSON(string(messageRawData), expectedJSON) | |
// now the encoded message messageRawData is sent over the wire and arrives to the signer | |
//Attempt to extract an UpdateRequest out of the encoded message | |
var recoveredRequest Request | |
//sign the request and see if it matches our predefined signature above. | |
} | |
func main() { | |
fmt.Println("Hello, world.") | |
TestEncodingDecodingUpdateRequests() | |
fmt.Println("Hello, world.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment