|
| 1 | +# Unique utils |
| 2 | + |
| 3 | +Package containing some low-level utils useful for everyday coding with Substrate and web3. |
| 4 | + |
| 5 | +Provides neat and no-extra-dependencies tools to interact with Substrate and Ethereum addresses, |
| 6 | +strings (utf8 and utf16 encoding/decoding) and more. |
| 7 | + |
| 8 | +For example, Address doesn't depend on Polkadot libraries, WASM, and even ethers.js. |
| 9 | +Minified version of address.js weights just 8 Kb with all hashing and crypto being bundled in. |
| 10 | +And there is no WASM at all when you need just check or convert an address. |
| 11 | + |
| 12 | +Zero dependencies. Definitely typed. Works in browser and Node.js. |
| 13 | + |
| 14 | +## Address |
| 15 | + |
| 16 | +Address object provide several tools for Substrate and Ethereum addresses. |
| 17 | +Like checking, validating, mirroring, boxing and unboxing, formatting and capitalizing. |
| 18 | + |
| 19 | +```ts |
| 20 | +import {Address} from '@unique-nft/utils' |
| 21 | +// or |
| 22 | +// import {Address} from '@unique-nft/utils/address' |
| 23 | +// or |
| 24 | +// import {mirror} from '@unique-nft/utils/address' |
| 25 | + |
| 26 | + |
| 27 | +Address.is.ethereumAddressObject({Ethereum: '0xf8cC75F76d46c3b1c5F270Fe06c8FFdeAB8E5eaB'}) //true |
| 28 | +Address.validate.substrateAddress('5HgvUDiRm5yjRSrrG9B6q6km7KLzkXMxvFLHPZpA13pmwCJQ') // true |
| 29 | +Address.mirror.substrateToEthereum('5HgvUDiRm5yjRSrrG9B6q6km7KLzkXMxvFLHPZpA13pmwCJQ') // 0xf8cC75F76d46c3b1c5F270Fe06c8FFdeAB8E5eaB |
| 30 | +Address.collection.idToAddress(105) // '0x17c4e6453CC49aaaAEAcA894e6d9683E00000069' |
| 31 | +Address.nesting.idsToAddress(10, 5) // '0xf8238CcffF8eD887463Fd5e00000000a00000005' |
| 32 | +Address.to.substrateNormalizedOrMirrorIfEthereum('0xf8cC75F76d46c3b1c5F270Fe06c8FFdeAB8E5eaB') // '5GwWnwbYRzwvcyAmQqCBB4h5JNspv8xPxpUm77wXbooxS3t5' |
| 33 | +``` |
| 34 | + |
| 35 | +And much more! Mirroring, packing/unpacking CrossAccountId in different formats, guessing address types and more. |
| 36 | + |
| 37 | +Also, encoding, decoding and formatting Substrate addresses without WASM or any heavy libraries. |
| 38 | + |
| 39 | +**[Full documentation for the Address util](https://github.com/fend25/unique_utils/blob/master/docs/Address.md)** |
| 40 | + |
| 41 | +## StringUtils |
| 42 | + |
| 43 | +StringUtils provides tools to encode/decode UTF-8 and UTF-16 strings and to pack/unpack hex strings. |
| 44 | + |
| 45 | +```ts |
| 46 | +import {Utf8, Utf16, HexString} from '@unique-nft/utils/string' |
| 47 | +// or import just all in one entry point: |
| 48 | +// import {StringUtils} from '@unique-nft/utils' |
| 49 | + |
| 50 | +Utf8.stringToU8a('a 🌷') // Uint8Array [97, 32, 240, 159, 140, 183] |
| 51 | +Utf8.u8aToString([97, 32, 240, 159, 140, 183]) // "a 🌷" |
| 52 | +Utf8.lengthInBytes('a 🌷') // 6 |
| 53 | + |
| 54 | +Utf16.stringToU16a('a 🌷') // Uint16Array [97, 32, 55356, 57143] |
| 55 | +Utf16.u16aToString([97, 32, 55356, 57143]) // "a 🌷" |
| 56 | +Utf16.lengthInBytes('a 🌷') // 4 |
| 57 | + |
| 58 | +HexString.toU8a('0x6120f09f8cb7') // Uint8Array [97, 32, 240, 159, 140, 183] |
| 59 | +HexString.fromU8a([97, 32, 240, 159, 140, 183]) // "0x6120f09f8cb7" |
| 60 | + |
| 61 | +// even super complex strings! |
| 62 | +Utf8.lengthInBytes('👨🏼👩🏼👧🏼👧🏼') // 41 |
| 63 | +Utf16.lengthInBytes('👨🏼👩🏼👧🏼👧🏼') // 19 |
| 64 | +``` |
| 65 | + |
| 66 | +**[Full documentation for the UTF and hex string helpers](https://github.com/fend25/utf-helpers#readme)** |
0 commit comments