Get client identity fabric-contract-api-go

1.1k Views Asked by At

There's a method in fabric-contract-api-go for getting transaction initiator's Identity

func (ctx *TransactionContext) GetClientIdentity() cid.ClientIdentity

How'd we use it to return client ID when, e.g., create is invoked in this contract https://github.com/hyperledger/fabric-contract-api-go/blob/master/tutorials/getting-started.md

// ...
// ...

// Create adds a new key with value to the world state
func (sc *SimpleContract) Create(ctx contractapi.TransactionContextInterface, key string, value string) error {
    existing, err := ctx.GetStub().GetState(key)

    if err != nil {
        return errors.New("Unable to interact with world state")
    }

    if existing != nil {
        return fmt.Errorf("Cannot create world state pair with key %s. Already exists", key)
    }

    err = ctx.GetStub().PutState(key, []byte(value))

    if err != nil {
        return errors.New("Unable to interact with world state")
    }

    return nil
}

// ...
// ...
1

There are 1 best solutions below

0
On

GetClientIdentity returns you an interface of type ClientIdentity defined here https://github.com/hyperledger/fabric-chaincode-go/blob/master/pkg/cid/interfaces.go This shows you the functions you can then invoke to retrieve information about the transaction submitter (ie the client identity)