Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Base58 decoding in naka3.sh for ARM large number handling #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

moodmosaic
Copy link

Resolves #1.

@moodmosaic
Copy link
Author

/cc @wileyj

@jcnelson
Copy link
Owner

jcnelson commented Aug 19, 2024

This doesn't seem to work for mainnet addresses:

$ cat /tmp/b58.sh
#!/bin/bash

# Decode b58 text into a hex byte stream
# $1: base58 text
# from https://github.com/grondilu/bitcoin-bash-tools
function b58_decode() {
   local base58_chars="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
   local base58_text="$1"
   local base58_length=${#base58_chars}
   local result="0"
   local i=0
   local char
   local value

   for (( i=0; i<${#base58_text}; i++ )); do
       char="${base58_text:i:1}"
       value=$(expr index "$base58_chars" "$char")
       value=$((value - 1))
       result=$(echo "$result * $base58_length + $value" | bc)
   done

   # Convert the result to hex using bc and xxd without printf
   echo "obase=16; $result" | bc | xxd -r -p | xxd -p
}

# Decode a base58 Bitcoin address into "$version_byte $hash_bytes"
# $1: address
# prints the above
function b58_address_decode() {
   local addr="$1"
   local decoded_hex=$(b58_decode "$addr")
   local version_byte="${decoded_hex:0:2}"
   local hash_bytes="${decoded_hex:2:40}"
   echo "$version_byte $hash_bytes"
}

b58_address_decode "$1"

To see:

$ bash ./b58.sh 1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs
f5 4a5851e9372b87810a8e60cdd2e7cfd80b6e31c7

It should instead decode to:

$ bash ./b58.sh 1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs
00 f54a5851e9372b87810a8e60cdd2e7cfd80b6e31c7

- Replaced `b58_decode` to use `bc` and `xxd` for large numbers.
  This change avoids overflow issues that occurred with the previous
  `dc` and `printf` based implementation, which failed with large
  Base58 values, particularly on ARM platforms.

- Updated `b58_address_decode` to work with the new `b58_decode`
  implementation, ensuring accurate extraction of the version byte
  and hash bytes from decoded Base58 addresses.

This update resolves issues with Base58 decoding on ARM systems, where
handling large numbers is critical for correct script operation.
@moodmosaic moodmosaic force-pushed the fix/arm-base58-decoding branch from b028171 to 91b4f9c Compare September 9, 2024 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stack-tx Operation Failing Due to Address Compatibility
2 participants