From 109ccc01462ce8dcd6c362921200b8874f0dda4c Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Fri, 4 Jul 2025 10:53:49 -0400 Subject: [PATCH] feat(option): add Option type for Varsig construction (initially for varsig < v1) --- option.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 option.go diff --git a/option.go b/option.go new file mode 100644 index 0000000..46d7861 --- /dev/null +++ b/option.go @@ -0,0 +1,45 @@ +package varsig + +// Options define customization when creating a new Varsig. +type Options struct { + forceVersion0 bool + signature []byte +} + +func newOptions(opts ...Option) *Options { + o := &Options{} + + for _, opt := range opts { + opt(o) + } + + return o +} + +// ForceVersion0 returns a boolean indicating that a Varsig < v1 should +// be created (which means the encoded Varsig won't have a version field +// and might contain the signature bytes as the last field.) +func (o *Options) ForceVersion0() bool { + return o.forceVersion0 +} + +// Signature returns the optional signature bytes when creating a Varsig +// < v1. +func (o *Options) Signature() []byte { + return o.signature +} + +// Option is a function that alters the default behavior of constructors +// that produce implementations of the Varsig type. +type Option func(*Options) + +// WithForceVersion0 indicates that a Varsig < v1 should be produced. If +// the signature is a) not nil, b) not empty and c) the correct length +// based on the signing algorithm or signing key, the signature's bytes +// will be appended to the encoded Varsig. +func WithForceVersion0(signature []byte) Option { + return func(o *Options) { + o.forceVersion0 = true + o.signature = signature + } +}