Skip to content

Commit

Permalink
added addressing for ecdsa and rsa types. fixed default return for pr…
Browse files Browse the repository at this point in the history
…ivate key string, and added formatting for eth and btc secret types
  • Loading branch information
bunfield committed Jun 1, 2024
1 parent b06ee5c commit d5da3f4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
32 changes: 31 additions & 1 deletion pkg/types/address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ package address

import (
"encoding/hex"
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"

"github.com/btcsuite/btcutil/base58"
"github.com/multiformats/go-multibase"
Expand Down Expand Up @@ -112,8 +116,28 @@ func (p *PrivateKey) String() string {

case protocol.SignatureTypeRCD1:
return FormatFs(p.Key[:32])

case protocol.SignatureTypeETH:
return hex.EncodeToString(p.Key)

case protocol.SignatureTypeBTCLegacy, protocol.SignatureTypeBTC:
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), p.Key)

wif, err := btcutil.NewWIF(privKey, &chaincfg.MainNetParams, true)
if err != nil {
return hex.EncodeToString(p.Key)
}
return wif.String()

case protocol.SignatureTypeEcdsaSha256:
return FormatAS2(p.Key)

case protocol.SignatureTypeRsaSha256:
return FormatAS3(p.Key)
}
return p.PublicKey.String()

//as a failsafe, just return the raw hex encoded bytes
return fmt.Sprintf("%X", p.Key)
}

type Lite struct {
Expand Down Expand Up @@ -143,6 +167,12 @@ func formatAddr(typ protocol.SignatureType, hash []byte) string {
case protocol.SignatureTypeETH:
return FormatETH(hash)

case protocol.SignatureTypeEcdsaSha256:
return FormatAC2(hash)

case protocol.SignatureTypeRsaSha256:
return FormatAC3(hash)

case protocol.SignatureTypeReceipt,
protocol.SignatureTypePartition,
protocol.SignatureTypeSet,
Expand Down
26 changes: 24 additions & 2 deletions pkg/types/address/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,38 @@ import (
)

// FormatAC1 formats the hash of an Accumulate public key as an Accumulate AC1
// address.
// address. (ed25519)
func FormatAC1(hash []byte) string {
return format2(hash, "AC1")
}

// FormatAS1 formats an Accumulate private key as an Accumulate AS1 address.
// FormatAS1 formats an Accumulate private key as an Accumulate AS1 address. (ed25519)
func FormatAS1(seed []byte) string {
return format2(seed, "AS1")
}

// FormatAC2 formats the hash of an Accumulate public key as an Accumulate AC2 (ecdsa)
// address.
func FormatAC2(hash []byte) string {
return format2(hash, "AC2")
}

// FormatAS2 formats an Accumulate private key as an Accumulate AS2 ecdsa address.
func FormatAS2(seed []byte) string {
return format2(seed, "AS2")
}

// FormatAC3 formats the hash of an Accumulate public key as an Accumulate AC3 (rsa)
// address.
func FormatAC3(hash []byte) string {
return format2(hash, "AC3")
}

// FormatAS3 formats an Accumulate private key as an Accumulate AS3 rsa address.
func FormatAS3(seed []byte) string {
return format2(seed, "AS3")
}

// FormatFA formats the hash of a Factom public key as a Factom FA address.
func FormatFA(hash []byte) string {
return format1(hash, 0x5f, 0xb1)
Expand Down

0 comments on commit d5da3f4

Please sign in to comment.