# syntax=docker.io/docker/dockerfile:1.3@sha256:42399d4635eddd7a9b8a24be879d2f9a930d0ed040a61324cfdf59ef1357b3b2
FROM node:16-alpine@sha256:004dbac84fed48e20f9888a23e32fa7cf83c2995e174a78d41d9a9dd1e051a20 as const-build

# fetch scripts/guardian-set-init.sh deps
RUN apk update && apk add bash g++ make python3 curl jq findutils

# Support additional root CAs
COPY README.md cert.pem* /certs/
# Alpine
RUN if [ -e /certs/cert.pem ]; then cp /certs/cert.pem /etc/ssl/cert.pem; fi

# install CLI deps & build
WORKDIR /clients/js

# copy package.json & package-lock.json by themselves to create a cache layer
COPY clients/js/package.json clients/js/package-lock.json ./
# mount the buildkit cache on npm's cache dir, install dependencies

# Commenting out npm cache due to heisenbug with github build machines
# RUN --mount=type=cache,uid=1000,gid=1000,target=/home/node/.npm

RUN npm ci

# copy the rest of the source files, as a layer on top of the deps
COPY clients/js ./
# build CLI
RUN make build

WORKDIR /

COPY scripts ./scripts
COPY ethereum/.env.test ./ethereum/.env.test

ARG num_guardians
ENV NUM_GUARDIANS=$num_guardians

# run guardian-set-init.sh to create env files with the init state for NUM_GUARDIANS
RUN ./scripts/guardian-set-init.sh $NUM_GUARDIANS

FROM scratch AS const-export
COPY --from=const-build /scripts/.env.0x ethereum/.env
COPY --from=const-build /scripts/.env.hex solana/.env
COPY --from=const-build /scripts/.env.hex terra/tools/.env
COPY --from=const-build /scripts/.env.hex cosmwasm/deployment/terra2/tools/.env
COPY --from=const-build /scripts/.env.hex algorand/.env
COPY --from=const-build /scripts/.env.hex near/.env
COPY --from=const-build /scripts/.env.hex aptos/.env
