2023-10-30 16:58:57 +00:00
|
|
|
package walletconnect
|
|
|
|
|
|
|
|
import (
|
2023-11-06 19:04:42 +00:00
|
|
|
"encoding/json"
|
2023-10-30 16:58:57 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-11-24 15:27:05 +00:00
|
|
|
"regexp"
|
2023-10-30 16:58:57 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// func sendResponseEvent(eventFeed *event.Feed, eventType walletevent.EventType, payloadObj interface{}, resErr error) {
|
|
|
|
// payload, err := json.Marshal(payloadObj)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Error("Error marshaling WC response: %v; result error: %w", err, resErr)
|
|
|
|
// } else {
|
|
|
|
// err = resErr
|
|
|
|
// }
|
|
|
|
|
|
|
|
// event := walletevent.Event{
|
|
|
|
// Type: eventType,
|
|
|
|
// Message: string(payload),
|
|
|
|
// }
|
|
|
|
|
2023-11-19 17:29:17 +00:00
|
|
|
// sentCount := eventFeed.Send(event)
|
|
|
|
|
|
|
|
// log.Debug("wallet.api.wc RESPONSE", "eventType", eventType, "error", err, "payload.len", len(payload), "sentCount", sentCount)
|
2023-10-30 16:58:57 +00:00
|
|
|
// }
|
|
|
|
|
2023-11-24 15:27:05 +00:00
|
|
|
// Returns namspace name, chainID and error
|
|
|
|
func parseCaip2ChainID(str string) (string, uint64, error) {
|
2023-10-30 16:58:57 +00:00
|
|
|
caip2 := strings.Split(str, ":")
|
|
|
|
if len(caip2) != 2 {
|
2023-11-24 15:27:05 +00:00
|
|
|
return "", 0, errors.New("CAIP-2 string is not valid")
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chainIDStr := caip2[1]
|
|
|
|
chainID, err := strconv.ParseUint(chainIDStr, 10, 64)
|
|
|
|
if err != nil {
|
2023-11-24 15:27:05 +00:00
|
|
|
return "", 0, fmt.Errorf("CAIP-2 second value not valid Chain ID: %w", err)
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|
2023-11-24 15:27:05 +00:00
|
|
|
return caip2[0], chainID, nil
|
2023-10-30 16:58:57 +00:00
|
|
|
}
|
2023-11-06 19:04:42 +00:00
|
|
|
|
|
|
|
// JSONProxyType provides a generic way of changing the JSON value before unmarshalling it into the target.
|
|
|
|
// transform function is called before unmarshalling.
|
|
|
|
type JSONProxyType struct {
|
|
|
|
target interface{}
|
|
|
|
transform func([]byte) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *JSONProxyType) UnmarshalJSON(input []byte) error {
|
|
|
|
if b.transform == nil {
|
|
|
|
return errors.New("transform function is not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := b.transform(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Unmarshal(output, b.target)
|
|
|
|
}
|
2023-11-24 15:27:05 +00:00
|
|
|
|
|
|
|
func isValidNamespaceName(namespaceName string) bool {
|
|
|
|
pattern := "^[a-z0-9-]{3,8}$"
|
|
|
|
|
|
|
|
regex := regexp.MustCompile(pattern)
|
|
|
|
|
|
|
|
return regex.MatchString(namespaceName)
|
|
|
|
}
|