41 lines
791 B
Go
41 lines
791 B
Go
package cmapi
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"git.ct129.com/cmesh/cmapi/keys"
|
|
"git.ct129.com/cmesh/cmapi/message"
|
|
)
|
|
|
|
func SignRequestV1(reqType string, value []byte, hostID string, counter uint, privkey keys.PrivateKey) ([]byte, error) {
|
|
encMsg, err := json.Marshal(message.RequestWrapper{
|
|
Type: reqType,
|
|
Value: value,
|
|
Timestamp: time.Now(),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
signedMsg := base64.StdEncoding.EncodeToString(encMsg)
|
|
sig, err := privkey.Sign([]byte(signedMsg))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
wrapper := message.RequestV1{
|
|
Version: 1,
|
|
HostID: hostID,
|
|
Counter: counter,
|
|
Message: signedMsg,
|
|
Signature: sig,
|
|
}
|
|
b, err := json.Marshal(wrapper)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return b, nil
|
|
}
|