From ce4615d407bc86303bf58c4a733a7250fab87abc Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Wed, 2 Oct 2024 09:55:12 +0200 Subject: [PATCH] implement wallet address function --- package-lock.json | 7 +++++++ package.json | 1 + src/components/steps/HomeScreen.tsx | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index ac4b16e..d621f89 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "@noble/hashes": "^1.5.0", "@react-native-async-storage/async-storage": "^2.0.0", + "bech32": "^2.0.0", "react": "18.3.1", "react-native": "0.75.3", "react-native-modal": "^13.0.1", @@ -5709,6 +5710,12 @@ ], "license": "MIT" }, + "node_modules/bech32": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/bech32/-/bech32-2.0.0.tgz", + "integrity": "sha512-LcknSilhIGatDAsY1ak2I8VtGaHNhgMSYVxFrGLXv+xLHytaKZKcaUJJUE7qmBr7h33o5YQwP55pMI0xmkpJwg==", + "license": "MIT" + }, "node_modules/bl": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", diff --git a/package.json b/package.json index cc9ce4c..bd77105 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "dependencies": { "@noble/hashes": "^1.5.0", "@react-native-async-storage/async-storage": "^2.0.0", + "bech32": "^2.0.0", "react": "18.3.1", "react-native": "0.75.3", "react-native-modal": "^13.0.1", diff --git a/src/components/steps/HomeScreen.tsx b/src/components/steps/HomeScreen.tsx index 02ed5f5..59404ac 100644 --- a/src/components/steps/HomeScreen.tsx +++ b/src/components/steps/HomeScreen.tsx @@ -2,6 +2,10 @@ import {FC, useEffect, useState } from "react"; import { SafeAreaView, StyleSheet, Text, View } from "react-native"; import Button from "../Button"; import { Camera, useCameraDevice, useCameraPermission, useCodeScanner } from "react-native-vision-camera"; +import { bytesToHex, hexToBytes } from "@noble/hashes/utils"; +import { bech32 } from "bech32"; +import { ripemd160 } from "@noble/hashes/ripemd160"; +import { sha256 } from "@noble/hashes/sha256"; enum HomeSteps { Home, @@ -41,7 +45,18 @@ const HomeScreen: FC = props => { }); const walletAddress = () => { - return walletKey; + var pubkey = hexToBytes(walletKey); + if (pubkey[0] == 0x04 && (pubkey.length == 65)) { + pubkey[0] = 0x02 | (pubkey[64] & 1); + pubkey = pubkey.slice(0, 33); + } + + const hash = ripemd160(sha256(pubkey)); + + var words = bech32.toWords(hash); + words.unshift(0); + + return bech32.encode('bc', words); } useEffect(() => {