Skip to content

Commit 992baff

Browse files
committed
create the asset-transfer in go implemetation
1 parent da2240f commit 992baff

File tree

8 files changed

+533
-0
lines changed

8 files changed

+533
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
#Stage 1 – Builder image
5+
FROM golang:1.23-alpine AS builder
6+
7+
RUN apk add --no-cache git
8+
9+
WORKDIR /build
10+
11+
12+
COPY go.mod go.sum ./
13+
RUN go mod download
14+
15+
COPY src/ ./src/
16+
RUN CGO_ENABLED=0 go build -v -o /build/chaincode ./src/...
17+
18+
19+
# Stage 2 – Chaincode-as-a-Service (CaaS) image
20+
FROM alpine:3.20 AS ccaas
21+
22+
ARG TARGETARCH
23+
ARG CC_SERVER_PORT=9999
24+
25+
# tini gives us proper PID-1 signal handling
26+
ENV TINI_VERSION=v0.19.0
27+
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-static-${TARGETARCH} /tini
28+
RUN chmod +x /tini
29+
30+
RUN addgroup -S chaincode && adduser -S chaincode -G chaincode
31+
WORKDIR /home/chaincode
32+
33+
COPY --from=builder /build/chaincode ./chaincode
34+
COPY docker/docker-entrypoint.sh ./docker-entrypoint.sh
35+
RUN chmod +x ./docker-entrypoint.sh
36+
37+
ENV PORT=${CC_SERVER_PORT}
38+
EXPOSE ${CC_SERVER_PORT}
39+
40+
USER chaincode
41+
ENTRYPOINT ["/tini", "--", "./docker-entrypoint.sh"]
42+
43+
44+
45+
# Stage 3 – k8s builder image
46+
FROM alpine:3.20 AS k8s
47+
48+
RUN addgroup -S chaincode && adduser -S chaincode -G chaincode
49+
WORKDIR /home/chaincode
50+
51+
COPY --from=builder /build/chaincode ./chaincode
52+
COPY docker/docker-entrypoint.sh ./docker-entrypoint.sh
53+
RUN chmod +x ./docker-entrypoint.sh
54+
55+
USER chaincode
56+
CMD ["./docker-entrypoint.sh"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
---
5+
smart_contract_name: "asset-transfer"
6+
smart_contract_version: "1.0.0"
7+
smart_contract_sequence: 1
8+
smart_contract_package: "asset-transfer.tgz"
9+
# smart_contract_constructor: ""
10+
smart_contract_endorsement_policy: ""
11+
smart_contract_collections_file: ""
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
: "${CORE_PEER_TLS_ENABLED:=false}"
6+
7+
if [[ ! -v CHAINCODE_SERVER_ADDRESS ]]; then
8+
# Legacy peer-managed mode: binary acts as a regular chaincode process.
9+
exec ./chaincode --peer.address "${CORE_PEER_ADDRESS}"
10+
11+
elif [[ "${CORE_PEER_TLS_ENABLED,,}" == "true" ]]; then
12+
# CaaS + TLS
13+
exec ./chaincode \
14+
--chaincode.address "${CHAINCODE_SERVER_ADDRESS}" \
15+
--chaincode.id "${CHAINCODE_ID}" \
16+
--chaincode.tls.enabled true \
17+
--chaincode.tls.key.file "${CHAINCODE_TLS_KEY:-/hyperledger/privatekey.pem}" \
18+
--chaincode.tls.cert.file "${CHAINCODE_TLS_CERT:-/hyperledger/cert.pem}" \
19+
--chaincode.tls.clientCaCert.file "${CHAINCODE_TLS_CLIENT_CACERT:-/hyperledger/rootcert.pem}"
20+
21+
else
22+
# CaaS without TLS
23+
exec ./chaincode \
24+
--chaincode.address "${CHAINCODE_SERVER_ADDRESS}" \
25+
--chaincode.id "${CHAINCODE_ID}"
26+
fi
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module asset
2+
3+
go 1.23.0
4+
5+
require (
6+
github.com/hyperledger/fabric-chaincode-go/v2 v2.0.0
7+
github.com/hyperledger/fabric-contract-api-go/v2 v2.2.0
8+
)
9+
10+
require (
11+
github.com/go-openapi/jsonpointer v0.21.0 // indirect
12+
github.com/go-openapi/jsonreference v0.21.0 // indirect
13+
github.com/go-openapi/spec v0.21.0 // indirect
14+
github.com/go-openapi/swag v0.23.0 // indirect
15+
github.com/hyperledger/fabric-protos-go-apiv2 v0.3.4 // indirect
16+
github.com/josharian/intern v1.0.0 // indirect
17+
github.com/mailru/easyjson v0.7.7 // indirect
18+
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
19+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
20+
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
21+
golang.org/x/net v0.28.0 // indirect
22+
golang.org/x/sys v0.24.0 // indirect
23+
golang.org/x/text v0.17.0 // indirect
24+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
25+
google.golang.org/grpc v1.67.0 // indirect
26+
google.golang.org/protobuf v1.36.1 // indirect
27+
gopkg.in/yaml.v3 v3.0.1 // indirect
28+
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
5+
github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY=
6+
github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF2+tg1TRrwQ=
7+
github.com/go-openapi/jsonreference v0.21.0/go.mod h1:LmZmgsrTkVg9LG4EaHeY8cBDslNPMo06cago5JNLkm4=
8+
github.com/go-openapi/spec v0.21.0 h1:LTVzPc3p/RzRnkQqLRndbAzjY0d0BCL72A6j3CdL9ZY=
9+
github.com/go-openapi/spec v0.21.0/go.mod h1:78u6VdPw81XU44qEWGhtr982gJ5BWg2c0I5XwVMotYk=
10+
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
11+
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
12+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
13+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
14+
github.com/hyperledger/fabric-chaincode-go/v2 v2.0.0 h1:IhkHfrl5X/fVnmB6pWeCYCdIJRi9bxj+WTnVN8DtW3c=
15+
github.com/hyperledger/fabric-chaincode-go/v2 v2.0.0/go.mod h1:PHHaFffjw7p7n9bmCfcm7RqDqYdivNEsJdiNIKZo5Lk=
16+
github.com/hyperledger/fabric-contract-api-go/v2 v2.2.0 h1:rmUoBmciB0GL/miqcbJmJbgp5QTWoJUrZo+CNxrNLF4=
17+
github.com/hyperledger/fabric-contract-api-go/v2 v2.2.0/go.mod h1:FeWeO/jwGjiME7ak3GufqKIcwkejtzrDG4QxbfKydWs=
18+
github.com/hyperledger/fabric-protos-go-apiv2 v0.3.4 h1:YJrd+gMaeY0/vsN0aS0QkEKTivGoUnSRIXxGJ7KI+Pc=
19+
github.com/hyperledger/fabric-protos-go-apiv2 v0.3.4/go.mod h1:bau/6AJhvEcu9GKKYHlDXAxXKzYNfhP6xu2GXuxEcFk=
20+
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
21+
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
22+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
23+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
24+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
25+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
26+
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
27+
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
28+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
29+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
30+
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
31+
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
32+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
33+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
34+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
35+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
36+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
37+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
38+
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
39+
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
40+
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
41+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
42+
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
43+
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
44+
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
45+
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
46+
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
47+
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
48+
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
49+
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
50+
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
51+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:e7S5W7MGGLaSu8j3YjdezkZ+m1/Nm0uRVRMEMGk26Xs=
52+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
53+
google.golang.org/grpc v1.67.0 h1:IdH9y6PF5MPSdAntIcpjQ+tXO41pcQsfZV2RxtQgVcw=
54+
google.golang.org/grpc v1.67.0/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
55+
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
56+
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
57+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
58+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
59+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
60+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
61+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
package main
6+
7+
// OwnerIdentifier represents the owner of an asset with their organisation MSP ID and user identifier.
8+
// Fields are lowercase to match the TypeScript serialisation format.
9+
type OwnerIdentifier struct {
10+
Org string `json:"org"`
11+
User string `json:"user"`
12+
}
13+
14+
// Asset describes the details of an asset stored in the world state.
15+
// Fields are defined in alphabetical order to produce deterministic JSON serialisation.
16+
type Asset struct {
17+
AppraisedValue int `json:"AppraisedValue"`
18+
Color string `json:"Color"`
19+
ID string `json:"ID"`
20+
Owner string `json:"Owner"` // JSON-encoded OwnerIdentifier
21+
Size int `json:"Size"`
22+
}

0 commit comments

Comments
 (0)