sdk

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

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

Go to latest
Published: Apr 6, 2021 License: MIT Imports: 21 Imported by: 0

README

Activeledger

Activeledger - Golang SDK

The Activeledger Golang SDK has been built to provide an easy way to connect your Go application to an Activeledger Network

Activeledger

Visit Activeledger.io

Read Activeledgers documentation

Installation

go get github.com/activeledger/SDK-Golang

Usage

The SDK currently supports the following functionality

  • Connection handling
  • Key generation
  • Key onboarding
  • Transaction building
Import
import (
  sdk "github.com/activeledger/SDK-Golang"
)
Connection

When sending a transaction, you must pass a connection that provides the information needed to establish a link to the network and specified node.

To do this a connection object must be created. This object must be passed the protocol, address, and port.

connection := sdk.Connection {
  Scheme:"protocol",
  Url:"url",
  Port:"port"
}

sdk.SetUrl(connection)
Example - Connecting to the Activeledger public testnet
connection := sdk.Connection {
  Scheme:"http",
  Url:"testnet-uk.activeledger.io",
  Port:"5260"
}

sdk.SetUrl(connection)

Key

There are two key types that can be generated currently, more are planned and will be implemented into Activeledger first. These types are RSA and Elliptic Curve.

Generating a key
Example
// RSA
// Generate the private key
privatekey := sdk.RsaKeyGen()
// Get the public key from the private key
publicKey := privatekey.PublicKey

// ECDSA
privateKey, _ := sdk.EcdsaKeyGen()

// See key exporting to get ECDSA Public key
Exporting Key
Example
// RSA Public key string PEM
 publicKeyString := sdk.RsaToPem(publicKey)

// ECDSA private and public key PEMs
 privatekeyStr, publicKeyString := sdk.EcdsaToPem(privateKey)
Onboarding a key and creating a transaction

Once you have a key generated, to use it to sign transactions it must be onboarded to the ledger network

Example
 txObject := sdk.TxObject {
   Namespace: "default",
   Contract: "onboard",
   Input: input,
   Output: output,
   ReadOnly: readOnly,
  }

  tx, _ := json.Marshal(txObject)

  // RSA
  signedMessage,_ := sdk.RsaSign(*privatekey, []byte(tx))

  // ECDSA (Elliptic curve)
  pemPrivate := sdk.EcdsaFromPem(privatekeyStr)
  signedMessage := sdk.EcdsaSign(pemPrivate,string(tx))

  signature["identity"] = signedMessage
  selfsign := true
  transaction := sdk.Transaction {
    TxObject: txObject,
    SelfSign: selfsign,
    Signature:signature,
  }

  sdk.SetUrl(sdk.Connection {
    Scheme:"protocol",
    Url:"url",
    Port:"port"
  })

  // Response contains Code (int) and Desc (string)
  response := sdk.SendTransaction(transaction, sdk.GetUrl())

Signing & sending a transaction

When signing a transaction you must send the finished version of it. No changes can be made after signing as this will cause the ledger to reject it.

The key must be one that has been successfully onboarded to the ledger which the transaction is being sent to.

Events Subscription

SDK contains different helper functions for the purpose of subscribing to different events.

  • Subscribe(host) // host=protocol://ip:port
  • SubscribeStream(host,stream)
  • EventSubscribeContract(host,contract,event)
  • EventSubscribe(host,contract)
  • AllEventSubscribe(host)

They all return events which can then be used by developers.

ActivityStreams

SDK also contains helper functions to get and search streams from Activeledger.

  • GetActivityStreams(host, ids) // host=protocol://ip:port
  • GetActivityStream(host, id)
  • GetActivityStreamVolatile(host, id)
  • SetActivityStreamVolatile(host, id, bdy) // Anything in the bdy will be written to that location for that stream id.
  • GetActivityStreamChanges(host)
  • SearchActivityStreamPost(host, query) //post request
  • SearchActivityStreamGet(host, query) //get Request
  • FindTransaction(host, umid )

They all return map[string]interface{}.

License


This project is licensed under the MIT License

Documentation

Overview

* MIT License (MIT) * Copyright (c) 2018 * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE.

* MIT License (MIT) * Copyright (c) 2018 * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE.

* MIT License (MIT) * Copyright (c) 2018 * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE.

* MIT License (MIT) * Copyright (c) 2018 * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE.

* MIT License (MIT) * Copyright (c) 2018 * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE.

* MIT License (MIT) * Copyright (c) 2018 * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE.

* MIT License (MIT) * Copyright (c) 2018 * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE.

Index

Constants

View Source
const (
	RSA = iota
	EC
)

Types of Encryption

Variables

View Source
var (
	RSAKey  *rsa.PrivateKey
	ECKey   *bitecdsa.PrivateKey
	KeyName string
	Stream  string
	KeyType string
)
View Source
var Encrptype = [...]string{
	"rsa",
	"secp256k1",
}

Encrptype stores types of Encryption available

Functions

func AllEventSubscribe

func AllEventSubscribe(host string) (chan sseclient.Event, error)

func EcdsaFromPem

func EcdsaFromPem(pemEncoded string) *bitecdsa.PrivateKey

func EcdsaKeyGen

func EcdsaKeyGen() (priv *bitecdsa.PrivateKey, err error)

Generate a pair of private and puiblic key using ecdsa. output: private key public can be extracted from private key using EcdsaToPem function provided below.

func EcdsaSign

func EcdsaSign(prv *bitecdsa.PrivateKey, data string) string

Sign Wrapper exports signature as comptible activeledger string input: Private key, Transaction output: signature

func EcdsaToPem

func EcdsaToPem(prv *bitecdsa.PrivateKey) (string, string)

Convert Private key object into PCKS1 PEM Private & Public input: Private key output: Pem formatted Public and private key

func EventSubscribe

func EventSubscribe(host string, contract string) (chan sseclient.Event, error)

func EventSubscribeContract

func EventSubscribeContract(host string, contract string, event string) (chan sseclient.Event, error)

func FindTransaction

func FindTransaction(host string, umid string) map[string]interface{}
FindTransaction finds the transaction using the umid in request

host:http://ip:port

func GetActivityStream

func GetActivityStream(host string, id string) map[string]interface{}

GetActivityStream returns a single Activity stream passed in request. host:http://ip:port

func GetActivityStreamChanges

func GetActivityStreamChanges(host string) map[string]interface{}

GetActivityStreamChanges returns All Activity streams changes. host:http://ip:port

func GetActivityStreamVolatile

func GetActivityStreamVolatile(host string, id string) map[string]interface{}

GetActivityStreamVolatile returns the passed activity stream volatile . host:http://ip:port

func GetActivityStreams

func GetActivityStreams(host string, ids []string) map[string]interface{}

GetActivityStreams returns All Activity streams passed in request. host:http://ip:port

func GetNodeReferences

func GetNodeReferences(url string) []string

Returns references of all the nodes. Used for Territoriality.

func GetUrl

func GetUrl() string

func RsaKeyGen

func RsaKeyGen() *rsa.PrivateKey

Generate a pair of RSA private and public key. Output: Private key object. Public key can be extracted using publicKey:=key.PublicKey

func RsaPrivToPem

func RsaPrivToPem(prvkey rsa.PrivateKey) string

func RsaSign

func RsaSign(r rsa.PrivateKey, data []byte) ([]byte, error)

Sign a transaction using your private key. Data is hashed using SHA256 before signing. Input: Private Key,Transaction byte array Output: Signature byte Array

func RsaToPem

func RsaToPem(pubkey rsa.PublicKey) string

Converting your public key into pem format. This is necessary when sending public key in a transaction. Input: Public Key Output: Pem formated public key

func SearchActivityStreamGet

func SearchActivityStreamGet(host string, query string) map[string]interface{}

SearchActivityStreamGet searches the past query in Activeledger and returns the response host:http://ip:port

func SearchActivityStreamPost

func SearchActivityStreamPost(host string, query map[string]interface{}) map[string]interface{}

SearchActivityStreamPost runs the passed query on Activeledger and returns the resposne. host:http://ip:port

func SetActivityStreamVolatile

func SetActivityStreamVolatile(host string, id string, bdy interface{}) map[string]interface{}

SetActivityStreamVolatile sets the passed activity stream id volatiles. host:http://ip:port

func SetUrl

func SetUrl(connection Connection)

Setter and getter for the URL connection string.

func Subscribe

func Subscribe(host string) (chan sseclient.Event, error)

func SubscribeStream

func SubscribeStream(host string, stream string) (chan sseclient.Event, error)

Types

type AlgorithmIdentifier

type AlgorithmIdentifier struct {
	Algorithm  asn1.ObjectIdentifier
	Parameters asn1.RawValue `asn1:"optional"`
}

Algorithm Struct

type Connection

type Connection struct {
	Scheme string
	Url    string
	Port   string
}

Connection struct for creating connection opbject. Shceme: Protocol used for the network eg http or https Url: IP address of the network Port: port of the network

type Encryption

type Encryption int

Encryption enum

func (Encryption) String

func (encrp Encryption) String() string

type PrivateKey

type PrivateKey struct {
	PublicKey
	D *big.Int
}

Private Key points struct

type PublicKey

type PublicKey struct {
	X, Y *big.Int
}

Public Key points struct

type Response

type Response struct {
	UMID           string        `json:"$umid"`
	Summary        Summary       `json:"$summary"`
	Response       []interface{} `json:"$responses"`
	Territoriality string        `json:"$territoriality"`
	Streams        Streams       `json:"$streams"`
}

Response Object to store activeledger response

func CreateAndSendTransaction

func CreateAndSendTransaction(txReq TransactionReq) (Response, error)

CreateAndSendTransaction function creates and sends the transaction to acitveledger. Send the Response object back to user

func SendTransaction

func SendTransaction(transaction Transaction, url string) (Response, error)

SendTransaction function sends complete transaction the activeledger network. input: transaction,url

type StreamData

type StreamData struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

type Streams

type Streams struct {
	New     []StreamData `json:"new"`
	Updated []StreamData `json:"updated"`
}

type Summary

type Summary struct {
	Total  int      `json:"total"`
	Vote   int      `json:"vote"`
	Commit int      `json:"commit"`
	Errors []string `json:"errors"`
}

type Transaction

type Transaction struct {
	Territoriality string                 `json:"$territoriality,omitempty"`
	TxObject       TxObject               `json:"$tx"`
	SelfSign       bool                   `json:"$selfsign"`
	Signature      map[string]interface{} `json:"$sigs"`
}

Transaction elements

func CreateTransaction

func CreateTransaction(txReq TransactionReq) *Transaction

CreateTransaction function create a transaction object and returns it to User. This function is for when user need to add multiple signature to the sigs object.

type TransactionReq

type TransactionReq struct {
	TxObject       TxObject
	Territoriality string
	SelfSign       bool
	StreamID       string
	KeyName        string
	RsaKey         *rsa.PrivateKey
	EcKey          *bitecdsa.PrivateKey
	KeyType        string
}

Response Object to store activeledger response

type TxObject

type TxObject struct {
	Namespace string                 `json:"$namespace"`
	Contract  string                 `json:"$contract"`
	Entry     string                 `json:"$entry,omitempty"`
	Input     map[string]interface{} `json:"$i"`
	Output    map[string]interface{} `json:"$o,omitempty"`
	ReadOnly  map[string]interface{} `json:"$r,omitempty"`
}

TxObject within a transaction

Jump to

Keyboard shortcuts

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