Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main' into feat/rns…
Browse files Browse the repository at this point in the history
…-search

# Conflicts:
#	src/theme/MDXComponents.js
  • Loading branch information
alexcss committed Nov 14, 2024
2 parents 3cf0992 + 3d79437 commit 4d08153
Show file tree
Hide file tree
Showing 12 changed files with 637 additions and 5 deletions.
6 changes: 3 additions & 3 deletions docs/01-concepts/rbtc/conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Thus, we have `BTC` and `RBTC` on the Mainnets, which correspond to `tBTC` and `

Enter your BTC address below to verify whether it may be used to peg in from BTC to RBTC.

<AddressVerifier />

## User Guide

- [Mainnet Guide](/concepts/rbtc/networks#mainnet-conversion)
Expand All @@ -47,9 +49,7 @@ You can try the conversion process using either options below;

Watch this explainer video on **How to do BTC & R-BTC Conversions using the Rootstock Powpeg**.

<div class="video-container">
<iframe width="949" height="534" src="https://youtube.com/embed/XTpQW9Rw838" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
<iframe width="949" height="534" src="https://youtube.com/embed/XTpQW9Rw838" frameBorder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowFullScreen></iframe>

### FAQs

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
"@mendable/search": "^0.0.206",
"@splidejs/react-splide": "^0.7.12",
"bootstrap": "5.3.3",
"buffer": "^6.0.3",
"clsx": "^2.0.0",
"docusaurus-plugin-sass": "^0.2.5",
"jssha": "^3.3.1",
"prism-react-renderer": "^2.3.0",
"react": "^18.0.0",
"react-bootstrap": "^2.10.2",
Expand Down
46 changes: 46 additions & 0 deletions src/_utils/pegin-address-verifier/crypto/base58.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Base58 encoding/decoding
// Originally written by Mike Hearn for BitcoinJ
// Copyright (c) 2011 Google Inc
// Ported to JavaScript by Stefan Thomas
// Merged Buffer refactorings from base58-native by Stephen Pair
// Copyright (c) 2013 BitPay Inc

var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
var ALPHABET_MAP = {};
for (var i = 0; i < ALPHABET.length; ++i) {
ALPHABET_MAP[ALPHABET.charAt(i)] = i;
}
var BASE = ALPHABET.length;

module.exports = {
decode: function(string) {
if (string.length === 0) return [];

var i, j, bytes = [0];
for (i = 0; i < string.length; ++i) {
var c = string[i];
if (!(c in ALPHABET_MAP)) throw new Error('Non-base58 character');

for (j = 0; j < bytes.length; ++j) bytes[j] *= BASE
bytes[0] += ALPHABET_MAP[c];

var carry = 0;
for (j = 0; j < bytes.length; ++j) {
bytes[j] += carry;
carry = bytes[j] >> 8;
bytes[j] &= 0xff
}

while (carry) {
bytes.push(carry & 0xff);
carry >>= 8;
}
}
// deal with leading zeros
for (i = 0; string[i] === '1' && i < string.length - 1; ++i){
bytes.push(0);
}

return bytes.reverse();
}
};
116 changes: 116 additions & 0 deletions src/_utils/pegin-address-verifier/crypto/bech32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (c) 2017 Pieter Wuille
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

var CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
var GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];

module.exports = {
decode: decode,
encode: encode,
};


function polymod (values) {
var chk = 1;
for (var p = 0; p < values.length; ++p) {
var top = chk >> 25;
chk = (chk & 0x1ffffff) << 5 ^ values[p];
for (var i = 0; i < 5; ++i) {
if ((top >> i) & 1) {
chk ^= GENERATOR[i];
}
}
}
return chk;
}

function hrpExpand (hrp) {
var ret = [];
var p;
for (p = 0; p < hrp.length; ++p) {
ret.push(hrp.charCodeAt(p) >> 5);
}
ret.push(0);
for (p = 0; p < hrp.length; ++p) {
ret.push(hrp.charCodeAt(p) & 31);
}
return ret;
}

function verifyChecksum (hrp, data) {
return polymod(hrpExpand(hrp).concat(data)) === 1;
}

function createChecksum (hrp, data) {
var values = hrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]);
var mod = polymod(values) ^ 1;
var ret = [];
for (var p = 0; p < 6; ++p) {
ret.push((mod >> 5 * (5 - p)) & 31);
}
return ret;
}

function encode (hrp, data) {
var combined = data.concat(createChecksum(hrp, data));
var ret = hrp + '1';
for (var p = 0; p < combined.length; ++p) {
ret += CHARSET.charAt(combined[p]);
}
return ret;
}

function decode (bechString) {
var p;
var has_lower = false;
var has_upper = false;
for (p = 0; p < bechString.length; ++p) {
if (bechString.charCodeAt(p) < 33 || bechString.charCodeAt(p) > 126) {
return null;
}
if (bechString.charCodeAt(p) >= 97 && bechString.charCodeAt(p) <= 122) {
has_lower = true;
}
if (bechString.charCodeAt(p) >= 65 && bechString.charCodeAt(p) <= 90) {
has_upper = true;
}
}
if (has_lower && has_upper) {
return null;
}
bechString = bechString.toLowerCase();
var pos = bechString.lastIndexOf('1');
if (pos < 1 || pos + 7 > bechString.length || bechString.length > 90) {
return null;
}
var hrp = bechString.substring(0, pos);
var data = [];
for (p = pos + 1; p < bechString.length; ++p) {
var d = CHARSET.indexOf(bechString.charAt(p));
if (d === -1) {
return null;
}
data.push(d);
}
if (!verifyChecksum(hrp, data)) {
return null;
}
return {hrp: hrp, data: data.slice(0, data.length - 6)};
}
29 changes: 29 additions & 0 deletions src/_utils/pegin-address-verifier/crypto/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const ADDRESS_TYPES = {
P2PKH: 'p2pkh',
P2SH: 'p2sh',
BECH32: 'bech32'
};

const NETWORKS = {
MAINNET: 'mainnet',
TESTNET: 'testnet',
REGTEST: 'regtest'
};

const HASH_FIELD_NAMES = {
p2pkh: 'scriptPubKey',
p2sh: 'scriptHash'
};

// Check https://github.com/rsksmart/RSKIPs/blob/2c994cc108885ccc5a116e4aee8c073b5eca5682/IPs/RSKIP170.md#specification for more details.
const ADDRESS_TYPES_CODES = {
p2pkh: '01',
p2sh: '02'
};

module.exports = {
ADDRESS_TYPES,
NETWORKS,
HASH_FIELD_NAMES,
ADDRESS_TYPES_CODES,
};
90 changes: 90 additions & 0 deletions src/_utils/pegin-address-verifier/crypto/p2pkh-p2sha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
let base58 = require('./base58')
let { Buffer } = require('buffer')
let { toHex, sha256Checksum } = require('./utils')
let { ADDRESS_TYPES, NETWORKS, HASH_FIELD_NAMES } = require('./constants')

var DEFAULT_NETWORK_TYPE = 'prod'

const ADDRESS_TYPE_INFO = {}
ADDRESS_TYPE_INFO['00'] = {
network: NETWORKS.MAINNET,
type: ADDRESS_TYPES.P2PKH
}
ADDRESS_TYPE_INFO['05'] = {
network: NETWORKS.MAINNET,
type: ADDRESS_TYPES.P2SH
}
ADDRESS_TYPE_INFO['6f'] = {
network: NETWORKS.TESTNET,
type: ADDRESS_TYPES.P2PKH
}
ADDRESS_TYPE_INFO['c4'] = {
network: NETWORKS.TESTNET,
type: ADDRESS_TYPES.P2SH
}

function getDecoded (address) {
try {
return base58.decode(address)
} catch (e) {
// if decoding fails, assume invalid address
return null
}
}

function getAddressType (address) {
var expectedLength = 25
var decoded = getDecoded(address)

if (decoded) {
var length = decoded.length

if (length !== expectedLength) {
return null
}

var checksum = toHex(decoded.slice(length - 4, length)),
body = toHex(decoded.slice(0, length - 4)),
goodChecksum = sha256Checksum(body)

return checksum === goodChecksum ? toHex(decoded.slice(0, expectedLength - 24)) : null
}

return null
}

function isValidP2PKHandP2SHAddress (address, networkType) {
networkType = networkType || DEFAULT_NETWORK_TYPE

var addressType = getAddressType(address)

if (addressType && ADDRESS_TYPE_INFO[addressType]) {
if (networkType === NETWORKS.MAINNET || networkType === NETWORKS.TESTNET) {
return ADDRESS_TYPE_INFO[addressType].network == networkType
} else {
return true
}
}

return false
}

function getAddressInfo (address) {
const addressType = getAddressType(address)
const addressInfo = ADDRESS_TYPE_INFO[addressType]
if (!addressInfo) {
return null
}

let decodedAddress = Buffer.from(getDecoded(address))
let hash = decodedAddress.slice(1, 21).toString('hex')
let fieldName = HASH_FIELD_NAMES[addressInfo.type]
addressInfo[fieldName] = hash

return addressInfo
}

module.exports = {
isValid: isValidP2PKHandP2SHAddress,
getAddressInfo: getAddressInfo
}
Loading

0 comments on commit 4d08153

Please sign in to comment.