feat: signing pkg for programmatic access#36
Conversation
|
@brunocalza i'm not sure if this is something we want to merge, but it was a request from IoTeX to make this available via a pkg instead of cmd. At the very least, they can access this branch. If it's worthwhile pursuing, i can fix the linting issues. |
In some sense, the But it's a good idea to have its own package. But it would be good to refactor the CLI logic to use that package so we don't have 2 signers Update: |
|
@brunocalza Okay gotcha, and I totally missed that impl in Since this is sort of a one-off, partially for fun, I can take a stab at it. So, would it make sense for me to take your existing impl in Then, do 2 things:
|
That approach makes sense to me. Let me know if you need help along the way. |
| } | ||
| } | ||
|
|
||
| func newSignCommand() *cli.Command { |
There was a problem hiding this comment.
new command to sign data—i.e., vaults sign --private-key abcd /path/to/file. it just print the raw signature string
cmd/vaults/commands.go
Outdated
| Description: "The result of the `vaults account create` command will write a private key to a file, \n" + | ||
| "and this lets you retrieve the public key value for use in other commands.\n\n" + | ||
| "EXAMPLE:\n\nvaults account address /path/to/file", | ||
| UsageText: "vaults account address <file_path|hex_string>", |
There was a problem hiding this comment.
this is a backlog issue from the UX improvements project: ENG-787
i.e., you can do vaults account address /path/to/pk-file, or you can do vaults account address abcd1234. i thought about adding a boolean flag like --file false, or --type file or --type string in case that's a better UX, but it's a small feature
There was a problem hiding this comment.
yeah, i also prefer being explicit by using flags, but looks good
brunocalza
left a comment
There was a problem hiding this comment.
looks pretty good 💯 left some comments but no big changes really required
cmd/vaults/commands.go
Outdated
| Description: "The result of the `vaults account create` command will write a private key to a file, \n" + | ||
| "and this lets you retrieve the public key value for use in other commands.\n\n" + | ||
| "EXAMPLE:\n\nvaults account address /path/to/file", | ||
| UsageText: "vaults account address <file_path|hex_string>", |
There was a problem hiding this comment.
yeah, i also prefer being explicit by using flags, but looks good
internal/app/uploader.go
Outdated
| signature, err := signer.SignFile(filepath) | ||
| signer := signing.NewSigner(bu.privateKey) | ||
| signatureBytes, err := signer.SignFile(filepath) | ||
| signature := signing.SignatureBytesToHex(signatureBytes) |
There was a problem hiding this comment.
the err check should be above this line
|
@brunocalza i agree with all of your points! i've done the following:
|
Removed the `SignatureBytesToHex` since its just a wrapper on std hex lib method and refactored where needed. Removed failure tests for `HexToECDSA` and `FileToECDSA` since these are just wrappers for eth crypto. And made a small fix on order of err check in `uploader.go`.
a92c033 to
70c8c5b
Compare
Summary
Adds a pkg for signing requests and a
signcommand, modeled off of the signer CLI and uploader.go. Closes ENG-787.Details
It offers a few methods:
HexToECDSA: Loads a private key from the given string and creates an ECDSA private key.FileToECDSA: Loads a private key from a file and creates an ECDSA private key.NewSigner: Creates a new signer with the given private key, provided byLoadPrivateKey.SignFile: Signs the given file with the signer and returns the signature as bytes.SignBytes: Signs the given bytes with the signer and returns the signature as bytes.SignatureBytesToHex: Converts bytes so they can be used in the URL POST request to write data to a vault.How it was tested
You can find the tests to ensure correct usage. Namely, I tested the CLI
signerssignature matched with the example I set up in the tests. I also created a working demo example here: https://github.com/dtbuchholz/demo-textile-http-api