<commit_before>{ Bitcoin, Crypto, BigInteger } = require '../lib/bitcoinjs-lib.js'
{ ECDSA, ECKey, Script, Address, Base58, Message } = Bitcoin
{ sha256ripe160 } = Bitcoin.Util
{ randomBytes, bytesToHex, hexToBytes, bytesToBase64, base64ToBytes } = Crypto.util
{ SHA256, charenc: { UTF8 } } = Crypto
{ getSECCurveByName } = BigInteger.sec
{ OP_HASH160, OP_EQUAL } = Bitcoin.Opcode.map

TESTNET = !!~document.location.hash.indexOf('TESTNET')

ADDR_PUB  = if TESTNET then 0x6f else 0x00
ADDR_P2SH = if TESTNET then 0xc4 else 0x05
ADDR_PRIV = if TESTNET then 0xef else 0x80
PRIVKEY_LEN = 32
PUBKEY_LEN = 65
ADDR_LEN = 20
#PUBKEY_COMPRESS_LEN = 33 # not supported yet

# Turn a byte array to a bitcoin address
get_address = (hash, version) ->
  [ version, hash... ] = hash unless version?
  hash = sha256ripe160 hash if version in [ ADDR_PUB, ADDR_P2SH ] and hash.length isnt ADDR_LEN
  Address::toString.call { version, hash }

# Get the public key of a private key
get_pub = (secexp, compressed=false) ->
  secexp = BigInteger.fromByteArrayUnsigned secexp unless secexp instanceof BigInteger
  (getSECCurveByName 'secp256k1')
    .getG().multiply(secexp)
    .getEncoded(compressed)

# Creates an 2-of-3 multisig
create_multisig = (pubkeys) ->
  # order pubkeys to ensure the same multisig address
  # regardless of the order
  pubkeys = pubkeys.map(bytesToHex).sort().map(hexToBytes)
  script = Script.createMultiSigOutputScript 2, pubkeys
  address = get_address script.buffer, ADDR_P2SH
  { pubkeys, script, address }
<commit_msg>check if its a browser environment before accessing the location<commit_after>{ Bitcoin, Crypto, BigInteger } = require '../lib/bitcoinjs-lib.js'
{ ECDSA, ECKey, Script, Address, Base58, Message } = Bitcoin
{ sha256ripe160 } = Bitcoin.Util
{ randomBytes, bytesToHex, hexToBytes, bytesToBase64, base64ToBytes } = Crypto.util
{ SHA256, charenc: { UTF8 } } = Crypto
{ getSECCurveByName } = BigInteger.sec
{ OP_HASH160, OP_EQUAL } = Bitcoin.Opcode.map

TESTNET = document?.location? and !!~document.location.hash.indexOf('TESTNET')

ADDR_PUB  = if TESTNET then 0x6f else 0x00
ADDR_P2SH = if TESTNET then 0xc4 else 0x05
ADDR_PRIV = if TESTNET then 0xef else 0x80
PRIVKEY_LEN = 32
PUBKEY_LEN = 65
ADDR_LEN = 20
#PUBKEY_COMPRESS_LEN = 33 # not supported yet

# Turn a byte array to a bitcoin address
get_address = (hash, version) ->
  [ version, hash... ] = hash unless version?
  hash = sha256ripe160 hash if version in [ ADDR_PUB, ADDR_P2SH ] and hash.length isnt ADDR_LEN
  Address::toString.call { version, hash }

# Get the public key of a private key
get_pub = (secexp, compressed=false) ->
  secexp = BigInteger.fromByteArrayUnsigned secexp unless secexp instanceof BigInteger
  (getSECCurveByName 'secp256k1')
    .getG().multiply(secexp)
    .getEncoded(compressed)

# Creates an 2-of-3 multisig
create_multisig = (pubkeys) ->
  # order pubkeys to ensure the same multisig address
  # regardless of the order
  pubkeys = pubkeys.map(bytesToHex).sort().map(hexToBytes)
  script = Script.createMultiSigOutputScript 2, pubkeys
  address = get_address script.buffer, ADDR_P2SH
  { pubkeys, script, address }

{'fraction_non_alphanumeric': 0.09786420146636914, 'fraction_numerical': 0.03634045266177877, 'mean_word_length': 4.613595706618963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4366891', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1086, 'n_words': 307}