Skip to content

Instantly share code, notes, and snippets.

View thecipherBlock's full-sized avatar

SAI PRASANTH VUPPALA thecipherBlock

  • U+0000
View GitHub Profile
@thecipherBlock
thecipherBlock / convertSecp256k1PubKeyToCompressed.ts
Last active November 18, 2023 11:34
Convert 32 bytes secp256k1 publicKey into compressed public key
function determineSignByte(publicKey: string): string {
if(publicKey.length !== 64) {
throw new Error("required `public key` of length 32");
}
const xCoordinate = BigInt("0x" + publicKey);
return xCoordinate % BigInt(2) === BigInt(0) ? "02" : "03";
}
@thecipherBlock
thecipherBlock / privKeyToAddr.dart
Created May 12, 2022 11:30
Convert secp256k1 privateKey to ethereum address in dart
import 'dart:typed_data';
import 'package:pointycastle/digests/keccak.dart';
import 'package:pointycastle/ecc/api.dart';
import 'package:pointycastle/ecc/curves/secp256k1.dart';
BigInt decodeToBigInt(List<int> magnitude) {
BigInt result;
if (magnitude.length == 1) {
result = BigInt.from(magnitude[0]);
@thecipherBlock
thecipherBlock / checkSum_of_ethAddress.dart
Created April 23, 2022 13:51
Convert or verify the ethereum address into checksum address in dart/flutter
import 'dart:typed_data';
import 'package:convert/convert.dart';
import 'package:pointycastle/digests/keccak.dart';
/// This gist/function helps in convertion or verification of the ethereum address
/// Why checksum is explained here: https://coincodex.com/article/2078/ethereum-address-checksum-explained/
String toChecksum(String ethAddress) {
// Checking for 0x prefix
String addrWithoutChecksum = ethAddress.substring(0, 2) == '0x'
? ethAddress.substring(2, ethAddress.length)
@thecipherBlock
thecipherBlock / asymmEncNdDec.js
Last active November 18, 2023 11:35
Asymmetric encryption and decryption of JSON data in js
var crypto = require("crypto");
var path = require("path");
var fs = require("fs");
var encryptStringWithRsaPublicKey = function(toEncrypt, publicKeyInPemPath) {
var absolutePath = path.resolve(publicKeyInPemPath);
var publicKey = fs.readFileSync(absolutePath, "utf8");
var buffer = Buffer.from(JSON.stringify(toEncrypt));
var encrypted = crypto.publicEncrypt(publicKey, buffer);
return encrypted.toString("base64");