seed_scan package

Submodules

seed_scan.bundle module

The public bundle: what identify writes and the later commands read.

A bundle is one JSON object. dumps writes it and loads reads it back, so a command that reads a bundle imports this module and never seed_scan.identify. It holds no secret: no mnemonic, passphrase, seed, entropy or private key, in any encoding. It is still not harmless, README.md’s What touches the secret says why.

The object’s members:

  • format: the string "seed-scan-bundle".

  • version: the integer VERSION. A reader refuses any other value: a change of meaning takes a new version, never a reuse of the old one.

  • claims: the schemes that claim the words, best first, as bip39_slip39.dispatch.all_seed_types_from_mnemonic names them: "slip39", "electrum_old", "electrum_standard", "electrum_segwit", "electrum_2fa", "electrum_2fa_segwit", "bip39" and "bip39_wordlist". Empty where no scheme claims them.

  • readings: one object per scheme and passphrase that gave a seed:

    • scheme: "bip39", "slip39", "electrum_standard" or "electrum_segwit". For Electrum, the single-signature wallet alone: the multisig layouts a versioned seed can also be a cosigner of are not derived;

    • languages: for "bip39", the word-lists in which the words are a valid mnemonic; empty for the other schemes;

    • passphrase: which passphrase gave the seed, never the passphrase itself: 0 is the empty one, n the n-th typed at the prompt;

    • master_fingerprint: the BIP32 master key’s fingerprint, as 8 hex characters;

    • accounts: one object per account key.

  • not_checked: one object per scheme the words claim and no reading covers, with scheme and reason. A user acts on “nothing found”, so what was not searched is said, never passed over.

  • search: null, or the search for an address the user knows: address, empty where the text given is no Bitcoin address, so that a key typed by mistake is not written; checked, false where the search did not run, and then reason says why; accounts and indexes, the bound (accounts 0 to accounts - 1 of each Bitcoin purpose, of Bitcoin Cash’s, Bitcoin SV’s, eCash’s and SLP’s coin types for a base58 address, and the one account of an Electrum reading, receive and change branches, indexes 0 to indexes - 1); searched, how many positions were compared; and matches, each the position of a reading in readings, the coin of the account the address is in, and the full derivation path of the address.

An account object holds:

  • coin: "bitcoin", "ethereum", "ethereum-classic", "bnb", "bitcoin-cash", "bitcoin-sv", "ecash", "ecash-tokens", "slp-tokens", "litecoin" or "dogecoin";

  • coin_type: its SLIP-0044 number, from 0;

  • purpose: the BIP43 purpose, or null for a layout that has none: Electrum’s, and the legacy layouts of an EVM coin, whose addresses are the key’s own children, and its addresses alone, as seed_scan.identify’s COINS says;

  • account: the account number, from 0; a coin’s legacy layouts, then its addresses alone, are numbered apart, from 0 too;

  • path: the derivation path of the key, hardened levels written with h;

  • xpub: the extended public key at path, in BIP32’s mainnet version whatever the coin, so that each reader decodes one spelling; empty for a layout held key by key;

  • descriptors: for Bitcoin, the receive and change output descriptors, checksum included, as Bitcoin Core imports them; empty for the other coins;

  • index: null, or, for an address alone, its index below xpub: the one child of the key that is an address. Only an account of no purpose and of a coin other than Bitcoin has one;

  • keys: empty, or, for a layout held key by key, the compressed public keys of the children 0, 1 and on of the key at path, as 66 hex characters each, the n-th being child n’s. Only an account of no purpose, of a coin other than Bitcoin and with no index has them.

A layout is held key by key where its key is above hardened accounts, as seed_scan.identify’s COINS says, and README.md’s What touches the secret says why. Each other xpub is three levels below the master key, the depth of BIP44’s accounts, or deeper, except an Electrum reading’s, the master key or its child 0h: that is the reading’s one account, whose addresses are on the key’s own unhardened branches.

class seed_scan.bundle.Account(coin: str, coin_type: int, purpose: int | None, account: int, path: str, xpub: str, descriptors: tuple[str, ...], index: int | None = None, keys: tuple[str, ...] = ())[source]

Bases: object

One account key of a reading: the module docstring’s account object.

class seed_scan.bundle.Bundle(claims: tuple[str, ...], readings: tuple[Reading, ...], not_checked: tuple[NotChecked, ...], search: Search | None)[source]

Bases: object

What identify found: the module docstring’s top-level object.

seed_scan.bundle.CLAIMS = ('slip39', 'electrum_old', 'electrum_standard', 'electrum_segwit', 'electrum_2fa', 'electrum_2fa_segwit', 'bip39', 'bip39_wordlist')

The schemes a claim, or a scheme not checked, names.

seed_scan.bundle.COIN_NAMES = ('bitcoin', 'ethereum', 'ethereum-classic', 'bnb', 'bitcoin-cash', 'bitcoin-sv', 'ecash', 'ecash-tokens', 'slp-tokens', 'litecoin', 'dogecoin')

The coins an account names.

seed_scan.bundle.FORMAT = 'seed-scan-bundle'

The value of the format member.

seed_scan.bundle.LANGUAGES = ('cs', 'en', 'es', 'fr', 'it', 'ja', 'ko', 'pt', 'ru', 'tr', 'zh', 'zh_tw')

The BIP39 word-lists a reading names, as bip39-slip39 names them.

class seed_scan.bundle.Match(reading: int, coin: str, path: str)[source]

Bases: object

Which reading, at which coin’s path, gives the address searched.

class seed_scan.bundle.NotChecked(scheme: str, reason: str)[source]

Bases: object

A scheme the words claim and no reading covers, and why.

seed_scan.bundle.READ_SCHEMES = ('bip39', 'slip39', 'electrum_standard', 'electrum_segwit')

The schemes a reading names.

seed_scan.bundle.REASONS = {'bip39_wordlist': 'every word is in a BIP39 word-list, and the checksum or the word count fails', 'electrum_2fa': "Electrum two-factor seed: its third key is TrustedCoin's", 'electrum_old': 'pre-2.0 Electrum seed: no dependency derives its addresses', 'incomplete_set': 'the shares typed do not complete a set', 'not_an_address': 'not a Bitcoin address: neither searched nor written, in case it is a key', 'not_mainnet': 'not a mainnet address: only Bitcoin mainnet ones are searched'}

Each reason a scheme is not checked, or an address not searched, by name. A passphrase SLIP-0039 refuses is the one reason with a number in it, slip39_passphrase below.

class seed_scan.bundle.Reading(scheme: str, languages: tuple[str, ...], passphrase: int, master_fingerprint: str, accounts: tuple[Account, ...])[source]

Bases: object

One scheme and passphrase that gave a seed, and its account keys.

class seed_scan.bundle.Search(address: str, checked: bool, reason: str, accounts: int, indexes: int, searched: int, matches: tuple[Match, ...])[source]

Bases: object

The search for an address the user knows, and its bound.

seed_scan.bundle.VERSION = 2

The value of the version member this module writes and reads.

seed_scan.bundle.dumps(bundle: Bundle) → str[source]

Return the JSON text of a bundle, members in a fixed order.

Anything but a Bundle is a SeedScanTypeError.

seed_scan.bundle.loads(text: str) → Bundle[source]

Read a bundle back from its JSON text.

Refused, as a SeedScanValueError: text that is not JSON, text nested too deep for Python to read, a number too long for Python to read, another format, another version, a missing member, a negative count, a fingerprint that is not 8 lower-case hex characters, or a match naming no reading. A member of the wrong type is a SeedScanTypeError. Members this version does not name are ignored. A refusal names the member, never its value.

A reader echoes the text members, so each holds only what the format defines, and a key put in one is refused: a scheme, a language, a coin or a reason among CLAIMS, READ_SCHEMES, LANGUAGES, COIN_NAMES and REASONS; a path of m and indexes below 2^31; an xpub whose Base58Check, version bytes, length and key prefix are a BIP32 mainnet public key’s, or empty; a key of keys of 66 hex characters, 02 or 03 first; a Bitcoin account’s two descriptors over that xpub, and none for another coin; an address that is a base58 or a segwit one, or empty. An index is below 2^31, of an account of no purpose and of a coin other than Bitcoin. An account holds an xpub or keys, not both, and keys only as the module docstring says, each key once.

seed_scan.bundle.slip39_passphrase(number: int) → str[source]

Return the reason a passphrase SLIP-0039 refuses is not checked.

A number that is no int, bool included, is a SeedScanTypeError, and one whose reason loads would refuse is a SeedScanValueError.

Parameters:

number – the passphrase’s number, never the passphrase.

Returns:

the reason.

seed_scan.cli module

The seed-scan command.

seed-scan identify reads the mnemonic at a hidden prompt, then passphrases at a second one until an empty answer, and writes the public bundle seed_scan.bundle describes to standard output. What it found is summarized on standard error. Its options are public: the address the user knows, and the bounds of the accounts and keys written and of the search.

The secret is read from the terminal alone, never from an argument, a file, standard input or the environment: where no terminal can be opened the command refuses rather than read standard input. Before it reads anything it refuses every socket and every new process for the rest of the process, and where that refusal is not in place it reads nothing. A refusal is one line on standard error, never a traceback, and says nothing of what was typed. An argument refused is not repeated either, in case it is a mnemonic typed there by mistake.

seed-scan scan reads a bundle from the file named, runs seed_scan.scan.scan on it against the Electrum server the user names as host:port, the EVM node the user names by its URL, or both, and writes the scan result to standard output and a summary to standard error. It reads no secret. README.md’s Scanning a bundle says why there is no default server or node. The Electrum connection is TLS, through btclib_wallet.fetch.transport.PersistentTlsLineTransport, kept across requests and replaced as README.md’s Scanning a bundle says. The server’s certificate is verified: it must chain to a certificate the context trusts and name the host as given, with TLS 1.2 at least. The context trusts the system’s CA certificates, or, where --cafile names a file, the certificates of that file alone, which is how a server of one’s own with a self-signed certificate is reached. With --tcp the connection is plain TCP instead, one connection per request, through btclib_wallet.fetch.transport.TcpLineTransport, and only to a loopback address, as _loopback says. The server is asked first for its genesis block, then for its block at a height Bitcoin Core pins, and one answering another block than Bitcoin’s at either is refused. The tip of each history it answers is then proved on that block, as seed_scan._proof says, and a history whose tip does not prove is refused. A read of a history during which the server’s tip moved is read again, a bounded number of times. The node is a seed_scan.evm.Node, whose docstring says which URLs it takes and how its certificate is verified: --cafile is the Electrum server’s alone. Its connection is kept across requests, and SessionTransport replaces it where data waits on it before a request is sent, as when the node closed it, or a request on it failed. A failure of the server, the node or the connection is one line of fixed text, which names neither the answer nor the addresses asked.

--evm-chain names the chain the node serves, Ethereum where it is not given, and is refused without --node. seed_scan.scan.scan refuses a node whose chain id is another chain’s before it asks the node any address.

seed-scan forks reads a bundle from the file named, runs seed_scan.forks.forks on it against the Electrum server the user names for the chain named, and writes the forks result and a summary as scan writes its own. It reads no secret, connects as scan connects to its server, and says a failure as scan says one. Its server’s blocks are checked, and its histories proved, as scan checks and proves them, at the chain’s checkpoints in place of Bitcoin’s and under the chain’s rule.

seed-scan report reads a bundle from the file named and, where --scan names one, the scan result made from it, and the forks result each --forks names, and writes seed_scan.report.report’s text to standard output. It reads no secret, and refuses every socket and every new process before it reads a file, as identify does. The report goes out as its UTF-8 bytes, with no newline translation, so that the file it is redirected to is what its last line hashes, on Windows too.

A file the user names is read as its UTF-8 bytes, with no newline translation either, so that a digest of its text, as the bundle’s in scan’s result and the report’s lines naming each input, is the digest of the file.

seed_scan.cli.main(argv: list[str] | tuple[str, ...] | None = None, *, prompt: Callable[[str], str] = <function _hidden>, forbid_network: Callable[[], None] = <function _forbid_network>, connect: Callable[[str, int, ssl.SSLContext | None], LineTransport] = <function _connect>) → int[source]

Run the command; answer its exit status.

prompt, forbid_network and connect are what a test replaces: the hidden prompt, the refusal of every socket and new process that identify and report install, and the transport to a server, which _connect says.

seed_scan.evm module

An EVM node’s JSON-RPC, for the reads seed_scan.scan makes.

Node asks an execution client the user names: its chain id, its latest block number, and an address’s balance, its nonce and its balance of an ERC-20 token, each at a block the caller numbers or at the tag latest. The methods and their types are those of the Ethereum execution-apis specification, read at commit 981aa66d79364be8322a9c358aaf0a571cca0873:

The call is EIP-20’s balanceOf(address), encoded as the Solidity ABI specification says: its input is the function selector, the first four bytes of the Keccak-256 of that signature, then the address encoded as a uint160, padded on the left with zero bytes to 32; its result, bytes of the same base types, is one uint256, 32 bytes big-endian, as the specification encodes what a function returns. A result of any other length is refused, 0x included.

The response is read as JSON-RPC 2.0’s section 5 defines it: an object whose jsonrpc is "2.0", whose id is the request’s, and which holds either result or error, the error an object with an integer code and a string message as section 5.1 says. Anything else, an HTTP status other than 200 included, is refused, and no refusal quotes the answer.

An address is 0x and 40 hex digits, in one case, lower or upper, or in mixed case where the case is its EIP-55 checksum, as coin_addresses.evm.assert_valid_address reads it. Node.balance, Node.nonce and Node.token_balance refuse any other address before the node is asked, Node.token_balance its contract as well as its holder, a string as a SeedScanValueError and any other type as a SeedScanTypeError; none quotes it.

The request goes through btclib_wallet.fetch.transport.http_request, whose network policy is the one btclib_wallet.fetch.transport’s TlsLineTransport states for its own: a bounded read, refused rather than truncated past MAX_ANSWER octets; a timeout over the whole exchange, name resolution, connect, handshake, send and read; no proxy taken from the environment; and no redirect followed. Over https the certificate is verified against the system’s CA certificates, which OpenSSL’s SSL_CERT_FILE and SSL_CERT_DIR choose, and must name the host.

The URL is https, except on a loopback address, where http is accepted too: the addresses asked tie a wallet’s accounts together, and over plain HTTP they would cross the network in the clear. The loopback address is written as an address, 127.0.0.1 or [::1]: a name such as localhost is resolved by the system, and RFC 6761 asks resolvers to answer it with the loopback address without requiring it. A URL with a login is refused, and so is one that is not printable ASCII.

EVM_CHAINS holds the chains a node may serve, each with the chain id its eth_chainId answers, the coins of a bundle whose accounts are read on it and the ERC-20 tokens whose balances are read; the comments above it cite each chain and each token. A key gives the same address on each, so the bundle’s Ethereum accounts, at m/44h/60h and its legacy layouts, are read on every one: what such an account held on Ethereum when Ethereum Classic parted from it is on that chain too. Ethereum Classic’s, at m/44h/61h, its SLIP-0044 coin type, and at its legacy layout, are read on Ethereum Classic alone, where its own wallets derive them, and BNB’s, at m/44h/714h and its address alone, on BNB Smart Chain alone, as seed_scan.identify’s comments on its COINS cite.

seed_scan.evm.EVM_CHAINS = mappingproxy({'ethereum': EvmChain(name='ethereum', title='Ethereum', chain_id=1, symbol='ETH', coins=('ethereum',), tokens=(EvmToken(contract='0xdAC17F958D2ee523a2206206994597C13D831ec7', symbol='USDT', decimals=6), EvmToken(contract='0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', symbol='USDC', decimals=6), EvmToken(contract='0x6B175474E89094C44Da98b954EedeAC495271d0F', symbol='DAI', decimals=18), EvmToken(contract='0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', symbol='WETH', decimals=18), EvmToken(contract='0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599', symbol='WBTC', decimals=8))), 'ethereum-classic': EvmChain(name='ethereum-classic', title='Ethereum Classic', chain_id=61, symbol='ETC', coins=('ethereum', 'ethereum-classic'), tokens=()), 'bnb-smart-chain': EvmChain(name='bnb-smart-chain', title='BNB Smart Chain', chain_id=56, symbol='BNB', coins=('ethereum', 'bnb'), tokens=(EvmToken(contract='0x55d398326f99059fF775485246999027B3197955', symbol='USDT', decimals=18), EvmToken(contract='0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d', symbol='USDC', decimals=18), EvmToken(contract='0x1AF3F329e8BE154074D8769D1FFa4eE058B1DBc3', symbol='DAI', decimals=18), EvmToken(contract='0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', symbol='WBNB', decimals=18), EvmToken(contract='0x7130d2A12B9BCbFAe4f2634d864A1Ee1Ce3Ead9c', symbol='BTCB', decimals=18))), 'polygon-pos': EvmChain(name='polygon-pos', title='Polygon PoS', chain_id=137, symbol='POL', coins=('ethereum',), tokens=(EvmToken(contract='0xc2132D05D31c914a87C6611C10748AEb04B58e8F', symbol='USDT', decimals=6), EvmToken(contract='0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359', symbol='USDC', decimals=6), EvmToken(contract='0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', symbol='USDC.e', decimals=6), EvmToken(contract='0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063', symbol='DAI', decimals=18), EvmToken(contract='0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270', symbol='WMATIC', decimals=18), EvmToken(contract='0x1BFD67037B42Cf73acF2047067bd4F2C47D9BfD6', symbol='WBTC', decimals=8))), 'arbitrum-one': EvmChain(name='arbitrum-one', title='Arbitrum One', chain_id=42161, symbol='ETH', coins=('ethereum',), tokens=(EvmToken(contract='0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9', symbol='USDT', decimals=6), EvmToken(contract='0xaf88d065e77c8cC2239327C5EDb3A432268e5831', symbol='USDC', decimals=6), EvmToken(contract='0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8', symbol='USDC.e', decimals=6), EvmToken(contract='0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1', symbol='DAI', decimals=18), EvmToken(contract='0x82aF49447D8a07e3bd95BD0d56f35241523fBab1', symbol='WETH', decimals=18), EvmToken(contract='0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f', symbol='WBTC', decimals=8))), 'op-mainnet': EvmChain(name='op-mainnet', title='OP Mainnet', chain_id=10, symbol='ETH', coins=('ethereum',), tokens=(EvmToken(contract='0x94b008aA00579c1307B0EF2c499aD98a8ce58e58', symbol='USDT', decimals=6), EvmToken(contract='0x01bFF41798a0BcF287b996046Ca68b395DbC1071', symbol='USDT0', decimals=6), EvmToken(contract='0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85', symbol='USDC', decimals=6), EvmToken(contract='0x7F5c764cBc14f9669B88837ca1490cCa17c31607', symbol='USDC.e', decimals=6), EvmToken(contract='0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1', symbol='DAI', decimals=18), EvmToken(contract='0x4200000000000000000000000000000000000006', symbol='WETH', decimals=18), EvmToken(contract='0x68f180fcCe6836688e9084f035309E29Bf0A2095', symbol='WBTC', decimals=8))), 'base': EvmChain(name='base', title='Base', chain_id=8453, symbol='ETH', coins=('ethereum',), tokens=(EvmToken(contract='0xfde4C96c8593536E31F229EA8f37b2ADa2699bb2', symbol='USDT', decimals=6), EvmToken(contract='0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', symbol='USDC', decimals=6), EvmToken(contract='0xd9aAEc86B65D86f6A7B5B1b0c42FFA531710b6CA', symbol='USDbC', decimals=6), EvmToken(contract='0x50c5725949A6F0c72E6C4a641F24049A917DB0Cb', symbol='DAI', decimals=18), EvmToken(contract='0x4200000000000000000000000000000000000006', symbol='WETH', decimals=18), EvmToken(contract='0xcbB7C0000aB88B473b1f5aFd9ef808440eed33Bf', symbol='cbBTC', decimals=8))), 'avalanche-c-chain': EvmChain(name='avalanche-c-chain', title='Avalanche C-Chain', chain_id=43114, symbol='AVAX', coins=('ethereum',), tokens=(EvmToken(contract='0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7', symbol='USDt', decimals=6), EvmToken(contract='0xc7198437980c041c805A1EDcbA50c1Ce5db95118', symbol='USDT.e', decimals=6), EvmToken(contract='0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E', symbol='USDC', decimals=6), EvmToken(contract='0xA7D7079b0FEaD91F3e65f86E8915Cb59c1a4C664', symbol='USDC.e', decimals=6), EvmToken(contract='0xd586E7F844cEa2F87f50152665BCbc2C279D8d70', symbol='DAI.e', decimals=18), EvmToken(contract='0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7', symbol='WAVAX', decimals=18), EvmToken(contract='0x50b7545627a5162F82A992c33b87aDc75187B218', symbol='WBTC.e', decimals=8)))})

The chains a node may serve, by name.

class seed_scan.evm.EvmChain(name: str, title: str, chain_id: int, symbol: str, coins: tuple[str, ...], tokens: tuple[EvmToken, ...])[source]

Bases: object

An EVM chain a node serves, and the bundle’s coins read on it.

name is how the command and the result name it, and title how a summary and the report do. chain_id is what eth_chainId answers on it, and symbol names its native asset, of which a balance in wei counts 10**-18. coins are the coins of a bundle whose accounts are read on it, and tokens the ERC-20 tokens whose balances are read.

class seed_scan.evm.EvmToken(contract: str, symbol: str, decimals: int)[source]

Bases: object

An ERC-20 token whose balances a chain’s scan reads.

contract is the token’s address, EIP-55 encoded. A balance counts 10**-decimals of the token symbol names. Neither is read from the contract: both are those of the source the comment above EVM_CHAINS cites.

seed_scan.evm.MAX_ANSWER = 4096

The octets an answer may hold. A response object holding a quantity of at most 64 hex digits, which quantity refuses past, or the 32 bytes of a token balance, is well under it.

class seed_scan.evm.Node(url: str, *, transport: Callable[[Request, float], tuple[int, bytes]] = <function urlopen_transport>, timeout: float = 30.0)[source]

Bases: object

An EVM node the user names, asked over JSON-RPC 2.0.

Construction checks the URL, as the module docstring says, and opens nothing. transport is btclib_wallet.fetch.transport.http_request’s own parameter: its default opens one connection per request, and a SessionTransport keeps one open across requests. timeout bounds each request, in seconds.

balance, nonce and token_balance check their address first, and token_balance its contract too, as the module docstring says; they refuse a bad one without asking anything.

Each method asks one request. A failure below the answer is a btclib_wallet.exceptions.FetchError that names the node alone. Its failure_type is the one client_errors gives: the class of what failed, which holds no message, where the exchange did not complete, and None elsewhere. What the transport raised is not kept, as its messages can quote the node. An HTTP status other than 200 is its HttpError, and a JSON-RPC error object its RpcError, with the code the node gave and neither the message nor the data. An answer that is not a JSON-RPC 2.0 response to the request, or whose result is not a quantity, or for a token balance not one 32-byte word, is a FetchError too. None of these chains an exception, not even one the caller is handling.

balance, nonce and token_balance read the state at the block numbered block, or at latest where it is None. They refuse a block that is no int, a bool among them, as a SeedScanTypeError, and one below 0 as a SeedScanValueError, without asking anything.

balance(address: str, block: int | None = None) → int[source]

Return an address’s balance, in wei.

block_number() → int[source]

Return the latest block’s number, eth_blockNumber’s.

chain_id() → int[source]

Return the chain id, eth_chainId’s.

property name: str

Return the node as scheme, host and port, with no path or query.

nonce(address: str, block: int | None = None) → int[source]

Return an address’s nonce.

eth_getTransactionCount’s: the specification says why the name is no longer the count of the transactions the address sent.

property requests: int

Return how many requests were sent, or were about to be.

token_balance(contract: str, address: str, block: int | None = None) → int[source]

Return an address’s balance of the ERC-20 token at contract.

eth_call of the token’s balanceOf, as the module docstring says, in the token’s units.

seed_scan.evm.quantity(value: object) → int[source]

Return the integer a JSON-RPC quantity spells, of 256 bits at most.

A quantity is a string: 0x, then 0 or up to 64 lower-case hex digits with no leading zero. Anything else is refused, a string as a SeedScanValueError and any other type as a SeedScanTypeError; neither quotes it.

seed_scan.exceptions module

Exception classes.

These exist only to tell an exception raised by seed_scan from one raised by any other code: each derives from the built-in that says what kind of failure it is, and adds nothing to it.

SeedScanException makes that telling apart a single except. It is inherited beside the built-in and not instead of it: SeedScanValueError is a ValueError, so code catching the built-in catches it, as json.JSONDecodeError is a ValueError too.

It is caught and never raised: every refusal the package raises is to be one of the two below it, and which one says whether the value was wrong or the type was.

The classes are this package’s own rather than those of a package it builds on: a caller catching another package’s base class does not catch these, and one catching ValueError catches both.

exception seed_scan.exceptions.SeedScanException[source]

Bases: Exception

Anything seed_scan raised, whatever kind of failure it is.

Never raised: the two below it are, and each says which kind of failure it was.

exception seed_scan.exceptions.SeedScanTypeError[source]

Bases: SeedScanException, TypeError

An input of a type no conversion accepts: a caller error.

exception seed_scan.exceptions.SeedScanValueError[source]

Bases: SeedScanException, ValueError

A value no valid input could carry; the package’s usual refusal.

seed_scan.forks module

What a bundle’s keys hold on a chain forked from Bitcoin.

forks reads the text of a bundle, with seed_scan.bundle alone, and asks a btclib_wallet.fetch.history.HistoryReader on a chain forked from Bitcoin which of the bundle’s accounts have history there. It reads no secret, as seed_scan.scan reads none. seed_scan.cli gives it an Electrum server the user names for that chain, refuses one whose blocks at the chain’s checkpoints are another chain’s, and proves each history it answers on the chain’s last checkpoint, under the chain’s rule.

CHAINS holds the chains read. A key’s coins at a fork are on both chains, at the same script, and a wallet of the new chain may since have used the key at Bitcoin’s paths or at coin types of its own: seed_scan.identify’s comment on its COINS cites the wallets of each chain and the paths they derive at. Bitcoin SV and eCash each parted from Bitcoin Cash after Bitcoin Cash parted from Bitcoin, so each also holds what a key held on Bitcoin Cash at its own later split. And a base58 address spells the same script on each of these chains, so a payment made on one chain to an address of another chain’s wallet is at that wallet’s path.

So the accounts read, on whichever chain, are those whose script the chain spends with the key, P2PKH: each Bitcoin account whose descriptors are pkh, as BIP44’s purpose and Electrum’s standard reading have them, and each account of a coin of CHAINS, of ecash-tokens or of slp-tokens, P2PKH over its xpub. Each is read as seed_scan.scan reads a Bitcoin account, with the same two bounds. A Bitcoin account of another script is listed as not checked, with the reason: the chain’s transactions carry no witness, so a key does not sign for a segwit script there. The bundle’s other coins are scan’s, and are not listed.

The balance of an account is read as seed_scan.scan reads it, off the history the server answers for the chain. at_fork is read off the same history, from the transactions confirmed below the chain’s Chain.fork_height, which Bitcoin’s history holds too: what the account held when the chain parted from Bitcoin, which for Bitcoin SV and eCash is Bitcoin Cash’s fork, not their own later split from Bitcoin Cash.

Each token protocol below writes its tokens either in the script of the output holding them, or in an OP_RETURN output of the transaction. seed_scan.report names the protocols of each chain, and says that their tokens are not checked.

Where the tokens are in the script, the output is not read as paying the account’s script, so its satoshis are not counted, and an account paid only through such outputs reads as unused. These are:

Where the tokens are in an OP_RETURN output, the output holding them pays its script as any output does: where that is an account’s P2PKH, its satoshis are counted, and its tokens are not read. These are:

The result is one JSON object, which dumps writes. It holds public data only. Its members:

  • format: the string "seed-scan-forks".

  • version: the integer VERSION. A change of meaning takes a new version.

  • bundle: the SHA-256 of the bundle’s text, UTF-8 encoded, as hex.

  • chain: the chain read, a key of CHAINS.

  • server: the server asked, as the user named it.

  • fork_height: the height of the chain’s first block that is not Bitcoin’s.

  • gap_limit and account_gap: the bound of the search, as in seed_scan.scan’s result.

  • searched: one object per reading, coin and purpose read: reading, coin, purpose, accounts and cut_short, as seed_scan.scan’s result has them.

  • used: one object per account with history on the chain, in the order read: coin; the members of an item of seed_scan.scan’s used, each address in Bitcoin’s legacy format, which spells the same script on each chain of CHAINS; and at_fork, in satoshis.

  • not_checked: one object per reading, coin and purpose not read, with reading, coin, purpose and reason.

loads reads a result back, against the text of the bundle it was made from.

seed_scan.forks.BITCOIN_CASH = Chain(name='bitcoin-cash', title='Bitcoin Cash', fork_height=478559, checkpoints=((478559, '000000000000000000651ef99cb9fcbe0dadde1d424bd9f15ff20136191a5eec'), (970892, '000000000000000000e76270e57f4ebbd2b282db46a1695984935126c0a68cd8')))

Bitcoin Cash. Its checkpoints are two of Bitcoin Cash Node’s, the fork block, which Electron Cash pins too, and the latest of the list. A chain that parted from Bitcoin Cash before that height has another block there, as eCash has from 661648 on. The sources: https://gitlab.com/bitcoin-cash-node/bitcoin-cash-node/-/blob/e13b48e37b3005dca9a316f75aea456697a5dfd6/src/chainparams.cpp#L269-271 https://gitlab.com/bitcoin-cash-node/bitcoin-cash-node/-/blob/e13b48e37b3005dca9a316f75aea456697a5dfd6/src/chainparams.cpp#L323 https://github.com/Electron-Cash/Electron-Cash/blob/7f4afadbf62a6daa1d6d0563d0951141ddcc6a7c/electroncash/networks.py#L68-L70 https://github.com/Bitcoin-ABC/bitcoin-abc/blob/988b903deb04311b12ba067734dab3ff9f5411a7/src/networks/abc/checkpoints.cpp#L58-L61

seed_scan.forks.BITCOIN_SV = Chain(name='bitcoin-sv', title='Bitcoin SV', fork_height=478559, checkpoints=((478559, '000000000000000000651ef99cb9fcbe0dadde1d424bd9f15ff20136191a5eec'), (773040, '000000000000000007a8838b1ead3b288c504de5eee26f3189fdbcd0b1a006ac')))

Bitcoin SV, which holds Bitcoin Cash’s blocks up to their own split. Its checkpoints are the fork block from Bitcoin, which ElectrumSV pins, and ElectrumSV’s checkpoint after the split, which tells it from Bitcoin Cash. None of the Bitcoin SV node’s checkpoints does: its list ends at 530359, whose id Bitcoin Cash Node’s list holds too. The sources: https://github.com/electrumsv/electrumsv/blob/7ab1c7ceaaaa0f8079da21356989d9b250e874b4/electrumsv/networks.py#L62-L74 https://github.com/bitcoin-sv/bitcoin-sv/blob/6504a3aff65ba97c0f6c80962b033e35ecbfed4b/src/chainparams.cpp#L1157-L1159 https://gitlab.com/bitcoin-cash-node/bitcoin-cash-node/-/blob/e13b48e37b3005dca9a316f75aea456697a5dfd6/src/chainparams.cpp#L275-277

seed_scan.forks.CHAINS = mappingproxy({'bitcoin-cash': Chain(name='bitcoin-cash', title='Bitcoin Cash', fork_height=478559, checkpoints=((478559, '000000000000000000651ef99cb9fcbe0dadde1d424bd9f15ff20136191a5eec'), (970892, '000000000000000000e76270e57f4ebbd2b282db46a1695984935126c0a68cd8'))), 'bitcoin-sv': Chain(name='bitcoin-sv', title='Bitcoin SV', fork_height=478559, checkpoints=((478559, '000000000000000000651ef99cb9fcbe0dadde1d424bd9f15ff20136191a5eec'), (773040, '000000000000000007a8838b1ead3b288c504de5eee26f3189fdbcd0b1a006ac'))), 'ecash': Chain(name='ecash', title='eCash', fork_height=478559, checkpoints=((478559, '000000000000000000651ef99cb9fcbe0dadde1d424bd9f15ff20136191a5eec'), (661648, '000000000000000004284c9d8b2c8ff731efeaec6be50729bdc9bd07f910757d'), (949200, '000000000000000098694560815190dba8bbe2f06c08a7c23837df3c4886cba2')))})

The chains forks reads, by name, in the order their accounts are read.

class seed_scan.forks.Chain(name: str, title: str, fork_height: int, checkpoints: tuple[tuple[int, str], ...])[source]

Bases: object

A chain forked from Bitcoin, and how its server is told from another’s.

name is how the command and the result name it, and the coin of its own accounts in a bundle. title is how a summary and a refusal name it. fork_height is the height of its first block that is not Bitcoin’s. checkpoints are heights and the id of the chain’s block at each, as hex in display order: a server answering another block at one of them serves another chain, though it shares Bitcoin’s genesis block.

seed_scan.forks.ECASH = Chain(name='ecash', title='eCash', fork_height=478559, checkpoints=((478559, '000000000000000000651ef99cb9fcbe0dadde1d424bd9f15ff20136191a5eec'), (661648, '000000000000000004284c9d8b2c8ff731efeaec6be50729bdc9bd07f910757d'), (949200, '000000000000000098694560815190dba8bbe2f06c08a7c23837df3c4886cba2')))

eCash, which holds Bitcoin Cash’s blocks up to its own split, at Bitcoin Cash’s upgrade of November 2020. Its checkpoints are the fork block from Bitcoin, which Electrum ABC pins; Bitcoin ABC’s checkpoint at that upgrade, where Bitcoin Cash Node’s list holds another block; and the latest of Bitcoin ABC’s list. Its wallets write an address in CashAddr, with the prefix ecash, and its base58 prefixes are Bitcoin’s, so the legacy address of the result spells the same script. The sources: https://github.com/Bitcoin-ABC/bitcoin-abc/blob/988b903deb04311b12ba067734dab3ff9f5411a7/electrum/electrumabc/networks.py#L106-L110 https://github.com/Bitcoin-ABC/bitcoin-abc/blob/988b903deb04311b12ba067734dab3ff9f5411a7/src/networks/abc/checkpoints.cpp#L58-L61 https://gitlab.com/bitcoin-cash-node/bitcoin-cash-node/-/blob/e13b48e37b3005dca9a316f75aea456697a5dfd6/src/chainparams.cpp#L290-292 https://github.com/Bitcoin-ABC/bitcoin-abc/blob/988b903deb04311b12ba067734dab3ff9f5411a7/src/networks/abc/checkpoints.cpp#L106-L109 https://github.com/Bitcoin-ABC/bitcoin-abc/blob/988b903deb04311b12ba067734dab3ff9f5411a7/src/kernel/chainparams.cpp#L198-L203

seed_scan.forks.FORMAT = 'seed-scan-forks'

The value of the format member.

class seed_scan.forks.ForkAccount(coin: str, reading: int, purpose: int | None, account: int, path: str, addresses: tuple[Address, ...], transactions: tuple[Transaction, ...], confirmed: int, unconfirmed: int, tip: Tip, at_fork: int)[source]

Bases: object

An account with history on the chain, and what it held at the fork.

class seed_scan.forks.ForkSearched(reading: int, coin: str, purpose: int | None, accounts: int, cut_short: bool)[source]

Bases: object

The accounts of one reading, coin and purpose that were read.

class seed_scan.forks.ForksResult(bundle: str, chain: str, server: str, fork_height: int, gap_limit: int, account_gap: int, searched: tuple[ForkSearched, ...], used: tuple[ForkAccount, ...], not_checked: tuple[Skipped, ...])[source]

Bases: object

What forks found: the module docstring’s top-level object.

class seed_scan.forks.Skipped(reading: int, coin: str, purpose: int | None, reason: str)[source]

Bases: object

The accounts of a reading, coin and purpose not read, and why.

seed_scan.forks.VERSION = 1

The value of the version member this module writes.

seed_scan.forks.dumps(result: ForksResult) → str[source]

Return the JSON text of a forks result, members in a fixed order.

Anything but a ForksResult is a SeedScanTypeError.

seed_scan.forks.forks(text: str, reader: HistoryReader, *, chain: str, server: str, gap_limit: int = 20, account_gap: int = 3) → ForksResult[source]

Run account discovery on a chain forked from Bitcoin, for a bundle.

text is the bundle’s text, chain a key of CHAINS, and server what the result names as the server reader asks, which serves that chain. The bundle is read, and every account it reads checked, before reader is asked anything. Refused as seed_scan.scan.scan refuses them: text that is not a bundle, a descriptor or an xpub that does not parse, accounts not numbered from 0, a server that seed_scan.scan.parse_server refuses, a bound below 1, an account whose discovery seed_scan.scan refuses in both reads of it, and an account whose balance, or a used address’s, is outside 0 to 21 million bitcoin. So is one whose holding at the fork is outside that bound. A chain not in CHAINS is refused too, without quoting it. A chain, a server or a bound of a wrong type is a SeedScanTypeError.

seed_scan.forks.loads(text: str, bundle: str) → ForksResult[source]

Read a forks result back from its JSON text, against its bundle.

Read as seed_scan.scan.loads reads a scan result, and refused as it refuses one, each refusal opening with forks result:. A used account and a search name accounts of the bundle that forks reads, by coin. Refused besides: a chain not in CHAINS, unquoted; a fork_height that is not the chain’s; an at_fork outside 0 to 21 million bitcoin, or not 0 where no transaction listed is confirmed below the fork height; and an item of not_checked that is not one forks writes for the bundle, its reason included.

seed_scan.identify module

From a mnemonic to the public bundle: the one module that holds a secret.

identify asks bip39_slip39.dispatch which schemes claim the words, and derives a seed for each one this version reads, with the empty passphrase and each one typed: BIP39 in every word-list, a set of SLIP-0039 shares, and Electrum’s “standard” and “segwit” seeds. From each seed it writes the master fingerprint and the account public keys COINS declares, and, for a layout whose key is above hardened accounts, the public keys of its first addresses in place of that key. A scheme the words claim and no reading covers is written as not checked, with the reason: Electrum’s pre-2.0 and two-factor seeds, a BIP39 sentence whose checksum fails, an incomplete set of shares.

Given an address the user knows, it compares the address’s script with those of every Bitcoin account it wrote, within a bound written beside the result. A base58 address is also compared with those of each account at Bitcoin Cash’s, Bitcoin SV’s, eCash’s and SLP’s coin types, since it spells the same script on each of these chains; a match names the coin of the path it is at. An address of another network is not searched, and the bundle says so; a text that is no Bitcoin address is not written either, since a key typed by mistake would otherwise land in the bundle.

Nothing here opens a connection: seed_scan.cli refuses one for the whole run. What leaves this module is seed_scan.bundle’s objects, which hold public data only.

seed_scan.identify.COINS = (Coin(name='bitcoin', coin_type=0, purposes=(44, 49, 84, 86), legacy=(), listed=(), alone=()), Coin(name='ethereum', coin_type=60, purposes=(44,), legacy=('m/44h/60h/0h',), listed=('m/44h/60h',), alone=()), Coin(name='ethereum-classic', coin_type=61, purposes=(44,), legacy=('m/44h/60h/160720h/0h',), listed=(), alone=()), Coin(name='bnb', coin_type=714, purposes=(44,), legacy=(), listed=(), alone=('m/44h/60h/9172h/3604/9999',)), Coin(name='bitcoin-cash', coin_type=145, purposes=(44,), legacy=(), listed=(), alone=()), Coin(name='bitcoin-sv', coin_type=236, purposes=(44,), legacy=(), listed=(), alone=()), Coin(name='ecash', coin_type=899, purposes=(44,), legacy=(), listed=(), alone=()), Coin(name='ecash-tokens', coin_type=1899, purposes=(44,), legacy=(), listed=(), alone=()), Coin(name='slp-tokens', coin_type=245, purposes=(44,), legacy=(), listed=(), alone=()), Coin(name='litecoin', coin_type=2, purposes=(44, 49, 84), legacy=(), listed=(), alone=()), Coin(name='dogecoin', coin_type=3, purposes=(44,), legacy=(), listed=(), alone=()))

The coins and purposes a bundle declares, the order they are written in. Bitcoin’s are the four btclib_wallet.bip44 maps to an address encoding; each other coin’s are those its wallets derive accounts at, its legacy layouts those they derive addresses at, and its addresses alone those they derive one by one, the comments above citing them.

class seed_scan.identify.Coin(name: str, coin_type: int, purposes: tuple[int, ...], legacy: tuple[str, ...] = (), listed: tuple[str, ...] = (), alone: tuple[str, ...] = ())[source]

Bases: object

A coin whose account keys a bundle declares, and their purposes.

legacy holds the paths of the keys of its legacy layouts, of no BIP43 purpose: each key’s own children are the addresses, and the bundle writes it as one account of purpose None, numbered from 0 in this order. listed holds the paths of the keys of its legacy layouts that are above hardened accounts: the bundle writes each as one more such account, which holds the public keys of the key’s first children and not the key, README.md’s What touches the secret says why. alone holds the paths of addresses its wallets derive one by one, outside any account: the bundle writes each as one more such account, whose key is the address’s parent and whose index is the address’s.

seed_scan.identify.identify(mnemonic: str, passphrases: list[str] | tuple[str, ...], prompt: Callable[[str], str], *, accounts: int = 5, address: str | None = None, indexes: int = 20, keys: int = 100) → Bundle[source]

Return the public bundle of a mnemonic.

passphrases are those to try, the empty one first; a reading names each by its position in them. prompt asks for a further SLIP-0039 share where the words are one share of a set: it shows the text it is given and answers what the user typed, hidden.

accounts is how many accounts of each coin and purpose are written, from account 0; the one key of a legacy layout or of an address alone is written once. keys is how many public keys of a listed layout’s children are written, from child 0, in place of its key. Where address is given, the receive and change branches of each Bitcoin account written, and of each account at Bitcoin Cash’s, Bitcoin SV’s, eCash’s and SLP’s coin types where it is a base58 address, are searched for it, from index 0 to indexes - 1.

A refusal names no secret: the words and the passphrases are never quoted.

seed_scan.report module

What identify, scan and forks found, as text a person acts on.

report reads the text of a bundle and, where they are given, of the scan result and the forks results made from it, with seed_scan.bundle.loads, seed_scan.scan.loads and seed_scan.forks.loads. It reads no secret, since none holds one, and opens no connection. Every coin and purpose of the bundle is listed: as read, with what its used accounts hold, or as not checked, with the reason; and so is every one a forks result reads, on its chain. A user acts on “nothing found”, so what was not searched is said, never passed over.

The report is lines of UTF-8 text, each ending in a newline, in this order. <...> stands for a value and ... for more of the same; every other character is literal.

  • seed-scan report <VERSION>: VERSION says the layout below. A change of meaning takes a new version.

  • bundle: sha256 <hex>: the SHA-256 of the bundle’s text, UTF-8 encoded; seed_scan.cli says how the command reads the file.

  • scan: sha256 <hex>, the SHA-256 of the scan result’s text, then server: <host:port> or server: none, node: <url>, <chain>, chain id <n>, latest block <n> before the first address was read or node: none, and bounds: account gap <n>, gap limit <n>; or scan: none alone, where no result is given. <chain> is the title of the EVM chain the node serves, and where a node is named, the node’s line is followed by <chain> tokens checked: <symbol> <contract>, ..., the ERC-20 tokens the scan read, or <chain> tokens checked: none, then by <chain> tokens: not checked: <reason>, for every other token.

  • per forks result, in the order given, forks: sha256 <hex>, the SHA-256 of its text, then chain: <title>, forked from Bitcoin at height <n>, server: <host:port> and bounds: account gap <n>, gap limit <n>, then <protocols>: not checked: <reason> per item CHAIN_TOKENS holds for its chain; or forks: none alone, where none is given.

  • claims: <scheme>, ...: the schemes that claim the words, or claims: none.

  • not derived: <scheme>: <reason>: one line per scheme the words claim and identify did not derive.

  • address search: none; or address search: not run: <reason>; or address search: <address>, <n> positions, accounts 0 to <n>, indexes 0 to <n>, then found: reading <n>, <path> per match, or found: none.

  • per reading, reading <n>: <scheme>, passphrase <n>, master fingerprint <hex>, with , languages <code> ... after the scheme of a BIP39 reading. Then, two spaces in, per coin and purpose of its accounts, in the bundle’s order, <coin>, purpose <n> or, for Electrum’s layout, <coin>, Electrum account, or, for an EVM coin’s legacy layouts and its addresses alone, <coin>, legacy account, with , on <chain> after a coin the node’s chain reads, followed by:

    • : not checked: <reason>, where the scan result does not read it, or no result is given;

    • or : accounts 0 to <n> read, <n> used, then , cut short: run identify with --accounts above <n>, then scan where the bundle’s accounts end before the account gap was reached, or, for a legacy account, , cut short: run identify with --keys above <n>, then scan where a layout’s keys end before the gap limit was.

    Four spaces in, per used Bitcoin account, account <n>, <path>: <n> sat confirmed, <n> sat unconfirmed, <n> transactions, read up to height <n>; and six spaces in, per used address, <receive or change> <path> <address>: <n> sat confirmed, <n> sat unconfirmed, the path being the address’s own. Per used account the node read, account <n>, <path>: <n> wei; six spaces in, per used address, receive <path> <address>: <n> wei, nonce <n>, block <n>, with address for receive in a legacy account, whose addresses are on no branch; and eight spaces in, per token it holds, <symbol> <contract>: <n>, <n> decimals, the balance in units of 10**-decimals of the token. What an address holds is the node’s answer at the block its line names. seed_scan.scan reads every address at the block of the node’s line; seed_scan.scan.loads also reads a result whose addresses name a later one, and an account’s sum is then no state of one block.

    Then, per forks result, two spaces in, per coin and purpose of the reading’s accounts of each coin seed_scan.forks reads, <coin>, purpose <n>, on <title> or <coin>, Electrum account, on <title>, followed by what was read or not checked as above, the rerun ending , then forks. Four spaces in, per used account, account <n>, <path>: <n> sat confirmed, <n> sat unconfirmed, <n> sat at the fork, <n> transactions, read up to height <n>, then its used addresses as a Bitcoin account’s.

  • total: bitcoin, <n> sat confirmed, <n> sat unconfirmed, <n> used accounts, each account key once, where a server is named, and total: <chain name>, <n> wei of <symbol>, <n> used accounts, each account key once, where a node is, the name and the symbol being those seed_scan.evm.EVM_CHAINS gives the node’s chain, followed by total: <chain name>, <symbol> <contract>: <n>, <n> decimals, each account key once per token whose total is not zero; per forks result, total: <chain>, <n> sat confirmed, <n> sat unconfirmed, <n> sat at the fork, <n> used accounts, each account key once. Two readings can hold the same account keys, as the same passphrase typed twice gives: each is listed with its accounts, and a total counts once the accounts paid by the same outputs, those of one key read as one script type. Two accounts of different keys can still list one address, so the Bitcoin and EVM totals sum the balance of each address once, and so do the token totals its token balances, as the first used account listing it has them. A forks total holds what each account had at the fork, so it counts once the accounts of one public key and chain code, whatever else their xpubs spell: those list the same scripts, and any others none of them.

  • digest: sha256 <hex>: the SHA-256 of every line above it, so that sed '$d' report.txt | shasum -a 256 gives it back.

seed_scan.report.CHAIN_TOKENS = mappingproxy({'bitcoin-cash': (('cashtokens', 'forks does not read an output carrying them as paying its script, so its satoshis are not counted, and an account paid only through such outputs reads as unused'), ('slp tokens', 'forks counts the satoshis of an output holding them, and does not read the tokens, which an OP_RETURN output of the transaction assigns to it')), 'bitcoin-sv': (('stas, bsv-20 and bsv-21 tokens', 'forks does not read an output carrying them as paying its script, so its satoshis are not counted, and an account paid only through such outputs reads as unused'), ('slp, run and tokenized tokens', 'forks counts the satoshis of an output holding them, and does not read the tokens, which an OP_RETURN output of the transaction assigns to it')), 'ecash': (('slp and alp tokens', 'forks counts the satoshis of an output holding them, and does not read the tokens, which an OP_RETURN output of the transaction assigns to it'),)})

The token protocols of each chain of seed_scan.forks.CHAINS, as a report names them, each with why its tokens are not checked. seed_scan.forks says where each protocol writes its tokens.

seed_scan.report.NOT_IN_FORKS = 'the forks result does not read it'

Why an account is not checked where the forks result does not read it.

seed_scan.report.NOT_IN_SCAN = 'the scan result does not read it'

Why an account is not checked where the scan result does not read it.

seed_scan.report.NO_SCAN = 'no scan result was given'

Why an account is not checked where no scan result is given.

seed_scan.report.NO_TOKENS = 'scan asks the balances of the tokens checked alone, so an address with no native balance, zero nonce and no listed token balance reads as unused, whatever other tokens it holds'

Why an EVM chain’s other tokens are not checked where the node was asked.

seed_scan.report.TOKENS_IN_OP_RETURN = 'forks counts the satoshis of an output holding them, and does not read the tokens, which an OP_RETURN output of the transaction assigns to it'

Why the tokens a protocol writes in an OP_RETURN output are not checked.

seed_scan.report.TOKENS_IN_SCRIPT = 'forks does not read an output carrying them as paying its script, so its satoshis are not counted, and an account paid only through such outputs reads as unused'

Why the tokens a protocol writes in the script of the output holding them are not checked.

seed_scan.report.VERSION = 2

The version the first line names.

seed_scan.report.report(bundle: str, result: str | None = None, forks: Sequence[str] = ()) → str[source]

Return the report on a bundle and on the results made from it.

Parameters:
  • bundle – the bundle’s text.

  • result – the scan result’s text, or None where there is none, as after scan refused, or its server or its node failed: scan’s lines then list every account as not checked.

  • forks – the texts of the forks results, one per chain.

Returns:

the report, the module docstring’s layout.

Refused as seed_scan.bundle.loads, seed_scan.scan.loads and seed_scan.forks.loads refuse their texts, a result made from another bundle among them, and where two forks results are of one chain. forks other than a list or a tuple is a SeedScanTypeError, before any text is read.

seed_scan.scan module

Account discovery on a public bundle: which accounts were used, and what for.

scan reads the text of a bundle, with seed_scan.bundle alone, and asks a btclib_wallet.fetch.history.HistoryReader which of its Bitcoin accounts have history, and a seed_scan.evm.Node which of its accounts of the coins that chain reads were used on the EVM chain it serves. It reads no secret: a bundle holds none, and seed_scan.bundle.loads refuses a key that is not public. seed_scan.cli gives it an Electrum server, an EVM node or both, each named by the user. A coin whose backend is not given is listed as not checked.

Bitcoin and the EVM chains’ coins are the coins read. For Bitcoin, for each reading, each BIP43 purpose is read from account 0 up, as BIP44’s Account discovery reads it, until account_gap accounts in a row have no history; an Electrum reading has one account, which is read alone. Each account is btclib_wallet.bip44.discover_account on the receive and change descriptors the bundle holds, each branch read until gap_limit indexes in a row have no history. A purpose whose accounts in the bundle end before that gap is reached is said to be cut short, since an account after them may be used. Every other coin of the bundle is listed as not checked, with the reason.

The caller names the EVM chain the node serves, a key of seed_scan.evm.EVM_CHAINS, and a node whose eth_chainId answers another chain id is refused before any address is asked. The accounts read are those of the coins its seed_scan.evm.EvmChain lists, as seed_scan.evm says; an account of a coin one chain alone reads is listed as not checked on any other. Each coin’s accounts are read the same way as Bitcoin’s, purpose by purpose and with the same two bounds, on the receive branch alone: the addresses of an account are those of the keys 0/0, 0/1 and on below its xpub, as coin_addresses.evm writes them. A legacy layout, of purpose null, is one account, read on no branch: its addresses are those of the keys 0, 1 and on below its xpub, or, for a layout the bundle holds key by key, those of its keys, which end the read where they end. An address its wallets derive alone is one more such account, whose index names the one child of its xpub read. A coin’s legacy layouts and addresses alone are each read, whatever account_gap. They are cut short only where a layout held key by key ends before gap_limit of its keys in a row are unused, since a later child may be used. Each address is asked its balance, its nonce and its balance of each ERC-20 token the chain’s seed_scan.evm.EvmChain lists, all at the scan’s block, the latest block, asked once before the first address. It is used where any of them is not zero.

One block for every address keeps a node whose backends answer from different heights from listing an address at a block before the scan’s, which loads refuses. A node that answers a read at that block with an error, as a backend that has not reached the block may, fails the scan as any failure of the node does.

A token balance counts as use, so that an address that only received a token, and so has a nonce of zero and no balance of the native asset, is found, and so is its account: a recovery would rather over-search, at one request per token for each address read. An address with no native balance, zero nonce and no listed token balance reads as unused, whatever other tokens it holds.

The balance of an account is read off the history discovery returns: an output paying one of its used scripts counts until a transaction of that history spends it. confirmed is what the confirmed transactions leave unspent; unconfirmed is the change the mempool’s transactions make to it, negative where they spend a confirmed output. The balance of a used address is read the same way off its own script, so the addresses’ add up to the account’s. The history is what the reader answers, so the balance is only as complete as the server’s list. Two transactions of a history that spend one output, as a replacement and the transaction it replaces do, would both count, where at most one can confirm: the mempool part of an answer is no snapshot, so the account is read again once, and refused where that read lists two such transactions too. So is an account whose used positions are not each within gap_limit of the one before: discovery reads in windows, and a payment a window found, which the last read no longer lists, leaves the positions it led to past the gap.

The result is one JSON object, which dumps writes. It holds public data only. Its members:

  • format: the string "seed-scan-scan".

  • version: the integer VERSION. A change of meaning takes a new version.

  • bundle: the SHA-256 of the bundle’s text, UTF-8 encoded, as hex.

  • server: the Electrum server asked, as the user named it, or null where none was.

  • gap_limit and account_gap: the bound of the search, GAP_LIMIT and ACCOUNT_GAP unless the caller gives others.

  • searched: one object per reading and purpose read: reading, the position of the reading in the bundle; purpose, null for an Electrum reading; accounts, how many accounts were read, from 0; and cut_short, true where the bundle’s accounts end before the account gap was reached, or, for an EVM coin’s accounts of purpose null, where a layout’s keys end before the gap limit was.

  • used: one object per Bitcoin account with history, in the order read: reading, purpose, account and path, as the bundle has them; addresses, each used position as branch, index, address, and its confirmed and unconfirmed balance in satoshis; transactions, each as tx_id, in display order, and height, null for the mempool’s; confirmed and unconfirmed, in satoshis; and tip, the height and block id the history was read up to.

  • not_checked: one object per reading and coin not read, with reading, coin and reason: NO_SERVER for Bitcoin, NO_NODE for a coin an EVM chain reads, the coin’s OTHER_CHAIN for a coin one chain alone reads on a node of another chain, and NOT_READ for every other coin.

  • evm: null where no EVM node was asked, or the EVM scan: node, the node asked, as its scheme, host and port, with no path or query; chain, the chain read, a key of seed_scan.evm.EVM_CHAINS; chain_id, as the node answered it, which is that chain’s; block, the latest block’s number, asked before the first address; tokens, the ERC-20 tokens read, each contract, symbol and decimals as the chain’s seed_scan.evm.EvmChain lists them, any other token being not checked; searched, as above, with coin after reading and purpose null for the legacy layouts and the addresses alone, for the accounts of each coin read; and used, one object per account with a used address: coin, reading, purpose, account and path, as the bundle has them; addresses, each used address as index, its index on the receive branch or, for a legacy layout, below the key itself, which for a layout held key by key is the position of its key in keys, and for an address alone the bundle’s index, address, balance, nonce, block, the block they were read at, and tokens, its token balances; balance, the sum of the addresses’ balances; and tokens, the sum of their balances of each token.

A balance in evm is in wei, 10**-18 of the chain’s native asset, written as a string of decimal digits: RFC 8259’s section 6 holds an integer past 2**53 to be one JSON readers may not agree on. A token balance is contract, symbol and decimals, as the token is listed, and balance, in units of 10**-decimals of the token, written the same way; a token is listed where its balance is not zero, in the order of tokens.

loads reads a result back, against the text of the bundle it was made from.

seed_scan.scan.ACCOUNT_GAP = 3

The default of account_gap. BIP44 stops at the first account whose external chain has no transactions; a recovery would rather over-search.

class seed_scan.scan.Address(branch: int, index: int, address: str, confirmed: int, unconfirmed: int)[source]

Bases: object

A used position of an account: where it is, its address, its balance.

class seed_scan.scan.EvmAccount(coin: str, reading: int, purpose: int | None, account: int, path: str, addresses: tuple[EvmAddress, ...], balance: str, tokens: tuple[EvmTokenBalance, ...])[source]

Bases: object

An account with a used address on the EVM chain, and what it holds.

class seed_scan.scan.EvmAddress(index: int, address: str, balance: str, nonce: int, block: int, tokens: tuple[EvmTokenBalance, ...])[source]

Bases: object

A used EVM address: its index, what it holds, and the block read.

class seed_scan.scan.EvmScan(node: str, chain: str, chain_id: int, block: int, tokens: tuple[EvmToken, ...], searched: tuple[EvmSearched, ...], used: tuple[EvmAccount, ...])[source]

Bases: object

What the EVM node answered: the module docstring’s evm.

class seed_scan.scan.EvmSearched(reading: int, coin: str, purpose: int | None, accounts: int, cut_short: bool)[source]

Bases: object

The accounts of one reading, coin and purpose read on the EVM chain.

class seed_scan.scan.EvmTokenBalance(contract: str, symbol: str, decimals: int, balance: str)[source]

Bases: object

A balance other than zero of an ERC-20 token the chain reads.

contract, symbol and decimals are those of the token’s seed_scan.evm.EvmToken, and balance counts 10**-decimals of it.

seed_scan.scan.FORMAT = 'seed-scan-scan'

The value of the format member.

seed_scan.scan.GAP_LIMIT = 20

BIP44’s gap limit, the default of gap_limit.

seed_scan.scan.NOT_READ = "scan reads Bitcoin and the EVM chains' coins alone in this version"

The reason every other coin is not checked for.

seed_scan.scan.NO_NODE = 'no EVM node was named'

The reason an EVM coin is not checked for, where no node is given.

seed_scan.scan.NO_SERVER = 'no Electrum server was named'

The reason Bitcoin is not checked for, where no server is given.

class seed_scan.scan.NotRead(reading: int, coin: str, reason: str)[source]

Bases: object

A coin of a reading that scan does not read, and why.

seed_scan.scan.OTHER_CHAIN = mappingproxy({'ethereum-classic': 'read on Ethereum Classic alone, and the node serves another chain', 'bnb': 'read on BNB Smart Chain alone, and the node serves another chain'})

The reason a coin one EVM chain alone reads is not checked for, where the node serves another chain, by coin.

class seed_scan.scan.ScanResult(bundle: str, server: str | None, gap_limit: int, account_gap: int, searched: tuple[Searched, ...], used: tuple[UsedAccount, ...], not_checked: tuple[NotRead, ...], evm: EvmScan | None)[source]

Bases: object

What scan found: the module docstring’s top-level object.

class seed_scan.scan.Searched(reading: int, purpose: int | None, accounts: int, cut_short: bool)[source]

Bases: object

The accounts of one reading and purpose that were read.

class seed_scan.scan.Tip(height: int, block_id: str)[source]

Bases: object

The block a history was read up to.

class seed_scan.scan.Transaction(tx_id: str, height: int | None)[source]

Bases: object

A transaction of an account’s history, and its height or None.

class seed_scan.scan.UsedAccount(reading: int, purpose: int | None, account: int, path: str, addresses: tuple[Address, ...], transactions: tuple[Transaction, ...], confirmed: int, unconfirmed: int, tip: Tip)[source]

Bases: object

An account with history: where it is, what it used, what it holds.

seed_scan.scan.VERSION = 2

The value of the version member this module writes.

seed_scan.scan.dumps(result: ScanResult) → str[source]

Return the JSON text of a scan result, members in a fixed order.

Anything but a ScanResult is a SeedScanTypeError.

seed_scan.scan.loads(text: str, bundle: str) → ScanResult[source]

Read a scan result back from its JSON text, against its bundle.

bundle is the text of the bundle the result was made from, which seed_scan.bundle.loads reads. Refused, as a SeedScanValueError: text that is not JSON, text nested too deep for Python to read, another format, another version, a missing member, a negative count, a bound below 1, a result whose bundle is not the SHA-256 of bundle, a result naming neither a server nor a node, a Bitcoin search with no server, a purpose searched twice, a used account or an address listed twice, and a number too long for Python to read. The bundle is refused as scan refuses it. A member of the wrong type is a SeedScanTypeError. Members this version does not name are ignored. A refusal names the member, never its value.

A reader echoes the text members, so each holds only what the format defines: a server that parse_server takes; a node that seed_scan.evm.Node takes, and names as it is written; a chain of seed_scan.evm.EVM_CHAINS, with that chain’s chain_id and tokens, and a coin in evm that the chain reads; a token balance of a token of tokens, as it is listed there, in their order and not zero; ids of 64 hex characters; a balance in wei or in a token’s units of decimal digits, an address’s below 2**256; a reason that is the one scan gives that coin, for a coin of that reading it did not read. What the result says of the bundle is checked against it: each search and each used account names accounts of the bundle of its coin, a used account one its search read, its path the bundle’s, each address the one the account’s keys give at that position, an EVM address with a balance, a nonce or a token balance, read at the scan’s block or later, and each account’s balance and token balances the sums of its addresses’. Each search’s accounts and cut_short are the ones scan gives for the used accounts listed, the bundle’s accounts and account_gap.

What a used account lists is what scan could have found: an address at least, each branch’s used positions within gap_limit of the one before, from index 0, or an address alone at its index; for Bitcoin, a transaction at least, a confirmed one where confirmed is not 0 and one of the mempool’s where an address’s unconfirmed is not 0, and balances that leave from 0 to 21 million bitcoin unspent.

seed_scan.scan.parse_server(text: str, name: str) → tuple[str, int][source]

Return the host and port of an Electrum server named host:port.

The one rule seed-scan scan --server takes a server by and loads reads server by, so that every result scan writes reads back. The host is not empty, and holds no space and no character that does not print; an IPv6 host is in brackets, which are dropped. The port is ASCII digits naming 1 to 65535, leading zeros allowed. Anything else is refused, as a SeedScanValueError opening with name and quoting nothing; a text or a name that is no string, as a SeedScanTypeError.

seed_scan.scan.scan(text: str, reader: HistoryReader | None, *, server: str | None = None, node: Node | None = None, evm_chain: str = 'ethereum', gap_limit: int = 20, account_gap: int = 3) → ScanResult[source]

Run account discovery on the bundle whose text is text.

reader reads Bitcoin’s accounts, and server is what the result names as the server it asks; node reads the accounts of the coins evm_chain reads, a key of seed_scan.evm.EVM_CHAINS, which the node serves. Either may be None, and not both; a coin whose backend is None is listed as not checked. The bundle is read, and every account of it checked, before reader or node is asked anything. An evm_chain not in seed_scan.evm.EVM_CHAINS is refused without quoting it, and one that is no string is a SeedScanTypeError. Refused, as seed_scan.bundle.loads refuses it: text that is not a bundle. Refused as a SeedScanValueError too: a descriptor that does not parse, as one whose key is no point of the curve, an extended public key that does not decode, a key of a layout held key by key that is no point of the curve, accounts not numbered from 0, and a server that parse_server refuses. A bound below 1 is refused, and one that is no integer is a SeedScanTypeError, as is a server that is no string beside a reader. Refused once reader is asked: an account whose history lists two transactions spending one output, or whose used positions a read under gap_limit does not reach, in two reads; and an account whose balance, or a used address’s, is outside 0 to 21 million bitcoin, which loads would refuse. Refused once node is asked: a node whose chain id is not evm_chain’s.

Module contents

The seed_scan package: what it publishes, and the version metadata.

What a mnemonic of unknown scheme holds: its scheme, its addresses, their coins and the forks that concern them. README.md’s What touches the secret says which command reads the mnemonic and which do not.

__all__ here is the root of the package’s public tree: the modules a caller reaches from this name. Each of them declares its own __all__, so a walk that starts here has a declared surface at every node – which is why the list is not pkgutil.iter_modules: discovery would answer the file tree, and a module added to the directory would publish itself rather than being published.

name is not in it, nor are the metadata dunders. name is the distribution’s name and not a member of the tree, __version__ bound by a star import would overwrite the importing module’s own, and each is still an attribute here: seed_scan.__version__ is how a caller reads the version and seed_scan.name how it reads the name.

Nothing is imported eagerly. A module is imported when it is first asked for, through the __getattr__ at the bottom of this file, so import seed_scan is the metadata lookup below and nothing else, and the import graph keeps its shape: importing every module here would put the whole package in sys.modules before any single module of it could be imported first, which is the situation tests/imports_test.py exists to make impossible.

What that costs is worth stating: mypy reads a module-level __getattr__ as a promise that any attribute may exist, so seed_scan.exception is Any to it and a misspelling on this package is a runtime AttributeError rather than a reported error. The spellings a caller actually writes – from seed_scan import exceptions, import seed_scan.exceptions, from seed_scan.exceptions import SeedScanValueError – resolve against the real modules and stay checked.