Skip to content

Instantly share code, notes, and snippets.

@kmcintyre
Created April 22, 2022 02:34
Show Gist options
  • Save kmcintyre/59e2aa9f698ecc5b1ce9d93d1eb756d4 to your computer and use it in GitHub Desktop.
Save kmcintyre/59e2aa9f698ecc5b1ce9d93d1eb756d4 to your computer and use it in GitHub Desktop.
bash to zip file on AWS lambda with onflow fcl-js - (failed layer)
#!/bin/bash
mkdir -p nodejs
cd nodejs
cat <<EOF >> package.json
{
"name": "attempted_layer",
"type": "module",
"description": "",
"version": "0.1.0",
"dependencies": {},
"devDependencies": {}
}
EOF
npm install --save @onflow/fcl @onflow/types elliptic
cat <<EOF >> index.js
//import * as t from "@onflow/types"; // no need
import fcl from '@onflow/fcl';
const {config, tx, mutate} = fcl;
import { SHA3 } from "sha3";
import elliptic from 'elliptic';
const {ec: EC} = elliptic;
const ec = new EC("p256");
config()
.put("env", "testnet")
.put("accessNode.api", "https://access-testnet.onflow.org")
const approve = `import NonFungibleToken from 0x631e88ae7f1d7c20
import OLD_NFT from 0x
transaction {
prepare(signer: AuthAccount) {
let collection <- OLD_NFT.createEmptyCollection()
signer.save<@NonFungibleToken.Collection>(<-collection, to: OLD_NFT.CollectionStoragePath)
signer.link<&{NonFungibleToken.CollectionPublic}>(OLD_NFT.CollectionPublicPath, target: OLD_NFT.CollectionStoragePath)
}
}`;
//
const ADMIN_ADDRESS = '';
const ADMIN_PRIVATE_KEY = '';
const authz = async (account) => {
return {
...account,
addr: ADMIN_ADDRESS,
keyId: 0,
signingFunction: async (signable) => ({
f_type: "CompositeSignature",
f_vsn: "1.0.0",
addr: ADMIN_ADDRESS,
keyId: 0,
signature: sign(ADMIN_PRIVATE_KEY, signable.message),
}),
};
}
// this could be replaced with KMS
const sign = (privateKey, message) => {
const key = ec.keyFromPrivate(Buffer.from(privateKey, "hex"));
const sig = key.sign(hashMsgHex(message));
const n = 32;
const r = sig.r.toArrayLike(Buffer, "be", n);
const s = sig.s.toArrayLike(Buffer, "be", n);
return Buffer.concat([r, s]).toString("hex");
}
const hashMsgHex = (message) => {
const sha = new SHA3(256);
sha.update(Buffer.from(message, "hex"));
return sha.digest();
}
export const handler = async (event) => {
// TODO implement
const transactionId = await mutate({
cadence: approve,
authz: authz,
limit: 65,
});
let result = await tx(transactionId).onceSealed();
const response = {
statusCode: 200,
body: JSON.stringify(result),
};
return response;
};
EOF
zip -r lambda.zip *
mv lambda.zip ../
cd ..
rm -rf nodejs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment