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

Fixing case where signingJobs is empty for non-DERIVE_KEY_AND_SIGN requests. #139

Merged
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
48 changes: 24 additions & 24 deletions remotesigning/remote_signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log"

"github.com/lightsparkdev/go-sdk/crypto"
Expand Down Expand Up @@ -58,31 +57,33 @@ func GraphQLResponseForRemoteSigningWebhook(
webhook webhooks.WebhookEvent,
seedBytes []byte,
) (SigningResponse, error) {
// Calculate the xpub for each L1 signing job
signingJobs, hasSigningJobs := (*webhook.Data)["signing_jobs"].([]interface{})
if !hasSigningJobs {
return nil, fmt.Errorf("signing_jobs not found or invalid type")
// If derive key and sign, calculate the xpub for each L1 signing job
subEventTypeStr, isValidSubEventType := (*webhook.Data)["sub_event_type"].(string)
if !isValidSubEventType {
return nil, errors.New("sub_event_type not found or invalid type")
}

signingJobs, hasSigningJobs := (*webhook.Data)["signing_jobs"].([]interface{})
var xpubs []string
for _, job := range signingJobs {
jobMap, isValidJobMap := job.(map[string]interface{})
if !isValidJobMap {
return nil, fmt.Errorf("invalid signing job format")
}
derivationPath := jobMap["derivation_path"].(string)

hardenedPath, _, err := SplitDerivationPath(derivationPath)
if err != nil {
return nil, err
}

masterSeedHex := hex.EncodeToString(seedBytes)
xpub, err := utils.GenHardenedXPub(masterSeedHex, hardenedPath, "mainnet")
if err != nil {
return nil, err
if subEventTypeStr == "DERIVE_KEY_AND_SIGN" && hasSigningJobs {
for _, job := range signingJobs {
jobMap, isValidJobMap := job.(map[string]interface{})
if !isValidJobMap {
return nil, errors.New("invalid signing job format")
}
derivationPath := jobMap["derivation_path"].(string)

hardenedPath, _, err := SplitDerivationPath(derivationPath)
if err != nil {
return nil, err
}

masterSeedHex := hex.EncodeToString(seedBytes)
xpub, err := utils.GenHardenedXPub(masterSeedHex, hardenedPath, "mainnet")
if err != nil {
return nil, err
}
xpubs = append(xpubs, xpub)
}
xpubs = append(xpubs, xpub)
}
if !validator.ShouldSign(webhook, xpubs) {
return nil, errors.New("declined to sign messages")
Expand All @@ -94,7 +95,6 @@ func GraphQLResponseForRemoteSigningWebhook(
return nil, errors.New("webhook data is missing")
}
var subtype objects.RemoteSigningSubEventType
subEventTypeStr := (*webhook.Data)["sub_event_type"].(string)
log.Printf("Received remote signing webhook with sub_event_type %s", subEventTypeStr)
err := subtype.UnmarshalJSON([]byte(`"` + subEventTypeStr + `"`))
if err != nil {
Expand Down