|
| 1 | +const parsePSBT = (psbtBase64) => { |
| 2 | + const psbtBuffer = Buffer.from(psbtBase64, 'base64'); |
| 3 | + let index = 0; |
| 4 | + |
| 5 | + // 魔术字节和分隔符 |
| 6 | + const magicBytes = psbtBuffer.slice(index, index + 5); |
| 7 | + index += 5; |
| 8 | + if (magicBytes.toString('hex') !== '70736274ff') { |
| 9 | + throw new Error('Not a valid PSBT'); |
| 10 | + } |
| 11 | + |
| 12 | + // 解析全局部分 |
| 13 | + console.log("Global Data:"); |
| 14 | + while (psbtBuffer[index] !== 0x00 && index < psbtBuffer.length) { |
| 15 | + |
| 16 | + if (index + 2 > psbtBuffer.length) break; // Prevent reading beyond buffer |
| 17 | + const keyLen = psbtBuffer[index]; |
| 18 | + const key = psbtBuffer.slice(index + 1, index + 1 + keyLen); |
| 19 | + index += 1 + keyLen; |
| 20 | + |
| 21 | + if (index + 1 > psbtBuffer.length) break; // Prevent reading beyond buffer |
| 22 | + const valueLen = psbtBuffer.readIntLE(index, 1); |
| 23 | + const value = psbtBuffer.slice(index + 1, index + 1 + valueLen); |
| 24 | + index += 1 + valueLen; |
| 25 | + |
| 26 | + console.log(`Key: ${key.toString('hex')}, Value: ${value.toString('hex')}`); |
| 27 | + } |
| 28 | + index++; // Skip separator |
| 29 | + |
| 30 | + // 解析输入部分 |
| 31 | + console.log("Inputs:"); |
| 32 | + while (index < psbtBuffer.length && psbtBuffer[index] !== 0x00) { |
| 33 | + while (psbtBuffer[index] !== 0x00 && index < psbtBuffer.length) { |
| 34 | + |
| 35 | + if (index + 2 > psbtBuffer.length) break; |
| 36 | + const keyLen = psbtBuffer[index]; |
| 37 | + const key = psbtBuffer.slice(index + 1, index + 1 + keyLen); |
| 38 | + index += 1 + keyLen; |
| 39 | + |
| 40 | + if (index + 1 > psbtBuffer.length) break; |
| 41 | + const valueLen = psbtBuffer.readUIntLE(index, 1); |
| 42 | + const value = psbtBuffer.slice(index + 1, index + 1 + valueLen); |
| 43 | + index += 1 + valueLen; |
| 44 | + console.log(`Key: ${key.toString('hex')}, Value: ${value.toString('hex')}`); |
| 45 | + } |
| 46 | + index++; // Skip separator |
| 47 | + } |
| 48 | + index++; // Skip separator |
| 49 | + |
| 50 | + // 解析输出部分 |
| 51 | + console.log("Outputs:"); |
| 52 | + while (index < psbtBuffer.length && psbtBuffer[index] !== 0x00) { |
| 53 | + console.log(`Output at index ${index}:`); |
| 54 | + while (psbtBuffer[index] !== 0x00 && index < psbtBuffer.length) { |
| 55 | + if (index + 2 > psbtBuffer.length) break; |
| 56 | + const keyLen = psbtBuffer[index]; |
| 57 | + const key = psbtBuffer.slice(index + 1, index + 1 + keyLen); |
| 58 | + index += 1 + keyLen; |
| 59 | + |
| 60 | + if (index + 1 > psbtBuffer.length) break; |
| 61 | + const valueLen = psbtBuffer.readUIntLE(index, 1); |
| 62 | + const value = psbtBuffer.slice(index + 1, index + 1 + valueLen); |
| 63 | + index += 1 + valueLen; |
| 64 | + |
| 65 | + console.log(`Key: ${key.toString('hex')}, Value: ${value.toString('hex')}`); |
| 66 | + } |
| 67 | + index++; // Skip separator |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | + |
| 72 | +// 用你提供的Base64字符串测试 |
| 73 | +const psbtBase64 = |
| 74 | + "cHNidP8BAHUCAAAAASaBcTce3/KF6Tet7qSze3gADAVmy7OtZGQXE8pCFxv2AAAAAAD+////AtPf9QUAAAAAGXapFNDFmQPFusKGh2DpD9UhpGZap2UgiKwA4fUFAAAAABepFDVF5uM7gyxHBQ8k0+65PJwDlIvHh7MuEwAAAQD9pQEBAAAAAAECiaPHHqtNIOA3G7ukzGmPopXJRjr6Ljl/hTPMti+VZ+UBAAAAFxYAFL4Y0VKpsBIDna89p95PUzSe7LmF/////4b4qkOnHf8USIk6UwpyN+9rRgi7st0tAXHmOuxqSJC0AQAAABcWABT+Pp7xp0XpdNkCxDVZQ6vLNL1TU/////8CAMLrCwAAAAAZdqkUhc/xCX/Z4Ai7NK9wnGIZeziXikiIrHL++E4sAAAAF6kUM5cluiHv1irHU6m80GfWx6ajnQWHAkcwRAIgJxK+IuAnDzlPVoMR3HyppolwuAJf3TskAinwf4pfOiQCIAGLONfc0xTnNMkna9b7QPZzMlvEuqFEyADS8vAtsnZcASED0uFWdJQbrUqZY3LLh+GFbTZSYG2YVi/jnF6efkE/IQUCSDBFAiEA0SuFLYXc2WHS9fSrZgZU327tzHlMDDPOXMMJ/7X85Y0CIGczio4OFyXBl/saiK9Z9R5E5CVbIBZ8hoQDHAXR8lkqASECI7cr7vCWXRC+B3jv7NYfysb3mk6haTkzgHNEZPhPKrMAAAAAAAAA"; |
| 75 | +parsePSBT(psbtBase64); |
0 commit comments