Skip to content

Instantly share code, notes, and snippets.

Avatar
🌴
Miami

Lucas H. lucashenning

🌴
Miami
View GitHub Profile
@lucashenning
lucashenning / 01_install_libs.sh
Created September 6, 2022 23:06
Postdeploy platform hook for puppeteer on Amazon Linux 2 (Elastic Beanstalk)
View 01_install_libs.sh
#!/bin/bash
# Put this in your Amazon Elastic Beanstalk repo
# Tested on Amazon Linux 2, Node.JS v16, puppeteer v15.3.0
# File path: .platform/hooks/postdeploy/01_install_libs.sh
sudo amazon-linux-extras install epel -y
cd node_modules/puppeteer/
cd .local-chromium/linux-*/chrome-linux
@lucashenning
lucashenning / ecdsa-sig-parse.ts
Created November 3, 2020 12:27
Parse a ASN1 DER encoded Sig with ASN1.js
View ecdsa-sig-parse.ts
const EcdsaSigAsnParse = asn1.define('EcdsaSig', function(this: any) {
this.seq().obj(
this.key('r').int(),
this.key('s').int(),
);
});
@lucashenning
lucashenning / kms-sign.ts
Created November 3, 2020 12:27
AWS KMS Sign DIGEST
View kms-sign.ts
async function sign(msgHash, keyId) {
const params : KMS.SignRequest = {
KeyId: keyId,
Message: msgHash,
SigningAlgorithm: 'ECDSA_SHA_256',
MessageType: 'DIGEST'
};
return kms.sign(params).promise();
}
@lucashenning
lucashenning / asn1-pub-key-parse.ts
Created November 3, 2020 12:26
Parse a DER encoded public key with ASN1.js
View asn1-pub-key-parse.ts
const EcdsaPubKey = asn1.define('EcdsaPubKey', function(this: any) {
// https://tools.ietf.org/html/rfc5480#section-2
this.seq().obj(
this.key('algo').seq().obj(
this.key('algorithm').objid(),
this.key('parameters').objid(),
),
this.key('pubKey').bitstr() // <-- this is what we want
);
});
@lucashenning
lucashenning / kms-get-public-key.ts
Created November 3, 2020 12:25
AWS KMS get public key
View kms-get-public-key.ts
import { KMS } from 'aws-sdk';
const kms = new KMS({
accessKeyId: '<access_key_id>',
secretAccessKey: '<access_secret>',
region: 'us-east-1',
apiVersion: '2014-11-01',
});
kms.getPublicKey({
KeyId: '<KMS key id>'
});
@lucashenning
lucashenning / signSendTx.ts
Created November 3, 2020 11:38
Signing and sending a Tx using AWS KMS
View signSendTx.ts
const web3 = new Web3(new Web3.providers.HttpProvider("https://kovan.infura.io/v3/<your key>"));
let pubKey = await getPublicKey(keyId);
let ethAddr = getEthereumAddress((pubKey.PublicKey as Buffer));
let ethAddrHash = ethutil.keccak(Buffer.from(ethAddr));
// signing the 1st time
// we're signing the hash of our ethereum address
let sig = await findEthereumSig(ethAddrHash);
let recoveredPubAddr = findRightKey(ethAddrHash, sig.r, sig.s, ethAddr);
@lucashenning
lucashenning / recoverPubKeyFromSig.ts
Created November 3, 2020 10:48
Uses Ethereum's ecrecover to recover the Ethereum address from a signature.
View recoverPubKeyFromSig.ts
function recoverPubKeyFromSig(sig: Buffer, r : BN, s : BN, v: number) {
console.log("Recovering public key with sig " + sig.toString('hex') + " r: " + r.toString(16) + " s: " + s.toString(16));
let rBuffer = r.toBuffer();
let sBuffer = s.toBuffer();
let pubKey = ethutil.ecrecover(sig, v, rBuffer, sBuffer);
let addrBuf = ethutil.pubToAddress(pubKey);
var RecoveredEthAddr = ethutil.bufferToHex(addrBuf);
console.log( "Recovered ethereum address: " + RecoveredEthAddr);
return RecoveredEthAddr;
}
@lucashenning
lucashenning / calc-eth-sig.ts
Created November 3, 2020 09:30
Typescript function to calculate a valid ethereum signature based on an ASN1 DER encoded signature
View calc-eth-sig.ts
async function calcEthSig(plaintext) {
let signature = await sign(plaintext, keyId);
if (signature.Signature == undefined) {
throw new Error('Signature is undefined.');
}
console.log("encoded sig: " + signature.Signature.toString('hex'));
let decoded = EcdsaSigAsnParse.decode(signature.Signature, 'der');
let r : BN = decoded.r;
let s : BN = decoded.s;
@lucashenning
lucashenning / getEthereumAddress.ts
Last active November 2, 2020 16:48
Get Ethereum Address from ASN1 DER encoded public key
View getEthereumAddress.ts
function getEthereumAddress(publicKey: Buffer): string {
console.log("Encoded Pub Key: " + publicKey.toString('hex'));
// The public key is ASN1 encoded in a format according to
// https://tools.ietf.org/html/rfc5480#section-2
// I used https://lapo.it/asn1js to figure out how to parse this
// and defined the schema in the EcdsaPubKey object
let res = EcdsaPubKey.decode(publicKey, 'der');
let pubKeyBuffer : Buffer = res.pubKey.data;
console.log("Pub Key Buffer: " + pubKeyBuffer.toString('hex'));
@lucashenning
lucashenning / aws-kms-eth-sign.ts
Created November 2, 2020 13:51
AWS KMS based Ethereum transaction signing
View aws-kms-eth-sign.ts
import { KMS } from 'aws-sdk';
import { keccak256 } from 'js-sha3';
import * as ethutil from 'ethereumjs-util';
import Web3 from 'web3';
import * as asn1 from 'asn1.js';
import BN from 'bn.js';
import { Transaction, TxData } from 'ethereumjs-tx';
import { TransactionReceipt } from 'web3-core/types';
const kms = new KMS({