bitjws

package module
v0.0.0-...-a671ffc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2016 License: MIT Imports: 13 Imported by: 0

README

bitjws-go

Go library for bitjws authentication, signing and verification.

This is a placehodler for a work in progress. For working examples, see:

Python bitjws Javascript bitjws

How To Use

import bitjws "github.com/GitGuild/bitjws-go"

// Generate a secret key (libsecp256k1)
seckey, err := bitjws.GenerateKey()
if err != nil {
        return nil, err
}
    
// Create a jws Header which contains your header
header := &bitjws.Header{
        Algorithm: "CUSTOM-BITCOIN-SIGN",
        Typ: "JWS",
        Kid: bitjws.ToAddress(&seckey.PublicKey),
}

// Create a set of claims
claims := &bitjws.ClaimSet{
        Iss: "http://www.example.com",
        ...
}

// Sign the header / claimset
signedMessage, err := bitjws.SimpleSign(seckey, header, claims)
if err != nil {
        return nil, err
}

// Encode the signed message into compact JWS format
compact, err := signedMessage.EncodeCompactJWS()
if err != nil {
        return nil, err
}

// Verify a signed message
checkSig, err := signedMessage.Verify(&seckey.PublicKey)
if err != nil {
        return nil, err
}

Documentation

Index

Constants

View Source
const (
	NETWORK_NAME = "bitcoin"
)

Variables

View Source
var (
	ErrRecovery   = errors.New("Supplied public key is not equal to the key recovered from the signature.")
	ErrKidAbsent  = errors.New("No Kid was supplied in the header.")
	ErrKidNoMatch = errors.New("Kid does not match recovered address.")

	ErrMultiHeadersLength = errors.New("The amount of keys is not equal to the amount of JWS headers.")
)

Functions

func Base64Decode

func Base64Decode(s string) ([]byte, error)

func Base64Encode

func Base64Encode(b []byte) string

func Base64URLDecode

func Base64URLDecode(s string) ([]byte, error)

func Base64URLEncode

func Base64URLEncode(b []byte) string

func BitcoinSign

func BitcoinSign(msg []byte, key *ecdsa.PrivateKey) (string, error)

Sign libsecp256k1

func BitcoinVerify

func BitcoinVerify(msg []byte, sig string, key *ecdsa.PublicKey) (bool, error)

Verify libsecp256k1

func CheckWIF

func CheckWIF(wif string) (valid bool, err error)

CheckWIF checks that string wif is a valid Wallet Import Format or Wallet Import Format Compressed string. If it is not, err is populated with the reason.

func DecodeSignature

func DecodeSignature(encodedSig string) ([]byte, error)

Decodes a base64 encoded Bitcoin signature as bytes

func EncodeMessage

func EncodeMessage(payload []byte) []byte

Encodes a message according to the Bitcoin protocol

func EncodeMessageHash

func EncodeMessageHash(payload []byte) []byte

Encodes the message and returns the message digest

func EncodeSignature

func EncodeSignature(sig []byte, compressed bool) string

Encodes a series of bytes representing as a Bitcoin compliant signature

func ExtractUint16

func ExtractUint16(size uint16) []byte

Extract the first two bytes from a uint as a bytestring

func ExtractUint32

func ExtractUint32(size uint32) []byte

Extract the first four bytes from a uint as a bytetring

func ExtractUint64

func ExtractUint64(size uint64) []byte

Extract the first eight bytes from a uint

func ExtractUint8

func ExtractUint8(size uint) []byte

Extract the first byte from a uint as a bytestring

func FromECDSA

func FromECDSA(seckey *ecdsa.PrivateKey) []byte

Convert an ecdsa.PrivateKey to a bytestring

func FromECDSAPub

func FromECDSAPub(pubkey *ecdsa.PublicKey) []byte

Convert an ecdsa.PublicKey to a bytestring

func GenerateKey

func GenerateKey() (*ecdsa.PrivateKey, error)

Generate a keypair and encode as base64

func HexToECDSA

func HexToECDSA(hexstring string) (*ecdsa.PrivateKey, error)

Convert hex to an ecdsa.PrivateKey

func ParseMessage

func ParseMessage(s string) interface{}

Try to parse a message, signed or multi or return nil

func Serialize

func Serialize(header *Header, claims *ClaimSet, message string) ([]byte, error)

Serialises a message

func Sha256RipeMD160

func Sha256RipeMD160(b []byte) []byte

Ripemd + SHA256 a series of bytes

func ShaSha256

func ShaSha256(b []byte) []byte

Double SHA256 a series of bytes

func ToAddress

func ToAddress(pub *ecdsa.PublicKey) (address string)

ToAddress converts a Bitcoin public key to a compressed Bitcoin address string.

func ToAddressUncompressed

func ToAddressUncompressed(pub *ecdsa.PublicKey) (address string)

ToAddressUncompressed converts a Bitcoin public key to an uncompressed Bitcoin address string.

func ToBytes

func ToBytes(pub *ecdsa.PublicKey) (b []byte)

ToBytes converts a Bitcoin public key to a 33-byte byte slice with point compression.

func ToBytesUncompressed

func ToBytesUncompressed(pub *ecdsa.PublicKey) (b []byte)

ToBytesUncompressed converts a Bitcoin public key to a 65-byte byte slice without point compression.

func ToECDSA

func ToECDSA(b []byte) *ecdsa.PrivateKey

Convert a set of bytes to an ecdsa.PrivateKey

func ToECDSAPub

func ToECDSAPub(b []byte) *ecdsa.PublicKey

Convert a set of bytes to an ecdsa.PublicKey

func VarInt

func VarInt(size uint) []byte

Performs variable length integer encoding (see BIP)

Types

type ClaimSet

type ClaimSet struct {
	Iss   string `json:"iss"`             // email address of the client_id of the application making the access token request
	Scope string `json:"scope,omitempty"` // space-delimited list of the permissions the application requests
	Aud   string `json:"aud,omitempty"`   // descriptor of the intended target of the assertion (Optional).
	Exp   int64  `json:"exp"`             // the expiration time of the assertion (seconds since Unix epoch)
	Iat   int64  `json:"iat"`             // the time the assertion was issued (seconds since Unix epoch)
	Typ   string `json:"typ,omitempty"`   // token type (Optional).

	// Email for which the application is requesting delegated access (Optional).
	Sub string `json:"sub,omitempty"`

	// The old name of Sub. Client keeps setting Prn to be
	// complaint with legacy OAuth 2.0 providers. (Optional)
	Prn string `json:"prn,omitempty"`

	// The public key corresponding to the private key used in signing
	PubKey string `json:"pubkey,omitempty"`

	// The public keys corresponding to the private keys used to sign
	PubKeys []string `json:"pubkeys,omitempty"`

	// A generic message expected to be JSON encodable
	Msg interface{} `json:"msg,omitempty"`
}

func CreateDefaultClaims

func CreateDefaultClaims(pubkey *ecdsa.PublicKey) *ClaimSet

func CreateDefaultClaimsMulti

func CreateDefaultClaimsMulti(pubkeys []*ecdsa.PublicKey) *ClaimSet

func DecodeClaims

func DecodeClaims(s string) (*ClaimSet, error)

Decodes a JWS ClaimSet

func (*ClaimSet) Encode

func (c *ClaimSet) Encode() (string, error)
type Header struct {
	// The algorithm used for signing the payload
	Algorithm string `json:"alg"`

	// The type of token
	Typ string `json:"typ,omitempty"`

	// The key id of protected header fields
	Kid string `json:"kid,omitempty"`
}

Header represents the header for the signed JWS payload

func CreateDefaultHeader

func CreateDefaultHeader(pubkey *ecdsa.PublicKey) *Header

func DecodeHeader

func DecodeHeader(s string) (*Header, error)

Decodes a JWS header

func (*Header) Encode

func (h *Header) Encode() (string, error)

type JWSMultiMessage

type JWSMultiMessage struct {
	Payload    string          `json:"payload"`
	Signatures []*JWSSignature `json:"signatures"`
}

Signed MultiSig JWS JSON structure

func ParseMultiSignedMessage

func ParseMultiSignedMessage(jwsString string) *JWSMultiMessage

Try to parse a JWS message or return nil

func SignMulti

func SignMulti(seckeys []*ecdsa.PrivateKey, hdrs []*Header, clm *ClaimSet, message string) (*JWSMultiMessage, error)

Sign an unsigned multi-signature request with the provided body

func SimpleSignMulti

func SimpleSignMulti(seckeys []*ecdsa.PrivateKey, hdrs []*Header, clm *ClaimSet) (*JWSMultiMessage, error)

Sign an unsigned multi-signature request without a body

func (*JWSMultiMessage) EncodeJWS

func (snm *JWSMultiMessage) EncodeJWS() (string, error)

Encode a signed multi signature message to JWS JSON format

func (*JWSMultiMessage) SimpleVerify

func (jm *JWSMultiMessage) SimpleVerify(pubkeys []*ecdsa.PublicKey) (bool, error)

Simple multi signature verification

func (*JWSMultiMessage) Verify

func (jm *JWSMultiMessage) Verify(pubkeys []*ecdsa.PublicKey, body string) (bool, error)

Combined multi signature verification

type JWSSignature

type JWSSignature struct {
	Protected string `json:"protected"`
	Signature string `json:"signature"` // Base64 URL encoded
}

A JWS JSON Signature structure

type SignedMessage

type SignedMessage struct {
	Header    *Header
	Claims    *ClaimSet
	Signature string // Base64 encoded
}

Signed JWS compact message structure

func ParseSignedMessage

func ParseSignedMessage(compact string) *SignedMessage

Try to parse a signed message or return nil

func Sign

func Sign(key *ecdsa.PrivateKey, hdr *Header, clm *ClaimSet, message string) (*SignedMessage, error)

Sign an unsigned single signature claimset with a message

func SimpleSign

func SimpleSign(key *ecdsa.PrivateKey, hdr *Header, clm *ClaimSet) (*SignedMessage, error)

Sign an unsigned single signature claimset without message

func (*SignedMessage) EncodeCompactJWS

func (sns *SignedMessage) EncodeCompactJWS() (string, error)

Encode a signed message to <header>.<payload>.<signature> compact serialization

func (*SignedMessage) SimpleVerify

func (sm *SignedMessage) SimpleVerify(pubkey *ecdsa.PublicKey) (bool, error)

Simple single signature verification

func (*SignedMessage) Verify

func (sm *SignedMessage) Verify(pubkey *ecdsa.PublicKey, body string) (bool, error)

Combined single signature verification

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL