|
| 1 | +import 'dart:typed_data'; |
| 2 | + |
| 3 | +import 'package:blake2b/blake2b_hash.dart'; |
| 4 | +import 'package:bs58check/bs58check.dart'; |
| 5 | +import 'package:convert/convert.dart'; |
| 6 | +import 'package:tezster_dart/helper/generateKeys.dart'; |
| 7 | +import 'package:tezster_dart/src/soft-signer/soft_signer.dart'; |
| 8 | + |
| 9 | +class TezosMessageUtils { |
| 10 | + static String writeBranch(String branch) { |
| 11 | + return hex.encode(base58 |
| 12 | + .decode(branch) |
| 13 | + .sublist(2, base58.decode(branch).length - 4) |
| 14 | + .toList()); |
| 15 | + // return hex.encode(base58.decode(branch).sublist(2).toList()); |
| 16 | + } |
| 17 | + |
| 18 | + static String writeInt(int value) { |
| 19 | + if (value < 0) { |
| 20 | + throw new Exception('Use writeSignedInt to encode negative numbers'); |
| 21 | + } |
| 22 | + var byteHexList = Uint8List.fromList(hex.decode(twoByteHex(value))); |
| 23 | + for (var i = 0; i < byteHexList.length; i++) { |
| 24 | + if (i != 0) byteHexList[i] ^= 0x80; |
| 25 | + } |
| 26 | + var result = hex.encode(byteHexList.reversed.toList()); |
| 27 | + return result; |
| 28 | + } |
| 29 | + |
| 30 | + static String twoByteHex(int n) { |
| 31 | + if (n < 128) { |
| 32 | + var s = ('0' + n.toRadixString(16)); |
| 33 | + return s.substring(s.length - 2); |
| 34 | + } |
| 35 | + String h = ''; |
| 36 | + if (n > 2147483648) { |
| 37 | + var r = BigInt.from(n); |
| 38 | + while (r.compareTo(BigInt.zero) != -1) { |
| 39 | + var _h = ('0' + (r & BigInt.from(127)).toRadixString(16)); |
| 40 | + h = _h.substring(_h.length - 2) + h; |
| 41 | + r = r >> 7; |
| 42 | + } |
| 43 | + } else { |
| 44 | + var r = n; |
| 45 | + while (r > 0) { |
| 46 | + var _h = ('0' + (r & 127).toRadixString(16)); |
| 47 | + h = _h.substring(_h.length - 2) + h; |
| 48 | + r = r >> 7; |
| 49 | + } |
| 50 | + } |
| 51 | + return h; |
| 52 | + } |
| 53 | + |
| 54 | + static String writeAddress(String address) { |
| 55 | + var base58data = base58.decode(address).sublist(3); |
| 56 | + base58data = base58data.sublist(0, base58data.length - 4); |
| 57 | + var _hex = hex.encode(base58data); |
| 58 | + if (address.startsWith("tz1")) { |
| 59 | + return "0000" + _hex; |
| 60 | + } else if (address.startsWith("tz2")) { |
| 61 | + return "0001" + _hex; |
| 62 | + } else if (address.startsWith("tz3")) { |
| 63 | + return "0002" + _hex; |
| 64 | + } else if (address.startsWith("KT1")) { |
| 65 | + return "01" + _hex + "00"; |
| 66 | + } else { |
| 67 | + throw new Exception( |
| 68 | + 'Unrecognized address prefix: ${address.substring(0, 3)}'); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + static String writePublicKey(String publicKey) { |
| 73 | + if (publicKey.startsWith("edpk")) { |
| 74 | + return "00" + hex.encode(base58.decode(publicKey).sublist(4)); |
| 75 | + } else if (publicKey.startsWith("sppk")) { |
| 76 | + return "01" + hex.encode(base58.decode(publicKey).sublist(4)); |
| 77 | + } else if (publicKey.startsWith("p2pk")) { |
| 78 | + return "02" + hex.encode(base58.decode(publicKey).sublist(4)); |
| 79 | + } else { |
| 80 | + throw new Exception('Unrecognized key type'); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + static Uint8List simpleHash(Uint8List message, int size) { |
| 85 | + return Uint8List.fromList(Blake2bHash.hashWithDigestSize(256, message)); |
| 86 | + } |
| 87 | + |
| 88 | + static String readSignatureWithHint(Uint8List opSignature, SignerCurve hint) { |
| 89 | + opSignature = Uint8List.fromList(opSignature); |
| 90 | + if (hint == SignerCurve.ED25519) { |
| 91 | + return GenerateKeys.readKeysWithHint(opSignature, '09f5cd8612'); |
| 92 | + } else if (hint == SignerCurve.SECP256K1) { |
| 93 | + return GenerateKeys.readKeysWithHint(opSignature, '0d7365133f'); |
| 94 | + } else if (hint == SignerCurve.SECP256R1) { |
| 95 | + return GenerateKeys.readKeysWithHint(opSignature, '36f02c34'); |
| 96 | + } else { |
| 97 | + throw Exception('Unrecognized signature hint, "$hint"'); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + static String writeBoolean(bool b) { |
| 102 | + return b ? "ff" : "00"; |
| 103 | + } |
| 104 | +} |
0 commit comments