0.1.1 • Published 2 years ago

react-native-secp256k1-urnm v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-native-secp256k1-urnm

This module provides native bindings to bitcoin-core/secp256k1 for React Native.

Demo

Example test cases Example test cases GIF

Installation

Npm

npm install react-native-secp256k1-urnm

Yarn

yarn add react-native-secp256k1-urnm

Usage

import * as secp256k1 from 'react-native-secp256k1-urnm';
import { utils } from 'react-native-secp256k1-urnm';

async function main() {
  const privAbase64 = await secp256k1.ext.generateKey();
  const privBbase64 = await secp256k1.ext.generateKey();
  const privA = utils.decodeBase64(privAbase64);
  const privB = utils.decodeBase64(privBbase64);

  const pubA = await secp256k1.computePubkey(privA, true);
  const pubB = await secp256k1.computePubkey(privB, true);

  // sign verify
  const data = utils.decodeBase64("1H1SJuGwoSFTqNI8wvVWEdGRpBvTnzLckoZ1QTF7gI0");
  const sigA = await secp256k1.sign(data, privA);
  console.log("verify: ", await secp256k1.verify(data, sigA, pubA));

  const pubABase64 = utils.encodeBase64(pubA);
  const pubBBase64 = utils.encodeBase64(pubB);
  // ecdh && aes256
  const encryped1 = await secp256k1.ext.encryptECDH(privAbase64, pubBBase64, "Hello World");
  const decryped1 = await secp256k1.ext.decryptECDH(privBbase64, pubABase64, encryped1);
  console.log(decryped1);
}

main().then(() => {
	console.log("Done");
}).catch((err) => {
	console.error(err);
});

API

Base methods

import * as secp256k1 from 'react-native-secp256k1-urnm';
MethodParamsReturn typeDescription
verify(sig: Unit8Array, mes32: Unit8Array, pubkey: Unit8Array)sig: signature, mes32: message to verify, pubkey: Unit8ArrayPromise\Verify an ECDSA signature.
sign(msg32: Unit8Array, privKey: Unit8Array)sig: signature, privKey: Unit8ArrayPromise\Create an ECDSA signature.
privateKeyVerify(privKey: Unit8Array)privKey: Unit8ArrayPromise\Verify a private key.
publicKeyCreate(privKey: Uint8Array, compressed?: boolean)privKey: Unit8Array, compressed: booleanPromise\Compute the public key for a secret key.
privateKeyTweakAdd(privKey: Unit8Array, tweak: Unit8Array)privKey: Unit8Array, tweak: Unit8ArrayPromise\Tweak a private key in place by adding tweak to it.
privateKeyTweakMul(privKey: Unit8Array, tweak: Unit8Array)privKey: Unit8Array, tweak: Unit8ArrayPromise\Tweak a private key in place by multiplying it by a tweak.
pubKeyTweakAdd(pubKey: Unit8Array, tweak: Unit8Array)pubKey: Unit8Array, tweak: Unit8ArrayPromise\Tweak a public key by adding tweak times the generator to it.
pubKeyTweakMul(pubKey: Unit8Array, tweak: Unit8Array)pubKey: Unit8Array, tweak: Unit8ArrayPromise\Tweak a public key by multiplying it by a tweak value.
createECDHSecret(privKey: Unit8Array, pubKey: Unit8Array)privKey: Unit8Array, pubKey: Unit8ArrayPromise\Compute an EC Diffie-Hellman secret in constant time.

Ext methods

import { ext } from 'react-native-secp256k1-urnm';
MethodParamsReturn typeDescription
generateKey()Promise\<base64 string>Create a random private key
encryptECDH(privKey: string, pubKey: string, data: string)privKey: base64 string, pubKey: base64 string, data: base64 stringPromise\<base64 string>Encrypt data an EC Diffie-Hellman
decryptECDH(privKey: string, pubKey: string, data: string)privKey: base64 string, pubKey: base64 string, data: base64 stringPromise\<base64 string>Decrypt data an EC Diffie-Hellman

Utils methods

import { utils } from 'react-native-secp256k1-urnm';
MethodParamsReturn typeDescription
encodeBase64(data: Uint8Array)data: Uint8Arraybase64 stringProvide @stablelib/base64 encode function
decodeBase64(base64: string)data: base64 stringUint8ArrayProvide @stablelib/base64 decode function
encodeHex(data: Uint8Array)data: Uint8Arraybase64 stringProvide @stablelib/hex encode function
decodeHex(base64: string)data: base64 stringUint8ArrayProvide @stablelib/hex decode function
encodeBase64WithoutPadding(data: Uint8Array)data: Uint8Arraybase64 stringEncode Unit8Array to base64 string without paddings(=). Used @stablelib/base64.
decodeBase64WithoutPadding(base64: string)data: base64 stringUint8ArrayDecode Unit8Array to base64 string without paddings(=). Used @stablelib/base64.
removeBase64Padding(base64: string)data: base64 stringbase64 stringRemove paddings(=) from base64 string
addBase64Padding(base64: string)data: base64 stringbase64 stringAdd paddings(=) to base64 string

Base64 methods

import { base64 } from 'react-native-secp256k1-urnm';
MethodParamsReturn typeDescription
verify(sig: string, mes32: string, pubkey: string)sig: base64 string, mes32: base64 string, pubkey: base64 stringPromise\Verify an ECDSA signature.
sign(msg32: string, privKey: string)sig: base64 string, privKey: base64 stringPromise\<base64 string>Create an ECDSA signature.
privateKeyVerify(privKey: string)privKey: base64 stringPromise\Verify a private key.
publicKeyCreate(privKey: string, compressed?: boolean)privKey: base64 string, compressed: booleanPromise\<base64 string>Compute the public key for a secret key.
privateKeyTweakAdd(privKey: string, tweak: string)privKey: base64 string, tweak: base64 stringPromise\<base64 string>Tweak a private key in place by adding tweak to it.
privateKeyTweakMul(privKey: string, tweak: string)privKey: base64 string, tweak: base64 stringPromise\<base64 string>Tweak a private key in place by multiplying it by a tweak.
pubKeyTweakAdd(pubKey: string, tweak: string)pubKey: base64 string, tweak: base64 stringPromise\<base64 string>Tweak a public key by adding tweak times the generator to it.
pubKeyTweakMul(pubKey: string, tweak: string)pubKey: base64 string, tweak: base64 stringPromise\<base64 string>Tweak a public key by multiplying it by a tweak value.
createECDHSecret(privKey: string, pubKey: string)privKey: base64 string, pubKey: base64 stringPromise\<base64 string>Compute an EC Diffie-Hellman secret in constant time.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library