Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC calls signdata and checksignature #86

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions rpc/wallet_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,26 @@ type (
Entry Entry `json:"entry,omitempty"`
}
)

// Sign data
type (
Sign_Data struct {
Data string `json:"data"`
Plain bool `json:"plain"`
}
Sign_Data_Result struct {
SignedData string `json:"signed_data"`
}
)

// Check signature
type (
Check_Signature struct {
Data string `json:"data"`
Plain bool `json:"plain"`
}
Check_Signature_Result struct {
Signer string `json:"signer"`
Message string `json:"message"`
}
)
58 changes: 58 additions & 0 deletions walletapi/rpcserver/rpc_check_signature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2017-2021 DERO Project. All rights reserved.
// Use of this source code in any form is governed by RESEARCH license.
// license can be found in the LICENSE file.
// GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
//
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package rpcserver

import (
"context"
"encoding/hex"
"fmt"
"runtime/debug"

"github.com/deroproject/derohe/rpc"
)

func CheckSignature(ctx context.Context, p rpc.Check_Signature) (result rpc.Check_Signature_Result, err error) {
defer func() { // safety so if anything wrong happens, we return error
if r := recover(); r != nil {
err = fmt.Errorf("panic occured. stack trace %s", debug.Stack())
}
}()

if p.Data == "" {
return result, fmt.Errorf("Data field is empty")
}

decData, err := hex.DecodeString(p.Data)
if err != nil {
return result, fmt.Errorf("Error parsing data err %s", err)
}
length := len(decData)

w := fromContext(ctx)
signer, message, err := w.wallet.CheckSignature(decData[:length])
if err != nil {
return result, fmt.Errorf("Error checking signature err %s", err)
}
if p.Plain {
result.Message = string(message[:])
} else {
result.Message = hex.EncodeToString(message[:])
}
result.Signer = signer.String()

return result, nil
}
57 changes: 57 additions & 0 deletions walletapi/rpcserver/rpc_sign_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2017-2021 DERO Project. All rights reserved.
// Use of this source code in any form is governed by RESEARCH license.
// license can be found in the LICENSE file.
// GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
//
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package rpcserver

import (
"context"
"encoding/hex"
"fmt"
"runtime/debug"

"github.com/deroproject/derohe/rpc"
)

func SignData(ctx context.Context, p rpc.Sign_Data) (result rpc.Sign_Data_Result, err error) {
defer func() { // safety so if anything wrong happens, we return error
if r := recover(); r != nil {
err = fmt.Errorf("panic occured. stack trace %s", debug.Stack())
}
}()

if p.Data == "" {
return result, fmt.Errorf("Data field is empty")
}

var decData []byte

if p.Plain {
decData = []byte(p.Data)
} else {
decData, err = hex.DecodeString(p.Data)
}

if err != nil {
return result, fmt.Errorf("Error parsing data err %s", err)
}
length := len(decData)

w := fromContext(ctx)
output := w.wallet.SignData(decData[:length])
result.SignedData = hex.EncodeToString(output[:])

return result, nil
}
4 changes: 4 additions & 0 deletions walletapi/rpcserver/rpc_websocket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ var wallet_handler = handler.Map{
"Transfer": handler.New(Transfer),
"transfer_split": handler.New(Transfer),
"scinvoke": handler.New(ScInvoke),
"signdata": handler.New(SignData),
"SignData": handler.New(SignData),
"checksignature": handler.New(CheckSignature),
"CheckSignature": handler.New(CheckSignature),
}

var servicemux = handler.ServiceMap{
Expand Down