|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:crypto/crypto.dart'; |
| 5 | + |
| 6 | +const _archiveUrl = |
| 7 | + 'https://pub.dev/api/archives/payjoin-0.2.1%2Bpayjoin-1.0.0-rc.8.tar.gz'; |
| 8 | +const _archiveSha256 = |
| 9 | + '52696fe34ef3c05f9827f5dd08a984e8ea86642677ba934f65a7f3a722755985'; |
| 10 | +const _bindingSha256 = |
| 11 | + 'aee74c23bf5076c5db8dda79c9ac1866446d3c4e237dbafeddd52c3efe499500'; |
| 12 | + |
| 13 | +Future<void> main() async { |
| 14 | + Directory? temporaryDirectory; |
| 15 | + File? temporaryBinding; |
| 16 | + |
| 17 | + try { |
| 18 | + final packageRoot = _findPayjoinRoot(); |
| 19 | + final binding = File('${packageRoot.path}/lib/payjoin.dart'); |
| 20 | + |
| 21 | + if (binding.existsSync()) { |
| 22 | + final actualHash = await _sha256(binding); |
| 23 | + if (actualHash != _bindingSha256) { |
| 24 | + throw StateError( |
| 25 | + 'Existing payjoin binding has SHA-256 $actualHash, expected ' |
| 26 | + '$_bindingSha256.', |
| 27 | + ); |
| 28 | + } |
| 29 | + stdout.writeln('Validated existing payjoin binding: ${binding.path}'); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + stdout.writeln('Payjoin binding is missing; hydrating ${binding.path}'); |
| 34 | + temporaryDirectory = await Directory.systemTemp.createTemp('payjoin-'); |
| 35 | + final archive = File('${temporaryDirectory.path}/payjoin.tar.gz'); |
| 36 | + await _downloadArchive(archive); |
| 37 | + final archiveHash = await _sha256(archive); |
| 38 | + if (archiveHash != _archiveSha256) { |
| 39 | + throw StateError( |
| 40 | + 'Downloaded payjoin archive has SHA-256 $archiveHash, expected ' |
| 41 | + '$_archiveSha256.', |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + final extractionDirectory = await Directory( |
| 46 | + '${temporaryDirectory.path}/extract', |
| 47 | + ).create(); |
| 48 | + final tarResult = await Process.run('tar', [ |
| 49 | + '-xzf', |
| 50 | + archive.path, |
| 51 | + '-C', |
| 52 | + extractionDirectory.path, |
| 53 | + '--strip-components=1', |
| 54 | + 'lib/payjoin.dart', |
| 55 | + ]); |
| 56 | + if (tarResult.exitCode != 0) { |
| 57 | + throw StateError( |
| 58 | + 'Could not extract lib/payjoin.dart from the verified payjoin archive: ' |
| 59 | + '${tarResult.stderr}' |
| 60 | + .trim(), |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + final extractedBinding = File('${extractionDirectory.path}/payjoin.dart'); |
| 65 | + if (!extractedBinding.existsSync()) { |
| 66 | + throw StateError( |
| 67 | + 'Verified payjoin archive did not contain lib/payjoin.dart.', |
| 68 | + ); |
| 69 | + } |
| 70 | + final bindingHash = await _sha256(extractedBinding); |
| 71 | + if (bindingHash != _bindingSha256) { |
| 72 | + throw StateError( |
| 73 | + 'Extracted payjoin binding has SHA-256 $bindingHash, expected ' |
| 74 | + '$_bindingSha256.', |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + temporaryBinding = File('${binding.path}.tmp'); |
| 79 | + if (temporaryBinding.existsSync()) { |
| 80 | + throw StateError( |
| 81 | + 'Refusing to use an existing temporary binding: ${temporaryBinding.path}', |
| 82 | + ); |
| 83 | + } |
| 84 | + await extractedBinding.copy(temporaryBinding.path); |
| 85 | + await temporaryBinding.rename(binding.path); |
| 86 | + temporaryBinding = null; |
| 87 | + stdout.writeln('Installed verified payjoin binding: ${binding.path}'); |
| 88 | + } catch (error) { |
| 89 | + stderr.writeln('Failed to prepare payjoin dependency: $error'); |
| 90 | + exitCode = 1; |
| 91 | + } finally { |
| 92 | + if (temporaryBinding != null && temporaryBinding.existsSync()) { |
| 93 | + temporaryBinding.deleteSync(); |
| 94 | + } |
| 95 | + temporaryDirectory?.deleteSync(recursive: true); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +Directory _findPayjoinRoot() { |
| 100 | + final configFile = File('.dart_tool/package_config.json'); |
| 101 | + if (!configFile.existsSync()) { |
| 102 | + throw StateError( |
| 103 | + 'Missing .dart_tool/package_config.json; run pub get first.', |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + final config = jsonDecode(configFile.readAsStringSync()); |
| 108 | + final packages = config is Map<String, dynamic> ? config['packages'] : null; |
| 109 | + if (packages is! List) { |
| 110 | + throw StateError( |
| 111 | + 'Invalid .dart_tool/package_config.json: packages is missing.', |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + for (final package in packages) { |
| 116 | + if (package is! Map<String, dynamic> || package['name'] != 'payjoin') { |
| 117 | + continue; |
| 118 | + } |
| 119 | + final rootUri = package['rootUri']; |
| 120 | + if (rootUri is! String) { |
| 121 | + throw StateError( |
| 122 | + 'Invalid payjoin package configuration: rootUri is missing.', |
| 123 | + ); |
| 124 | + } |
| 125 | + final configUri = configFile.absolute.uri; |
| 126 | + final root = Uri.parse(rootUri).isAbsolute |
| 127 | + ? Uri.parse(rootUri) |
| 128 | + : configUri.resolve(rootUri); |
| 129 | + return Directory.fromUri(root); |
| 130 | + } |
| 131 | + |
| 132 | + throw StateError('Package "payjoin" was not found in package_config.json.'); |
| 133 | +} |
| 134 | + |
| 135 | +Future<void> _downloadArchive(File destination) async { |
| 136 | + final client = HttpClient(); |
| 137 | + try { |
| 138 | + final request = await client.getUrl(Uri.parse(_archiveUrl)); |
| 139 | + request.followRedirects = true; |
| 140 | + request.maxRedirects = 5; |
| 141 | + final response = await request.close(); |
| 142 | + if (response.statusCode != HttpStatus.ok) { |
| 143 | + throw StateError( |
| 144 | + 'pub.dev returned HTTP ${response.statusCode} for the payjoin archive.', |
| 145 | + ); |
| 146 | + } |
| 147 | + await response.pipe(destination.openWrite()); |
| 148 | + } finally { |
| 149 | + client.close(force: true); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +Future<String> _sha256(File file) async => |
| 154 | + sha256.convert(await file.readAsBytes()).toString(); |
0 commit comments