Expose vanilla ECDSA signing (#1444)
This commit is contained in:
parent
442a12e996
commit
056bf367a7
|
@ -758,3 +758,24 @@ func (b *StatusBackend) UpdateMailservers(enodes []string) error {
|
|||
}
|
||||
return st.UpdateMailservers(nodes)
|
||||
}
|
||||
|
||||
// SignHash exposes vanilla ECDSA signing for signing a message for Swarm
|
||||
func (b *StatusBackend) SignHash(hexEncodedHash string) (string, error) {
|
||||
hash, err := hexutil.Decode(hexEncodedHash)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("SignHash: could not unmarshal the input: %v", err)
|
||||
}
|
||||
|
||||
chatAccount, err := b.AccountManager().SelectedChatAccount()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("SignHash: could not select account: %v", err.Error())
|
||||
}
|
||||
|
||||
signature, err := ethcrypto.Sign(hash, chatAccount.AccountKey.PrivateKey)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("SignHash: could not sign the hash: %v", err)
|
||||
}
|
||||
|
||||
hexEncodedSignature := hexutil.Encode(signature)
|
||||
return hexEncodedSignature, nil
|
||||
}
|
||||
|
|
|
@ -386,6 +386,43 @@ func TestStartStopMultipleTimes(t *testing.T) {
|
|||
require.NoError(t, backend.StopNode())
|
||||
}
|
||||
|
||||
func TestSignHash(t *testing.T) {
|
||||
backend := NewStatusBackend()
|
||||
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, backend.StartNode(config))
|
||||
defer func() {
|
||||
require.NoError(t, backend.StopNode())
|
||||
}()
|
||||
|
||||
var testCases = []struct {
|
||||
name string
|
||||
chatPrivKeyHex string
|
||||
hashHex string
|
||||
expectedSignatureHex string
|
||||
}{
|
||||
{
|
||||
name: "tc1",
|
||||
chatPrivKeyHex: "facadefacadefacadefacadefacadefacadefacadefacadefacadefacadefaca",
|
||||
hashHex: "0xa4735de5193362fe856416000105cdfa6ce56265607311cebae93b26e5adf438",
|
||||
expectedSignatureHex: "0x176c971bae188c663614fc535ac9dbf62871dfeaadb38645809a510d28b3c4b0245415d5547c1b27f7cfea3341564f9c6981421144d3606b455346be69bd078c01",
|
||||
},
|
||||
}
|
||||
|
||||
const dummyEncKey = "facadefacadefacadefacadefacadefacadefacadefacadefacadefacadefaca"
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
require.NoError(t, backend.InjectChatAccount(tc.chatPrivKeyHex, dummyEncKey))
|
||||
|
||||
signature, err := backend.SignHash(tc.hashHex)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, signature, tc.expectedSignatureHex)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashTypedData(t *testing.T) {
|
||||
backend := NewStatusBackend()
|
||||
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
|
||||
|
|
|
@ -674,3 +674,17 @@ func GetNodesFromContract(rpcEndpoint *C.char, contractAddress *C.char) *C.char
|
|||
|
||||
return C.CString(string(data))
|
||||
}
|
||||
|
||||
// SignHash exposes vanilla ECDSA signing required for Swarm messages
|
||||
//export SignHash
|
||||
func SignHash(hexEncodedHash *C.char) *C.char {
|
||||
hexEncodedSignature, err := statusBackend.SignHash(
|
||||
C.GoString(hexEncodedHash),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return makeJSONResponse(err)
|
||||
}
|
||||
|
||||
return C.CString(hexEncodedSignature)
|
||||
}
|
||||
|
|
|
@ -664,3 +664,13 @@ func ChaosModeUpdate(on bool) string {
|
|||
err := node.ChaosModeCheckRPCClientsUpstreamURL(on)
|
||||
return makeJSONResponse(err)
|
||||
}
|
||||
|
||||
// SignHash exposes vanilla ECDSA signing required for Swarm messages
|
||||
func SignHash(hexEncodedHash string) string {
|
||||
hexEncodedSignature, err := statusBackend.SignHash(hexEncodedHash)
|
||||
if err != nil {
|
||||
return makeJSONResponse(err)
|
||||
}
|
||||
|
||||
return hexEncodedSignature
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue