Skip to content

Commit

Permalink
Merge pull request #1 from tomholford/th/ints-and-sigs
Browse files Browse the repository at this point in the history
co: helpers + generic interface
  • Loading branch information
deelawn authored Jul 31, 2024
2 parents 2eac192 + 1ab41b0 commit 7df2ce4
Show file tree
Hide file tree
Showing 5 changed files with 705 additions and 48 deletions.
65 changes: 56 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@ package main
import (
"flag"
"fmt"
"math/big"
"os"
"strconv"

"github.com/deelawn/urbit-gob/co"
)

const (
// Commands
cmdPatp string = "patp"
cmdPatp2Dec string = "patp2dec"
cmdPatp2Hex string = "patp2hex"
cmdPatp string = "patp"
cmdPatp2Dec string = "patp2dec"
cmdPatp2Hex string = "patp2hex"
cmdPatp2Point string = "patp2point"

cmdPatq string = "patq"
cmdPatq2Dec string = "patq2dec"
cmdPatq2Hex string = "patq2hex"
cmdPatq string = "patq"
cmdPatq2Dec string = "patq2dec"
cmdPatq2Hex string = "patq2hex"
cmdPatq2Point string = "patq2point"

cmdPoint2Patp string = "point2patp"
cmdPoint2Patq string = "point2patq"

cmdHex2Patp string = "hex2patp"
cmdHex2Patq string = "hex2patq"

cmdClan string = "clan"
cmdSein string = "sein"
cmdEqPatq string = "eqpatq"
cmdClan string = "clan"
cmdClanPoint string = "clanpoint"
cmdSein string = "sein"
cmdSeinPoint string = "seinpoint"
cmdEqPatq string = "eqpatq"

cmdIsValidPat string = "isvalidpat"
cmdIsValidPatp string = "isvalidpatp"
Expand All @@ -49,13 +58,19 @@ func main() {
fmt.Printf(usageCmdFmtStr, cmdPatp, "converts a number to a @p-encoded string\n")
fmt.Printf(usageCmdFmtStr, cmdPatp2Dec, "converts a @p-encoded string to a decimal-encoded string\n")
fmt.Printf(usageCmdFmtStr, cmdPatp2Hex, "converts a @p-encoded string to a hex-encoded string\n")
fmt.Printf(usageCmdFmtStr, cmdPatp2Point, "converts a @p-encoded string to a big.Int\n")
fmt.Printf(usageCmdFmtStr, cmdPatq, "converts a number to a @q-encoded string\n")
fmt.Printf(usageCmdFmtStr, cmdPatq2Dec, "converts a @q-encoded string to a decimal-encoded string\n")
fmt.Printf(usageCmdFmtStr, cmdPatq2Hex, "converts a @q-encoded string to a hex-encoded string\n")
fmt.Printf(usageCmdFmtStr, cmdPatq2Point, "converts a @q-encoded string to a big.Int\n")
fmt.Printf(usageCmdFmtStr, cmdPoint2Patp, "converts a big.Int to a @p-encoded string\n")
fmt.Printf(usageCmdFmtStr, cmdPoint2Patq, "converts a big.Int to a @q-encoded string\n")
fmt.Printf(usageCmdFmtStr, cmdHex2Patp, "converts a hex-encoded string to a @p-encoded string\n")
fmt.Printf(usageCmdFmtStr, cmdHex2Patq, "converts a hex-encoded string to a @q-encoded string\n")
fmt.Printf(usageCmdFmtStr, cmdClan, "determines the ship class of a @p value\n")
fmt.Printf(usageCmdFmtStr, cmdClanPoint, "determines the ship class of a big.Int\n")
fmt.Printf(usageCmdFmtStr, cmdSein, "determines the parent of a @p value\n")
fmt.Printf(usageCmdFmtStr, cmdSeinPoint, "determines the parent of a big.Int\n")
fmt.Printf(usageCmdFmtStr, cmdEqPatq, "performs an equality comparison on @q values\n")
fmt.Printf(usageCmdFmtStr, cmdIsValidPat, "weakly checks if a string is a valid @p or @q value\n")
fmt.Printf(usageCmdFmtStr, cmdIsValidPatp, "validates a @p string\n")
Expand Down Expand Up @@ -83,20 +98,52 @@ func main() {
result, err = co.Patp2Dec(args[1])
case cmdPatp2Hex:
result, err = co.Patp2Hex(args[1])
case cmdPatp2Point:
result, err = co.Patp2Point(args[1])
case cmdPatq:
result, err = co.Patq(args[1])
case cmdPatq2Dec:
result, err = co.Patq2Dec(args[1])
case cmdPatq2Hex:
result, err = co.Patq2Hex(args[1])
case cmdPatq2Point:
result, err = co.Patq2Point(args[1])
case cmdPoint2Patp:
i, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
fmt.Println(err)
os.Exit(codeErrorReturned)
}
result, err = co.Point2Patp(big.NewInt(i))
case cmdPoint2Patq:
i, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
fmt.Println(err)
os.Exit(codeErrorReturned)
}
result, err = co.Point2Patq(big.NewInt(i))
case cmdHex2Patp:
result, err = co.Hex2Patp(args[1])
case cmdHex2Patq:
result, err = co.Hex2Patq(args[1])
case cmdClan:
result, err = co.Clan(args[1])
case cmdClanPoint:
i, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
fmt.Println(err)
os.Exit(codeErrorReturned)
}
result, err = co.ClanPoint(big.NewInt(i))
case cmdSein:
result, err = co.Sein(args[1])
case cmdSeinPoint:
i, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
fmt.Println(err)
os.Exit(codeErrorReturned)
}
result, err = co.SeinPoint(big.NewInt(i))
case cmdIsValidPat:
result = co.IsValidPat(args[1])
case cmdIsValidPatp:
Expand Down
86 changes: 80 additions & 6 deletions co/co.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,33 @@ func patp2bn(name string) (*big.Int, error) {
return hex, nil
}

// Patp2Point converts a @p-encoded string to a big.Int pointer.
func Patp2Point(name string) (*big.Int, error) {
point, err := patp2bn(name)
if err != nil {
return nil, err
}

return point, nil
}

// Point2Patp converts a big.Int pointer to a @p-encoded string.
func Point2Patp(point *big.Int) (string, error) {
return Patp(point.String())
}

// Patp2Dec converts a @p-encoded string to a decimal-encoded string.
func Patp2Dec(name string) (string, error) {

dec, err := patp2bn(name)
dec, err := Patp2Point(name)
if err != nil {
return "", err
}

return dec.String(), nil
}

// Patq converts a number to a @q-encoded string.
func Patq(arg string) (string, error) {
func patq(arg string) (string, error) {

v, ok := big.NewInt(0).SetString(arg, 10)
if !ok {
Expand All @@ -226,6 +240,33 @@ func Patq(arg string) (string, error) {
return buf2patq(buf), nil
}

// Patq converts a string-encoded int or *big.Int to a @q-encoded string.
func Patq(arg interface{}) (string, error) {
switch v := arg.(type) {
case string:
return patq(v)
case *big.Int:
return patq(v.String())
default:
return "", fmt.Errorf(ugi.ErrInvalidQ, v)
}
}

// Patq2Point converts a @q-encoded string to a big.Int pointer.
func Patq2Point(name string) (*big.Int, error) {
point, err := patq2bn(name)
if err != nil {
return nil, err
}

return point, nil
}

// Point2Patq converts a big.Int pointer to a @q-encoded string.
func Point2Patq(point *big.Int) (string, error) {
return Patq(point.String())
}

func buf2patq(buf []byte) string {

var chunked [][]byte
Expand Down Expand Up @@ -409,6 +450,15 @@ func Clan(who string) (string, error) {
return ShipClassComet, nil
}

// ClanPoint determines the ship class of a big.Int-encoded @p value.
func ClanPoint(arg *big.Int) (string, error) {
patp, err := Patp(arg)
if err != nil {
return "", err
}
return Clan(patp)
}

// Sein determines the parent of a @p value.
func Sein(name string) (string, error) {

Expand Down Expand Up @@ -439,6 +489,20 @@ func Sein(name string) (string, error) {
return Patp(res.String())
}

// SeinPoint determines the parent of a big.Int-encoded @p value.
func SeinPoint(arg *big.Int) (*big.Int, error) {
patp, err := Point2Patp(arg)
if err != nil {
return nil, err
}
sein, err := Sein(patp)
if err != nil {
return nil, err
}

return Patp2Point(sein)
}

/*
IsValidPat weakly checks if a string is a valid @p or @q value.
Expand Down Expand Up @@ -539,9 +603,7 @@ func EqPatq(p, q string) (bool, error) {
return eqModLeadingZeros(phex, qhex), nil
}

// Patp converts a number to a @p-encoded string.
func Patp(arg string) (string, error) {

func patp(arg string) (string, error) {
v, ok := big.NewInt(0).SetString(arg, 10)
if !ok {
return "", fmt.Errorf(ugi.ErrInvalidInt, arg)
Expand All @@ -566,6 +628,18 @@ func Patp(arg string) (string, error) {
return p, nil
}

// Patp converts a either a string-encoded int or *big.Int to a @p-encoded string.
func Patp(arg interface{}) (string, error) {
switch v := arg.(type) {
case string:
return patp(v)
case *big.Int:
return patp(v.String())
default:
return "", fmt.Errorf(ugi.ErrInvalidP, arg)
}
}

func patpLoop(dyy, tsxz, timp *big.Int, trep string) string {

log := end(four, one, tsxz)
Expand Down
Loading

0 comments on commit 7df2ce4

Please sign in to comment.