assetexchange

package module
v2.0.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2023 License: Apache-2.0 Imports: 13 Imported by: 2

README

Asset Exchange Library

The asset exchange library within the Weaver chaincode package offers two sets of APIs to manage asset locks and claims for a cross-ledger asset exchange. In one set, the transactions take a unique contract ID referring to an asset exchange instance as parameter. This contract ID is generated by the Weaver chaincode when the first asset is locked (as per the protocol steps), and returned to the calling application client. Though you must use contract ID-based API functions for fungible assets, you may also use them for non-fungible assets. For non-fungible assets, Weaver also provides a parallel set of API functions that take asset type and ID as parameters rather than a temporarily generated contract ID.

To use this library, in you smart contract go.mod, add following in require (update the version according to the latest module version):

require(
    ...
    github.com/hyperledger/cacti/weaver/common/protos-go/v2 v1.5.3
    github.com/hyperledger/cacti/weaver/core/network/fabric-interop-cc/libs/assetexchange/v2 v1.5.3
    ...
)

With ContractId

Atleast following 5 functions needs to be added in chaincode (Note: the function signature, i.e. the name, arguments and return values needs to be exactly same):

  1. LockAsset
import (
    ...
    "github.com/hyperledger/cacti/weaver/core/network/fabric-interop-cc/libs/assetexchange/v2"
)
func (s *SmartContract) LockAsset(ctx contractapi.TransactionContextInterface, assetExchangeAgreementSerializedProto64 string, lockInfoSerializedProto64 string) (string, error) {
    // Add some safety checks before calling LockAsset from library
    // Caller of this chaincode is supposed to be the Locker and the owner of the asset being locked.
    contractId, err := assetexchange.LockAsset(ctx, "", assetExchangeAgreementSerializedProto64, lockInfoSerializedProto64)
    if err != nil {
        return "", logThenErrorf(err.Error())
    }
    // Post proccessing of asset after LockAsset called like change status of the asset so that it can't be spent.
    
    return contractId, nil
}

Here assetExchangeAgreementSerializedProto64 is serialized protobuf in base64 encoded string of AssetExchangeAgreement protobuf structure, and can be used to extract details like asset id, type of asset and recipient. Check the structure definition here. Similarly lockInfoSerializedProto64 is serialized protobuf in base64 encoded string of AssetLock protobuf structure. Check the structure definition here.

  1. LockFungibleAsset
func (s *SmartContract) LockFungibleAsset(ctx contractapi.TransactionContextInterface, fungibleAssetExchangeAgreementSerializedProto64 string, lockInfoSerializedProto64 string) (string, error) {
    // Add some safety checks before calling LockFungibleAsset from library
    // Caller of this chaincode is supposed to be the Locker and the owner of the asset being locked.
    contractId, err := assetexchange.LockFungibleAsset(ctx, "", fungibleAssetExchangeAgreementSerializedProto64, lockInfoSerializedProto64)
    if err != nil {
        return "", logThenErrorf(err.Error())
    }
    // Post proccessing of asset after LockFungibleAsset called like reduce the amount of tokens owned by the locker, or mark it locked so that it can't be spent.
    
    return contractId, nil
}

Here fungibleAssetExchangeAgreementSerializedProto64 is serialized protobuf in base64 encoded string of FungibleAssetExchangeAgreement protobuf structure, and can be used to extract details like asset quantity, type of asset and recipient. Check the structure definition here.

  1. IsAssetLockedQueryUsingContractId
func (s *SmartContract) IsAssetLockedQueryUsingContractId(ctx contractapi.TransactionContextInterface, contractId string) (bool, error) {
    return assetexchange.IsAssetLockedQueryUsingContractId(ctx, contractId)
}
  1. ClaimAssetUsingContractId
func (s *SmartContract) ClaimAssetUsingContractId(ctx contractapi.TransactionContextInterface, contractId, claimInfoSerializedProto64 string) (bool, error) {
    // Note recipient will be the caller for this function
    claimed := false
    err := assetexchange.ClaimAssetUsingContractId(ctx, contractId, claimInfoSerializedProto64)
    if err != nil {
        return false, logThenErrorf(err.Error())
    }
    claimed = true
    // After the above function call, update the owner of the asset with recipeint/caller
    
    return claimed, nil
}
  1. UnlockAssetUsingContractId
func (s *SmartContract) UnlockAssetUsingContractId(ctx contractapi.TransactionContextInterface, contractId string) (bool, error) {
    unlocked := false
    err := assetexchange.UnlockAssetUsingContractId(ctx, contractId)
    if err != nil {
        return false, logThenErrorf(err.Error())
    }
    unlocked = true
    ...
    ...
    return true, nil
}
  1. Add following extra utility functions as well:
func (s *SmartContract) GetHTLCHashByContractId(ctx contractapi.TransactionContextInterface, contractId string) (string, error) {
    return assetexchange.GetHTLCHashByContractId(ctx, contractId)
}
func (s *SmartContract) GetHTLCHashPreImageByContractId(ctx contractapi.TransactionContextInterface, contractId string) (string, error) {
    return assetexchange.GetHTLCHashPreImageByContractId(ctx, contractId)
}

Without ContractId:

  1. LockAsset: This will be same as in "With ContractId" section.

  2. LockFungibleAsset: This will also be same as in "With ContractId" section.

  3. IsAssetLocked

func (s *SmartContract) IsAssetLocked(ctx contractapi.TransactionContextInterface, assetAgreementSerializedProto64 string) (bool, error) {
    return assetexchange.IsAssetLocked(ctx, "", assetAgreementSerializedProto64)
}
  1. IsFungibleAssetLocked
func (s *SmartContract) IsFungibleAssetLocked(ctx contractapi.TransactionContextInterface, contractId string) (bool, error) {
    return assetexchange.IsFungibleAssetLocked(ctx, contractId)
}
  1. ClaimAsset
func (s *SmartContract) ClaimAsset(ctx contractapi.TransactionContextInterface, assetAgreementSerializedProto64 string, claimInfoSerializedProto64 string) (bool, error) {
    // Note recipient will be the caller for this function
    claimed := false
    claimed, err = assetexchange.ClaimAsset(ctx, "", assetAgreementSerializedProto64, claimInfoSerializedProto64)
    if err != nil {
        return false, logThenErrorf(err.Error())
    }
    // After the above function call, update the owner of the asset with recipeint/caller
    
    return claimed, nil
}
  1. ClaimFungibleAsset
func (s *SmartContract) ClaimFungibleAsset(ctx contractapi.TransactionContextInterface, contractId, claimInfoSerializedProto64 string) (bool, error) {
    // Note recipient will be the caller for this function
    claimed := false
    err := assetexchange.ClaimAssetUsingContractId(ctx, contractId, claimInfoSerializedProto64)
    if err != nil {
        return false, logThenErrorf(err.Error())
    }
    claimed = true
    // After the above function call, update the owner of the asset with recipeint/caller
    
    return claimed, nil
}
  1. UnlockAsset
func (s *SmartContract) UnlockAsset(ctx contractapi.TransactionContextInterface, assetAgreementSerializedProto64 string) (bool, error) {
    unlocked := false
    unlocked, err = assetexchange.UnlockAsset(ctx, "", assetAgreementSerializedProto64)
    if err != nil {
        return false, logThenErrorf(err.Error())
    }
    ...
    ...
    return true, nil
}
  1. UnlockFungibleAsset
func (s *SmartContract) UnlockFungibleAsset(ctx contractapi.TransactionContextInterface, contractId string) (bool, error) {
    unlocked := false
    err := assetexchange.UnlockFungibleAsset(ctx, contractId)
    if err != nil {
        return false, logThenErrorf(err.Error())
    }
    unlocked = true
    ...
    ...
    return true, nil
}
  1. Add following extra utility functions as well:
func (s *SmartContract) GetHTLCHash(ctx contractapi.TransactionContextInterface, callerChaincodeID, assetAgreementBytesBase64 string) (string, error) {
    return assetexchange.GetHTLCHash(ctx, callerChaincodeID, assetAgreementBytesBase64)
}
func (s *SmartContract) GetHTLCHashPreImage(ctx contractapi.TransactionContextInterface, callerChaincodeID, assetAgreementBytesBase64 string) (string, error) {
    return assetexchange.GetHTLCHashPreImage(ctx, callerChaincodeID, assetAgreementBytesBase64)
}

Documentation

Overview

manage_assets is a chaincode that contains all the code related to asset management operations (e.g., Lock, Unlock, Claim) and any related utility functions

manage_assets is a chaincode that contains all the code related to asset management operations (e.g., Lock, Unlock, Claim) and any related utility functions

manage_assets is a chaincode that contains all the code related to asset management operations (e.g., Lock, Unlock, Claim) and any related utility functions

manage_assets is a chaincode that contains all the code related to asset management operations (e.g., Lock, Unlock, Claim) and any related utility functions

manage_assets is a chaincode that contains all the code related to asset management operations (e.g., Lock, Unlock, Claim) and any related utility functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClaimAsset

func ClaimAsset(ctx contractapi.TransactionContextInterface, callerChaincodeID, assetAgreementBytesBase64, claimInfoBytesBase64 string) (string, error)

Claim Functions ClaimAsset cc is used to record claim of an asset on the ledger

func ClaimAssetUsingContractId

func ClaimAssetUsingContractId(ctx contractapi.TransactionContextInterface, contractId, claimInfoBytesBase64 string) error

ClaimAsset cc is used to record claim of an asset on the ledger (this uses the contractId)

func ClaimFungibleAsset

func ClaimFungibleAsset(ctx contractapi.TransactionContextInterface, contractId, claimInfoBytesBase64 string) error

ClaimFungibleAsset cc is used to record claim of a fungible asset on the ledger

func GenerateAssetLockKeyAndContractId

func GenerateAssetLockKeyAndContractId(ctx contractapi.TransactionContextInterface, chaincodeId string, assetAgreement *common.AssetExchangeAgreement) (string, string, error)

* Function to generate asset-lock key (which is combination of asset-type and asset-id) * and contract-id (which is a hash on asset-lock key) for the non-fungible asset locking on the ledger

func GenerateClaimAssetLockKey

func GenerateClaimAssetLockKey(ctx contractapi.TransactionContextInterface, chaincodeId string, assetAgreement *common.AssetExchangeAgreement) (string, error)

* Function to generate asset-lock key (which is combination of asset-type and asset-id) * and contract-id (which is a hash on asset-lock key) for the non-fungible asset locking on the ledger

func GenerateFungibleAssetLockContractId

func GenerateFungibleAssetLockContractId(ctx contractapi.TransactionContextInterface, chaincodeId string, assetAgreement *common.FungibleAssetExchangeAgreement) string

* Function to generate contract-id for fungible asset-locking on the ledger (which is * a hash on the attributes of the fungible asset exchange agreement)

func GenerateSHA256HashInBase64Form

func GenerateSHA256HashInBase64Form(preimage string) string

function to generate a "SHA256" hash in base64 format for a given preimage

func GenerateSHA512HashInBase64Form

func GenerateSHA512HashInBase64Form(preimage string) string

function to generate a "SHA512" hash in base64 format for a given preimage

func GetHTLCHash

func GetHTLCHash(ctx contractapi.TransactionContextInterface, callerChaincodeID, assetAgreementBytesBase64 string) (string, error)

func GetHTLCHashByContractId

func GetHTLCHashByContractId(ctx contractapi.TransactionContextInterface, contractId string) (string, error)

func GetHTLCHashPreImage

func GetHTLCHashPreImage(ctx contractapi.TransactionContextInterface, callerChaincodeID, assetAgreementBytesBase64 string) (string, error)

func GetHTLCHashPreImageByContractId

func GetHTLCHashPreImageByContractId(ctx contractapi.TransactionContextInterface, contractId string) (string, error)

func IsAssetLocked

func IsAssetLocked(ctx contractapi.TransactionContextInterface, callerChaincodeID, assetAgreementBytesBase64 string) (bool, error)

IsLocked Query Functions IsAssetLocked cc is used to query the ledger and findout if an asset is locked or not

func IsAssetLockedQueryUsingContractId

func IsAssetLockedQueryUsingContractId(ctx contractapi.TransactionContextInterface, contractId string) (bool, error)

IsAssetLocked cc is used to query the ledger and find out if an asset is locked or not (this uses the contractId)

func IsFungibleAssetLocked

func IsFungibleAssetLocked(ctx contractapi.TransactionContextInterface, contractId string) (bool, error)

IsFungibleAssetLocked cc is used to query the ledger and find out if a fungible asset is locked or not

func LockAsset

func LockAsset(ctx contractapi.TransactionContextInterface, callerChaincodeID, assetAgreementBytesBase64, lockInfoBytesBase64 string) (string, error)

Lock Functions LockAsset cc is used to record locking of an asset on the ledger

func LockFungibleAsset

func LockFungibleAsset(ctx contractapi.TransactionContextInterface, callerChaincodeID, fungibleAssetAgreementBytesBase64, lockInfoBytesBase64 string) (string, error)

LockFungibleAsset cc is used to record locking of a group of fungible assets of an asset-type on the ledger

func UnlockAsset

func UnlockAsset(ctx contractapi.TransactionContextInterface, callerChaincodeID, assetAgreementBytesBase64 string) (string, error)

Unlock Functions UnlockAsset cc is used to record unlocking of an asset on the ledger

func UnlockAssetUsingContractId

func UnlockAssetUsingContractId(ctx contractapi.TransactionContextInterface, contractId string) error

UnlockAssetUsingContractId cc is used to record unlocking of an asset on the ledger (this uses the contractId)

func UnlockFungibleAsset

func UnlockFungibleAsset(ctx contractapi.TransactionContextInterface, contractId string) error

UnlockFungibleAsset cc is used to record unlocking of a fungible asset on the ledger

Types

type AssetLockInterface

type AssetLockInterface interface {
	GetLocker() string
	GetRecipient() string
	GetLockInfo() interface{}
	GetExpiryTimeSecs() uint64
}

type AssetLockValue

type AssetLockValue struct {
	ContractId     string      `json:"contractId"`
	Locker         string      `json:"locker"`
	Recipient      string      `json:"recipient"`
	LockInfo       interface{} `json:"lockInfo"`
	ExpiryTimeSecs uint64      `json:"expiryTimeSecs"`
}

Object used in the map, <asset-type, asset-id> --> <contractId, locker, recipient, ...> (for non-fungible assets)

func (AssetLockValue) GetExpiryTimeSecs

func (a AssetLockValue) GetExpiryTimeSecs() uint64

func (AssetLockValue) GetLockInfo

func (a AssetLockValue) GetLockInfo() interface{}

func (AssetLockValue) GetLocker

func (a AssetLockValue) GetLocker() string

func (AssetLockValue) GetRecipient

func (a AssetLockValue) GetRecipient() string

type FungibleAssetLockValue

type FungibleAssetLockValue struct {
	Type           string      `json:"type"`
	NumUnits       uint64      `json:"numUnits"`
	Locker         string      `json:"locker"`
	Recipient      string      `json:"recipient"`
	LockInfo       interface{} `json:"lockInfo"`
	ExpiryTimeSecs uint64      `json:"expiryTimeSecs"`
}

Object used in the map, contractId --> <asset-type, num-units, locker, ...> (for fungible assets)

func (FungibleAssetLockValue) GetExpiryTimeSecs

func (a FungibleAssetLockValue) GetExpiryTimeSecs() uint64

func (FungibleAssetLockValue) GetLockInfo

func (a FungibleAssetLockValue) GetLockInfo() interface{}

func (FungibleAssetLockValue) GetLocker

func (a FungibleAssetLockValue) GetLocker() string

func (FungibleAssetLockValue) GetRecipient

func (a FungibleAssetLockValue) GetRecipient() string

type HashLock

type HashLock struct {
	HashMechanism common.HashMechanism `json:"hashMechanism"`
	HashBase64    string               `json:"hashBase64"`
}

Object used to capture the HashLock details used in Asset Locking

Jump to

Keyboard shortcuts

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