DEV Community

BC
BC

Posted on

Golang: convert algorand address to public key

Install Algorand SDK:

$ go get -u github.com/algorand/go-algorand-sdk/...
Enter fullscreen mode Exit fullscreen mode

import (
    "crypto/ed25519"
    "crypto/sha512"
    "encoding/base64"

    algocrypto "github.com/algorand/go-algorand-sdk/crypto"
    "github.com/algorand/go-algorand-sdk/types"
)


func algoAddrToPublicKey(address string) (ed25519.PublicKey, error) {
    addr, err := types.DecodeAddress(address)
    if err != nil {
        return nil, err
    }
    bytes := [sha512.Size256]byte(addr)
    return ed25519.PublicKey(bytes[:]), nil
}

func main() {
    addr := "<wallet address, 58 characters>"
    pubKey, _ := algoAddrToPublicKey(addr)
    msg := "message to sign"
    // if raw sigature is base64 encoded, we need to decode it
    sig, _ := base64.StdEncoding.DecodeString(rawSig)
    valid := algocrypto.VerifyBytes(pubKey, []byte(msg), sig)
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)