public byte[] UntrustedHashSign(KeyPath keyPath, UserPin pin, LockTime lockTime, SigHash sigHashType) { MemoryStream data = new MemoryStream(); byte[] path = Serializer.Serialize(keyPath); BufferUtils.WriteBuffer(data, path); var pinBytes = pin == null ? new byte[0] : pin.ToBytes(); data.WriteByte((byte)pinBytes.Length); BufferUtils.WriteBuffer(data, pinBytes); BufferUtils.WriteUint32BE(data, (uint)lockTime); data.WriteByte((byte)sigHashType); return(CreateAPDU(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_HASH_SIGN, (byte)0x00, (byte)0x00, data.ToArray())); }
public async Task <TrustedInput> GetTrustedInputAsync(Transaction transaction, int outputIndex, CancellationToken cancellation = default(CancellationToken)) { if (outputIndex >= transaction.Outputs.Count) { throw new ArgumentOutOfRangeException("outputIndex is bigger than the number of outputs in the transaction", "outputIndex"); } List <byte[]> apdus = new List <byte[]>(); MemoryStream data = new MemoryStream(); // Header BufferUtils.WriteUint32BE(data, outputIndex); BufferUtils.WriteBuffer(data, transaction.Version); VarintUtils.write(data, transaction.Inputs.Count); apdus.Add(CreateAPDU(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, (byte)0x00, (byte)0x00, data.ToArray())); // Each input foreach (var input in transaction.Inputs) { data = new MemoryStream(); BufferUtils.WriteBuffer(data, input.PrevOut); VarintUtils.write(data, input.ScriptSig.Length); apdus.Add(CreateAPDU(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.ToArray())); data = new MemoryStream(); BufferUtils.WriteBuffer(data, input.ScriptSig.ToBytes()); apdus.AddRange(CreateApduSplit2(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.ToArray(), Utils.ToBytes(input.Sequence, true))); } // Number of outputs data = new MemoryStream(); VarintUtils.write(data, transaction.Outputs.Count); apdus.Add(CreateAPDU(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.ToArray())); // Each output foreach (var output in transaction.Outputs) { data = new MemoryStream(); BufferUtils.WriteBuffer(data, Utils.ToBytes((ulong)output.Value.Satoshi, true)); VarintUtils.write(data, output.ScriptPubKey.Length); apdus.Add(CreateAPDU(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.ToArray())); data = new MemoryStream(); BufferUtils.WriteBuffer(data, output.ScriptPubKey.ToBytes()); apdus.AddRange(CreateAPDUSplit(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, data.ToArray())); } // Locktime apdus.Add(CreateAPDU(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, (byte)0x80, (byte)0x00, transaction.LockTime.ToBytes())); byte[] response = await ExchangeApdusAsync(apdus.ToArray(), OK, cancellation).ConfigureAwait(false); return(new TrustedInput(response)); }
private List <byte[]> GetTrustedInputAPDUs(Transaction transaction, int outputIndex) { if (outputIndex >= transaction.Outputs.Count) { throw new ArgumentOutOfRangeException("outputIndex is bigger than the number of outputs in the transaction", "outputIndex"); } var apdus = new List <byte[]>(); var data = new MemoryStream(); // Header BufferUtils.WriteUint32BE(data, outputIndex); BufferUtils.WriteBuffer(data, transaction.Version); VarintUtils.write(data, transaction.Inputs.Count); apdus.Add(CreateAPDU(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, 0x00, 0x00, data.ToArray())); // Each input foreach (var input in transaction.Inputs) { data = new MemoryStream(); BufferUtils.WriteBuffer(data, input.PrevOut); VarintUtils.write(data, input.ScriptSig.Length); apdus.Add(CreateAPDU(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, 0x80, 0x00, data.ToArray())); data = new MemoryStream(); BufferUtils.WriteBuffer(data, input.ScriptSig.ToBytes()); apdus.AddRange(CreateApduSplit2(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, 0x80, 0x00, data.ToArray(), Utils.ToBytes(input.Sequence, true))); } // Number of outputs data = new MemoryStream(); VarintUtils.write(data, transaction.Outputs.Count); apdus.Add(CreateAPDU(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, 0x80, 0x00, data.ToArray())); // Each output foreach (var output in transaction.Outputs) { data = new MemoryStream(); BufferUtils.WriteBuffer(data, Utils.ToBytes((ulong)output.Value.Satoshi, true)); VarintUtils.write(data, output.ScriptPubKey.Length); apdus.Add(CreateAPDU(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, 0x80, 0x00, data.ToArray())); data = new MemoryStream(); BufferUtils.WriteBuffer(data, output.ScriptPubKey.ToBytes()); apdus.AddRange(CreateAPDUSplit(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, 0x80, 0x00, data.ToArray())); } // Locktime apdus.Add(CreateAPDU(LedgerWalletConstants.LedgerWallet_CLA, LedgerWalletConstants.LedgerWallet_INS_GET_TRUSTED_INPUT, 0x80, 0x00, transaction.LockTime.ToBytes())); return(apdus); }