-
Notifications
You must be signed in to change notification settings - Fork 110
Client-side MRU signing -- step 5/5 #704
Changes from all commits
fbdf8a9
82e838c
ebe3d5d
b136f96
67ab8c2
9e5c686
e3f7669
1d390b3
6690c5c
d23aec9
c60cfa7
b88d480
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,65 +30,109 @@ import ( | |
| "gopkg.in/urfave/cli.v1" | ||
| ) | ||
|
|
||
| // swarm resource create <name> <frequency> [--rawmru] <0x Hexdata> | ||
| // swarm resource update <Manifest Address or ENS domain> <0x Hexdata> | ||
| func NewGenericSigner(ctx *cli.Context) mru.Signer { | ||
| return mru.NewGenericSigner(getClientAccount(ctx)) | ||
| } | ||
|
|
||
| // swarm resource create <frequency> [--name <name>] [--data <0x Hexdata> [--multihash=false]] | ||
| // swarm resource update <Manifest Address or ENS domain> <0x Hexdata> [--multihash=false] | ||
| // swarm resource info <Manifest Address or ENS domain> | ||
|
|
||
| func resourceCreate(ctx *cli.Context) { | ||
| args := ctx.Args() | ||
|
|
||
| var ( | ||
| bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") | ||
| client = swarm.NewClient(bzzapi) | ||
| rawResource = ctx.Bool(SwarmResourceRawFlag.Name) | ||
| multihash = ctx.Bool(SwarmResourceMultihashFlag.Name) | ||
| initialData = ctx.String(SwarmResourceDataOnCreateFlag.Name) | ||
| name = ctx.String(SwarmResourceNameFlag.Name) | ||
| ) | ||
|
|
||
| if len(args) < 3 { | ||
| if len(args) < 1 { | ||
| fmt.Println("Incorrect number of arguments") | ||
| cli.ShowCommandHelpAndExit(ctx, "create", 1) | ||
| return | ||
| } | ||
| signer := mru.NewGenericSigner(getClientAccount(ctx)) | ||
|
|
||
| name := args[0] | ||
| frequency, err := strconv.ParseUint(args[1], 10, 64) | ||
| signer := NewGenericSigner(ctx) | ||
| frequency, err := strconv.ParseUint(args[0], 10, 64) | ||
| if err != nil { | ||
| utils.Fatalf("Frequency formatting error: %s", err.Error()) | ||
| fmt.Printf("Frequency formatting error: %s\n", err.Error()) | ||
| cli.ShowCommandHelpAndExit(ctx, "create", 1) | ||
| return | ||
| } | ||
|
|
||
| data, err := hexutil.Decode(args[2]) | ||
| newResourceRequest, err := mru.NewCreateRequest(&mru.ResourceMetadata{ | ||
| Name: name, | ||
| Frequency: frequency, | ||
| Owner: signer.Address(), | ||
| }) | ||
|
|
||
| if err != nil { | ||
| utils.Fatalf("Error parsing data: %s", err.Error()) | ||
| return | ||
| utils.Fatalf("Error creating new resource request: %s", err) | ||
| } | ||
|
|
||
| if initialData != "" { | ||
| initialDataBytes, err := hexutil.Decode(initialData) | ||
|
||
| if err != nil { | ||
| fmt.Printf("Error parsing data: %s\n", err.Error()) | ||
| cli.ShowCommandHelpAndExit(ctx, "create", 1) | ||
| return | ||
| } | ||
|
|
||
| newResourceRequest.SetData(initialDataBytes, multihash) | ||
| if err = newResourceRequest.Sign(signer); err != nil { | ||
| utils.Fatalf("Error signing resource update: %s", err.Error()) | ||
| } | ||
| } | ||
| manifestAddress, err := client.CreateResource(name, frequency, 0, data, !rawResource, signer) | ||
|
|
||
| manifestAddress, err := client.CreateResource(newResourceRequest) | ||
| if err != nil { | ||
| utils.Fatalf("Error creating resource: %s", err.Error()) | ||
| return | ||
| } | ||
| fmt.Println(manifestAddress) // output address to the user in a single line (useful for other commands to pick up) | ||
| fmt.Println(manifestAddress) // output manifest address to the user in a single line (useful for other commands to pick up) | ||
|
|
||
| } | ||
|
|
||
| func resourceUpdate(ctx *cli.Context) { | ||
| args := ctx.Args() | ||
|
|
||
| var ( | ||
| bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") | ||
| client = swarm.NewClient(bzzapi) | ||
| bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") | ||
| client = swarm.NewClient(bzzapi) | ||
| multihash = ctx.Bool(SwarmResourceMultihashFlag.Name) | ||
| ) | ||
|
|
||
| if len(args) < 2 { | ||
| fmt.Println("Incorrect number of arguments") | ||
| cli.ShowCommandHelpAndExit(ctx, "update", 1) | ||
| return | ||
| } | ||
| signer := mru.NewGenericSigner(getClientAccount(ctx)) | ||
| manifestAddressOrDomain := args[1] | ||
| data, err := hexutil.Decode(args[2]) | ||
| signer := NewGenericSigner(ctx) | ||
| manifestAddressOrDomain := args[0] | ||
| data, err := hexutil.Decode(args[1]) | ||
| if err != nil { | ||
| utils.Fatalf("Error parsing data: %s", err.Error()) | ||
| return | ||
| } | ||
| err = client.UpdateResource(manifestAddressOrDomain, data, signer) | ||
|
|
||
| // Retrieve resource status and metadata out of the manifest | ||
| updateRequest, err := client.GetResourceMetadata(manifestAddressOrDomain) | ||
| if err != nil { | ||
| utils.Fatalf("Error retrieving resource status: %s", err.Error()) | ||
| } | ||
|
|
||
| // set the new data | ||
| updateRequest.SetData(data, multihash) | ||
|
|
||
| // sign update | ||
| if err = updateRequest.Sign(signer); err != nil { | ||
| utils.Fatalf("Error signing resource update: %s", err.Error()) | ||
| } | ||
|
|
||
| // post update | ||
| err = client.UpdateResource(updateRequest) | ||
| if err != nil { | ||
| utils.Fatalf("Error updating resource: %s", err.Error()) | ||
| return | ||
|
|
@@ -112,7 +156,7 @@ func resourceInfo(ctx *cli.Context) { | |
| utils.Fatalf("Error retrieving resource metadata: %s", err.Error()) | ||
| return | ||
| } | ||
| encodedMetadata, err := mru.EncodeUpdateRequest(metadata) | ||
| encodedMetadata, err := metadata.MarshalJSON() | ||
| if err != nil { | ||
| utils.Fatalf("Error encoding metadata to JSON for display:%s", err) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we create a resource without a name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Name is optional. If you don't put the
nameflag, then the resource name is an empty string.