X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=c_sharp%2Fsrc%2Forg%2Fldk%2Fstructs%2FEntropySource.cs;h=f400924edd2dda1fc204f51b74b1ac681fb5da8b;hb=8de7213fbf663ff60322896282dad51e8ab2f001;hp=5a9cecf5bc03f013bb5a43dd7a8f564827ad0786;hpb=afc50e5d491a11364849383b75a8f939df703bac;p=ldk-java diff --git a/c_sharp/src/org/ldk/structs/EntropySource.cs b/c_sharp/src/org/ldk/structs/EntropySource.cs index 5a9cecf5..f400924e 100644 --- a/c_sharp/src/org/ldk/structs/EntropySource.cs +++ b/c_sharp/src/org/ldk/structs/EntropySource.cs @@ -1,3 +1,4 @@ + using org.ldk.impl; using org.ldk.enums; using org.ldk.util; @@ -5,52 +6,63 @@ using System; namespace org { namespace ldk { namespace structs { + + +/** An implementation of EntropySource */ +public interface EntropySourceInterface { + /**Gets a unique, cryptographically-secure, random 32-byte value. This method must return a + * different value each time it is called. + */ + byte[] get_secure_random_bytes(); +} + /** * A trait that describes a source of entropy. */ public class EntropySource : CommonBase { - internal readonly bindings.LDKEntropySource bindings_instance; + internal bindings.LDKEntropySource bindings_instance; + internal long instance_idx; + internal EntropySource(object _dummy, long ptr) : base(ptr) { bindings_instance = null; } - private EntropySource(bindings.LDKEntropySource arg) : base(bindings.LDKEntropySource_new(arg)) { - this.ptrs_to.AddLast(arg); - this.bindings_instance = arg; - } ~EntropySource() { if (ptr != 0) { bindings.EntropySource_free(ptr); } } - public interface EntropySourceInterface { - /** - * Gets a unique, cryptographically-secure, random 32-byte value. This method must return a - * different value each time it is called. - */ - byte[] get_secure_random_bytes(); - } private class LDKEntropySourceHolder { internal EntropySource held; } private class LDKEntropySourceImpl : bindings.LDKEntropySource { internal LDKEntropySourceImpl(EntropySourceInterface arg, LDKEntropySourceHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; } private EntropySourceInterface arg; private LDKEntropySourceHolder impl_holder; - public byte[] get_secure_random_bytes() { + public long get_secure_random_bytes() { byte[] ret = arg.get_secure_random_bytes(); GC.KeepAlive(arg); - byte[] result = InternalUtils.check_arr_len(ret, 32); + long result = InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(ret, 32)); return result; } } + + /** Creates a new instance of EntropySource from a given implementation */ public static EntropySource new_impl(EntropySourceInterface arg) { LDKEntropySourceHolder impl_holder = new LDKEntropySourceHolder(); - impl_holder.held = new EntropySource(new LDKEntropySourceImpl(arg, impl_holder)); + LDKEntropySourceImpl impl = new LDKEntropySourceImpl(arg, impl_holder); + long[] ptr_idx = bindings.LDKEntropySource_new(impl); + + impl_holder.held = new EntropySource(null, ptr_idx[0]); + impl_holder.held.instance_idx = ptr_idx[1]; + impl_holder.held.bindings_instance = impl; return impl_holder.held; } + /** * Gets a unique, cryptographically-secure, random 32-byte value. This method must return a * different value each time it is called. */ public byte[] get_secure_random_bytes() { - byte[] ret = bindings.EntropySource_get_secure_random_bytes(this.ptr); + long ret = bindings.EntropySource_get_secure_random_bytes(this.ptr); GC.KeepAlive(this); - return ret; + if (ret >= 0 && ret <= 4096) { return null; } + byte[] ret_conv = InternalUtils.decodeUint8Array(ret); + return ret_conv; } }