From b0338aa76ad80846cc696ea57f024af201a06b22 Mon Sep 17 00:00:00 2001 From: julian Date: Wed, 18 Oct 2023 14:46:57 -0600 Subject: [PATCH] quick and dirty SLP check --- lib/services/coins/bitcoincash/bch_utils.dart | 29 +++++++++++++++++++ .../coins/bitcoincash/bch_utils_test.dart | 25 ++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 lib/services/coins/bitcoincash/bch_utils.dart create mode 100644 test/services/coins/bitcoincash/bch_utils_test.dart diff --git a/lib/services/coins/bitcoincash/bch_utils.dart b/lib/services/coins/bitcoincash/bch_utils.dart new file mode 100644 index 000000000..296ecbdab --- /dev/null +++ b/lib/services/coins/bitcoincash/bch_utils.dart @@ -0,0 +1,29 @@ +import 'dart:typed_data'; + +import 'package:bitcoindart/src/utils/constants/op.dart' as op; +import 'package:bitcoindart/src/utils/script.dart' as bscript; + +abstract final class BchUtils { + static bool isSLP(Uint8List scriptPubKey) { + const id = [83, 76, 80, 0]; // 'SLP\x00' + final decompiled = bscript.decompile(scriptPubKey); + + if (decompiled != null && + decompiled.length > 1 && + decompiled.first == op.OPS["OP_RETURN"]) { + final _id = decompiled[1]; + + if (_id is List && _id.length == id.length) { + for (int i = 0; i < id.length; i++) { + if (_id[i] != id[i]) { + return false; + } + } + // lists match! + return true; + } + } + + return false; + } +} diff --git a/test/services/coins/bitcoincash/bch_utils_test.dart b/test/services/coins/bitcoincash/bch_utils_test.dart new file mode 100644 index 000000000..c25aee4fa --- /dev/null +++ b/test/services/coins/bitcoincash/bch_utils_test.dart @@ -0,0 +1,25 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:stackwallet/services/coins/bitcoincash/bch_utils.dart'; +import 'package:stackwallet/utilities/extensions/impl/string.dart'; + +void main() { + test("script pub key check for SLP is true", () { + expect( + BchUtils.isSLP( + "6a04534c500001010747454e45534953044d5430320f4d757461626c652054657374" + "2030321668747470733a2f2f46756c6c537461636b2e63617368200e9a18" + "8911ec0f2ac10cc9425b457d10ba14151a64eb4640f95ed7dab9e8f62601" + "004c00080000000000000001" + .toUint8ListFromHex), + true, + ); + }); + + test("script pub key check for SLP is not true", () { + expect( + BchUtils.isSLP("76a914a78bb9aa1b54c859b5fe72e6f6f576248b3231c888ac" + .toUint8ListFromHex), + false, + ); + }); +}