diff --git a/bridge/call.go b/bridge/call.go index ece4832..e19665d 100644 --- a/bridge/call.go +++ b/bridge/call.go @@ -55,6 +55,12 @@ func Call(name string, payload []byte) ([]byte, error) { output = instance.generate(payload) case "armorEncode": output = instance.armorEncode(payload) + case "getPublicKeyMetadata": + output = instance.getPublicKeyMetadata(payload) + case "getPrivateKeyMetadata": + output = instance.getPrivateKeyMetadata(payload) + case "convertPrivateKeyToPublicKey": + output = instance.convertPrivateKeyToPublicKey(payload) default: return nil, fmt.Errorf("not implemented: %s", name) } @@ -75,10 +81,7 @@ func (m instance) decrypt(payload []byte) []byte { request := model.GetRootAsDecryptRequest(payload, 0) output, err := m.instance.Decrypt(m.toString(request.Message()), m.toString(request.PrivateKey()), m.toString(request.Passphrase()), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._stringResponse(response, output, err) - } - return m._stringResponse(response, output, nil) + return m._stringResponse(response, output, err) } func (m instance) decryptFile(payload []byte) []byte { @@ -86,10 +89,7 @@ func (m instance) decryptFile(payload []byte) []byte { request := model.GetRootAsDecryptFileRequest(payload, 0) output, err := m.instance.DecryptFile(m.toString(request.Input()), m.toString(request.Output()), m.toString(request.PrivateKey()), m.toString(request.Passphrase()), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._intResponse(response, int64(output), err) - } - return m._intResponse(response, int64(output), nil) + return m._intResponse(response, int64(output), err) } func (m instance) decryptBytes(payload []byte) []byte { @@ -97,10 +97,7 @@ func (m instance) decryptBytes(payload []byte) []byte { request := model.GetRootAsDecryptBytesRequest(payload, 0) output, err := m.instance.DecryptBytes(request.MessageBytes(), m.toString(request.PrivateKey()), m.toString(request.Passphrase()), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._bytesResponse(response, output, err) - } - return m._bytesResponse(response, output, nil) + return m._bytesResponse(response, output, err) } func (m instance) encrypt(payload []byte) []byte { @@ -108,10 +105,7 @@ func (m instance) encrypt(payload []byte) []byte { request := model.GetRootAsEncryptRequest(payload, 0) output, err := m.instance.Encrypt(m.toString(request.Message()), m.toString(request.PublicKey()), m.parseEntity(request.Signed(nil)), m.parseFileHints(request.FileHints(nil)), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._stringResponse(response, output, err) - } - return m._stringResponse(response, output, nil) + return m._stringResponse(response, output, err) } func (m instance) encryptFile(payload []byte) []byte { @@ -119,10 +113,7 @@ func (m instance) encryptFile(payload []byte) []byte { request := model.GetRootAsEncryptFileRequest(payload, 0) output, err := m.instance.EncryptFile(m.toString(request.Input()), m.toString(request.Output()), m.toString(request.PublicKey()), m.parseEntity(request.Signed(nil)), m.parseFileHints(request.FileHints(nil)), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._intResponse(response, int64(output), err) - } - return m._intResponse(response, int64(output), nil) + return m._intResponse(response, int64(output), err) } func (m instance) encryptBytes(payload []byte) []byte { @@ -130,10 +121,7 @@ func (m instance) encryptBytes(payload []byte) []byte { request := model.GetRootAsEncryptBytesRequest(payload, 0) output, err := m.instance.EncryptBytes(request.MessageBytes(), m.toString(request.PublicKey()), m.parseEntity(request.Signed(nil)), m.parseFileHints(request.FileHints(nil)), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._bytesResponse(response, output, err) - } - return m._bytesResponse(response, output, nil) + return m._bytesResponse(response, output, err) } func (m instance) sign(payload []byte) []byte { @@ -141,130 +129,91 @@ func (m instance) sign(payload []byte) []byte { request := model.GetRootAsSignRequest(payload, 0) output, err := m.instance.Sign(m.toString(request.Message()), m.toString(request.PublicKey()), m.toString(request.PrivateKey()), m.toString(request.Passphrase()), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._stringResponse(response, output, err) - } - return m._stringResponse(response, output, nil) + return m._stringResponse(response, output, err) } func (m instance) signFile(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsSignFileRequest(payload, 0) output, err := m.instance.SignFile(m.toString(request.Input()), m.toString(request.PublicKey()), m.toString(request.PrivateKey()), m.toString(request.Passphrase()), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._stringResponse(response, output, err) - } - return m._stringResponse(response, output, nil) + return m._stringResponse(response, output, err) } func (m instance) signBytes(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsSignBytesRequest(payload, 0) output, err := m.instance.SignBytes(request.MessageBytes(), m.toString(request.PublicKey()), m.toString(request.PrivateKey()), m.toString(request.Passphrase()), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._bytesResponse(response, output, err) - } - return m._bytesResponse(response, output, nil) + return m._bytesResponse(response, output, err) } func (m instance) signBytesToString(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsSignBytesRequest(payload, 0) output, err := m.instance.SignBytesToString(request.MessageBytes(), m.toString(request.PublicKey()), m.toString(request.PrivateKey()), m.toString(request.Passphrase()), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._stringResponse(response, output, err) - } - return m._stringResponse(response, output, nil) + return m._stringResponse(response, output, err) } func (m instance) verify(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsVerifyRequest(payload, 0) output, err := m.instance.Verify(m.toString(request.Signature()), m.toString(request.Message()), m.toString(request.PublicKey())) - if err != nil { - return m._boolResponse(response, output, err) - } - return m._boolResponse(response, output, nil) + return m._boolResponse(response, output, err) } func (m instance) verifyFile(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsVerifyFileRequest(payload, 0) output, err := m.instance.VerifyFile(m.toString(request.Signature()), m.toString(request.Input()), m.toString(request.PublicKey())) - if err != nil { - return m._boolResponse(response, output, err) - } - return m._boolResponse(response, output, nil) + return m._boolResponse(response, output, err) } func (m instance) verifyBytes(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsVerifyBytesRequest(payload, 0) output, err := m.instance.VerifyBytes(m.toString(request.Signature()), request.MessageBytes(), m.toString(request.PublicKey())) - if err != nil { - return m._boolResponse(response, output, err) - } - return m._boolResponse(response, output, nil) + return m._boolResponse(response, output, err) } func (m instance) decryptSymmetric(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsDecryptSymmetricRequest(payload, 0) output, err := m.instance.DecryptSymmetric(m.toString(request.Message()), m.toString(request.Passphrase()), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._stringResponse(response, output, err) - } - return m._stringResponse(response, output, nil) + return m._stringResponse(response, output, err) } func (m instance) decryptSymmetricFile(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsDecryptSymmetricFileRequest(payload, 0) output, err := m.instance.DecryptSymmetricFile(m.toString(request.Input()), m.toString(request.Output()), m.toString(request.Passphrase()), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._intResponse(response, int64(output), err) - } - return m._intResponse(response, int64(output), nil) + return m._intResponse(response, int64(output), err) } func (m instance) decryptSymmetricBytes(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsDecryptSymmetricBytesRequest(payload, 0) output, err := m.instance.DecryptSymmetricBytes(request.MessageBytes(), m.toString(request.Passphrase()), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._bytesResponse(response, output, err) - } - return m._bytesResponse(response, output, nil) + return m._bytesResponse(response, output, err) } func (m instance) encryptSymmetric(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsEncryptSymmetricRequest(payload, 0) output, err := m.instance.EncryptSymmetric(m.toString(request.Message()), m.toString(request.Passphrase()), m.parseFileHints(request.FileHints(nil)), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._stringResponse(response, output, err) - } - return m._stringResponse(response, output, nil) + return m._stringResponse(response, output, err) } func (m instance) encryptSymmetricFile(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsEncryptSymmetricFileRequest(payload, 0) output, err := m.instance.EncryptSymmetricFile(m.toString(request.Input()), m.toString(request.Output()), m.toString(request.Passphrase()), m.parseFileHints(request.FileHints(nil)), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._intResponse(response, int64(output), err) - } - return m._intResponse(response, int64(output), nil) + return m._intResponse(response, int64(output), err) } func (m instance) encryptSymmetricBytes(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsEncryptSymmetricBytesRequest(payload, 0) output, err := m.instance.EncryptSymmetricBytes(request.MessageBytes(), m.toString(request.Passphrase()), m.parseFileHints(request.FileHints(nil)), m.parseKeyOptions(request.Options(nil))) - if err != nil { - return m._bytesResponse(response, output, err) - } - return m._bytesResponse(response, output, nil) + return m._bytesResponse(response, output, err) } func (m instance) generate(payload []byte) []byte { response := flatbuffers.NewBuilder(0) @@ -272,10 +221,39 @@ func (m instance) generate(payload []byte) []byte { options := m.parseOptions(request.Options(nil)) output, err := m.instance.Generate(options) - if err != nil { - return m._keyPairResponse(response, output, err) - } - return m._keyPairResponse(response, output, nil) + return m._keyPairResponse(response, output, err) +} + +func (m instance) armorEncode(payload []byte) []byte { + response := flatbuffers.NewBuilder(0) + request := model.GetRootAsArmorEncodeRequest(payload, 0) + + output, err := m.instance.ArmorEncode(request.PacketBytes()) + return m._stringResponse(response, output, err) +} + +func (m instance) getPublicKeyMetadata(payload []byte) []byte { + response := flatbuffers.NewBuilder(0) + request := model.GetRootAsGetPublicKeyMetadataRequest(payload, 0) + + output, err := m.instance.GetPublicKeyMetadata(m.toString(request.PublicKey())) + return m._publicKeyMetadataResponse(response, output, err) +} + +func (m instance) getPrivateKeyMetadata(payload []byte) []byte { + response := flatbuffers.NewBuilder(0) + request := model.GetRootAsGetPrivateKeyMetadataRequest(payload, 0) + + output, err := m.instance.GetPrivateKeyMetadata(m.toString(request.PrivateKey())) + return m._privateKeyMetadataResponse(response, output, err) +} + +func (m instance) convertPrivateKeyToPublicKey(payload []byte) []byte { + response := flatbuffers.NewBuilder(0) + request := model.GetRootAsConvertPrivateKeyToPublicKeyRequest(payload, 0) + + output, err := m.instance.ConvertPrivateKeyToPublicKey(m.toString(request.PrivateKey())) + return m._stringResponse(response, output, err) } func (m instance) parseOptions(input *model.Options) *openpgp.Options { @@ -376,17 +354,6 @@ func (m instance) parseCompression(input model.Compression) string { } } -func (m instance) armorEncode(payload []byte) []byte { - response := flatbuffers.NewBuilder(0) - request := model.GetRootAsArmorEncodeRequest(payload, 0) - - output, err := m.instance.ArmorEncode(request.PacketBytes()) - if err != nil { - return m._stringResponse(response, output, err) - } - return m._stringResponse(response, output, nil) -} - func (m instance) _keyPairResponse(response *flatbuffers.Builder, output *openpgp.KeyPair, err error) []byte { if err != nil { outputOffset := response.CreateString(err.Error()) @@ -410,6 +377,67 @@ func (m instance) _keyPairResponse(response *flatbuffers.Builder, output *openpg return response.FinishedBytes() } +func (m instance) _publicKeyMetadataResponse(response *flatbuffers.Builder, output *openpgp.PublicKeyMetadata, err error) []byte { + if err != nil { + outputOffset := response.CreateString(err.Error()) + model.PublicKeyMetadataResponseStart(response) + model.PublicKeyMetadataResponseAddError(response, outputOffset) + response.Finish(model.PublicKeyMetadataResponseEnd(response)) + return response.FinishedBytes() + } + + keyIDOffset := response.CreateString(output.KeyID) + keyIDShortOffset := response.CreateString(output.KeyIDShort) + creationTimeOffset := response.CreateString(output.CreationTime) + fingerprintOffset := response.CreateString(output.Fingerprint) + keyIDNumericOffset := response.CreateString(output.KeyIDNumeric) + + model.PublicKeyMetadataStart(response) + model.PublicKeyMetadataAddKeyId(response, keyIDOffset) + model.PublicKeyMetadataAddKeyIdShort(response, keyIDShortOffset) + model.PublicKeyMetadataAddCreationTime(response, creationTimeOffset) + model.PublicKeyMetadataAddFingerprint(response, fingerprintOffset) + model.PublicKeyMetadataAddKeyIdNumeric(response, keyIDNumericOffset) + model.PublicKeyMetadataAddIsSubKey(response, output.IsSubKey) + KeyPair := model.PublicKeyMetadataEnd(response) + + model.PublicKeyMetadataResponseStart(response) + model.PublicKeyMetadataResponseAddOutput(response, KeyPair) + response.Finish(model.PublicKeyMetadataResponseEnd(response)) + return response.FinishedBytes() +} + +func (m instance) _privateKeyMetadataResponse(response *flatbuffers.Builder, output *openpgp.PrivateKeyMetadata, err error) []byte { + if err != nil { + outputOffset := response.CreateString(err.Error()) + model.PrivateKeyMetadataResponseStart(response) + model.PrivateKeyMetadataResponseAddError(response, outputOffset) + response.Finish(model.PrivateKeyMetadataResponseEnd(response)) + return response.FinishedBytes() + } + + keyIDOffset := response.CreateString(output.KeyID) + keyIDShortOffset := response.CreateString(output.KeyIDShort) + creationTimeOffset := response.CreateString(output.CreationTime) + fingerprintOffset := response.CreateString(output.Fingerprint) + keyIDNumericOffset := response.CreateString(output.KeyIDNumeric) + + model.PrivateKeyMetadataStart(response) + model.PrivateKeyMetadataAddKeyId(response, keyIDOffset) + model.PrivateKeyMetadataAddKeyIdShort(response, keyIDShortOffset) + model.PrivateKeyMetadataAddCreationTime(response, creationTimeOffset) + model.PrivateKeyMetadataAddFingerprint(response, fingerprintOffset) + model.PrivateKeyMetadataAddKeyIdNumeric(response, keyIDNumericOffset) + model.PrivateKeyMetadataAddIsSubKey(response, output.IsSubKey) + model.PrivateKeyMetadataAddEncrypted(response, output.Encrypted) + KeyPair := model.PrivateKeyMetadataEnd(response) + + model.PrivateKeyMetadataResponseStart(response) + model.PrivateKeyMetadataResponseAddOutput(response, KeyPair) + response.Finish(model.PrivateKeyMetadataResponseEnd(response)) + return response.FinishedBytes() +} + func (m instance) _boolResponse(response *flatbuffers.Builder, output bool, err error) []byte { if err != nil { outputOffset := response.CreateString(err.Error()) diff --git a/bridge/model/ArmorDecodeRequest.go b/bridge/model/ArmorDecodeRequest.go deleted file mode 100644 index 4a86f21..0000000 --- a/bridge/model/ArmorDecodeRequest.go +++ /dev/null @@ -1,52 +0,0 @@ -// Code generated by the FlatBuffers compiler. DO NOT EDIT. - -package model - -import ( - flatbuffers "github.com/google/flatbuffers/go" -) - -type ArmorDecodeRequest struct { - _tab flatbuffers.Table -} - -func GetRootAsArmorDecodeRequest(buf []byte, offset flatbuffers.UOffsetT) *ArmorDecodeRequest { - n := flatbuffers.GetUOffsetT(buf[offset:]) - x := &ArmorDecodeRequest{} - x.Init(buf, n+offset) - return x -} - -func GetSizePrefixedRootAsArmorDecodeRequest(buf []byte, offset flatbuffers.UOffsetT) *ArmorDecodeRequest { - n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:]) - x := &ArmorDecodeRequest{} - x.Init(buf, n+offset+flatbuffers.SizeUint32) - return x -} - -func (rcv *ArmorDecodeRequest) Init(buf []byte, i flatbuffers.UOffsetT) { - rcv._tab.Bytes = buf - rcv._tab.Pos = i -} - -func (rcv *ArmorDecodeRequest) Table() flatbuffers.Table { - return rcv._tab -} - -func (rcv *ArmorDecodeRequest) Packet() []byte { - o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) - if o != 0 { - return rcv._tab.ByteVector(o + rcv._tab.Pos) - } - return nil -} - -func ArmorDecodeRequestStart(builder *flatbuffers.Builder) { - builder.StartObject(1) -} -func ArmorDecodeRequestAddPacket(builder *flatbuffers.Builder, packet flatbuffers.UOffsetT) { - builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(packet), 0) -} -func ArmorDecodeRequestEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { - return builder.EndObject() -} diff --git a/bridge/model/ConvertPrivateKeyToPublicKeyRequest.go b/bridge/model/ConvertPrivateKeyToPublicKeyRequest.go new file mode 100644 index 0000000..53c9781 --- /dev/null +++ b/bridge/model/ConvertPrivateKeyToPublicKeyRequest.go @@ -0,0 +1,52 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package model + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) + +type ConvertPrivateKeyToPublicKeyRequest struct { + _tab flatbuffers.Table +} + +func GetRootAsConvertPrivateKeyToPublicKeyRequest(buf []byte, offset flatbuffers.UOffsetT) *ConvertPrivateKeyToPublicKeyRequest { + n := flatbuffers.GetUOffsetT(buf[offset:]) + x := &ConvertPrivateKeyToPublicKeyRequest{} + x.Init(buf, n+offset) + return x +} + +func GetSizePrefixedRootAsConvertPrivateKeyToPublicKeyRequest(buf []byte, offset flatbuffers.UOffsetT) *ConvertPrivateKeyToPublicKeyRequest { + n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:]) + x := &ConvertPrivateKeyToPublicKeyRequest{} + x.Init(buf, n+offset+flatbuffers.SizeUint32) + return x +} + +func (rcv *ConvertPrivateKeyToPublicKeyRequest) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *ConvertPrivateKeyToPublicKeyRequest) Table() flatbuffers.Table { + return rcv._tab +} + +func (rcv *ConvertPrivateKeyToPublicKeyRequest) PrivateKey() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + +func ConvertPrivateKeyToPublicKeyRequestStart(builder *flatbuffers.Builder) { + builder.StartObject(1) +} +func ConvertPrivateKeyToPublicKeyRequestAddPrivateKey(builder *flatbuffers.Builder, privateKey flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(privateKey), 0) +} +func ConvertPrivateKeyToPublicKeyRequestEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/bridge/model/GetPrivateKeyMetadataRequest.go b/bridge/model/GetPrivateKeyMetadataRequest.go new file mode 100644 index 0000000..253e8dd --- /dev/null +++ b/bridge/model/GetPrivateKeyMetadataRequest.go @@ -0,0 +1,52 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package model + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) + +type GetPrivateKeyMetadataRequest struct { + _tab flatbuffers.Table +} + +func GetRootAsGetPrivateKeyMetadataRequest(buf []byte, offset flatbuffers.UOffsetT) *GetPrivateKeyMetadataRequest { + n := flatbuffers.GetUOffsetT(buf[offset:]) + x := &GetPrivateKeyMetadataRequest{} + x.Init(buf, n+offset) + return x +} + +func GetSizePrefixedRootAsGetPrivateKeyMetadataRequest(buf []byte, offset flatbuffers.UOffsetT) *GetPrivateKeyMetadataRequest { + n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:]) + x := &GetPrivateKeyMetadataRequest{} + x.Init(buf, n+offset+flatbuffers.SizeUint32) + return x +} + +func (rcv *GetPrivateKeyMetadataRequest) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *GetPrivateKeyMetadataRequest) Table() flatbuffers.Table { + return rcv._tab +} + +func (rcv *GetPrivateKeyMetadataRequest) PrivateKey() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + +func GetPrivateKeyMetadataRequestStart(builder *flatbuffers.Builder) { + builder.StartObject(1) +} +func GetPrivateKeyMetadataRequestAddPrivateKey(builder *flatbuffers.Builder, privateKey flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(privateKey), 0) +} +func GetPrivateKeyMetadataRequestEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/bridge/model/GetPublicKeyMetadataRequest.go b/bridge/model/GetPublicKeyMetadataRequest.go new file mode 100644 index 0000000..96c6aac --- /dev/null +++ b/bridge/model/GetPublicKeyMetadataRequest.go @@ -0,0 +1,52 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package model + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) + +type GetPublicKeyMetadataRequest struct { + _tab flatbuffers.Table +} + +func GetRootAsGetPublicKeyMetadataRequest(buf []byte, offset flatbuffers.UOffsetT) *GetPublicKeyMetadataRequest { + n := flatbuffers.GetUOffsetT(buf[offset:]) + x := &GetPublicKeyMetadataRequest{} + x.Init(buf, n+offset) + return x +} + +func GetSizePrefixedRootAsGetPublicKeyMetadataRequest(buf []byte, offset flatbuffers.UOffsetT) *GetPublicKeyMetadataRequest { + n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:]) + x := &GetPublicKeyMetadataRequest{} + x.Init(buf, n+offset+flatbuffers.SizeUint32) + return x +} + +func (rcv *GetPublicKeyMetadataRequest) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *GetPublicKeyMetadataRequest) Table() flatbuffers.Table { + return rcv._tab +} + +func (rcv *GetPublicKeyMetadataRequest) PublicKey() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + +func GetPublicKeyMetadataRequestStart(builder *flatbuffers.Builder) { + builder.StartObject(1) +} +func GetPublicKeyMetadataRequestAddPublicKey(builder *flatbuffers.Builder, publicKey flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(publicKey), 0) +} +func GetPublicKeyMetadataRequestEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/bridge/model/PrivateKeyMetadataResponse.go b/bridge/model/PrivateKeyMetadataResponse.go new file mode 100644 index 0000000..cfcb49d --- /dev/null +++ b/bridge/model/PrivateKeyMetadataResponse.go @@ -0,0 +1,68 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package model + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) + +type PrivateKeyMetadataResponse struct { + _tab flatbuffers.Table +} + +func GetRootAsPrivateKeyMetadataResponse(buf []byte, offset flatbuffers.UOffsetT) *PrivateKeyMetadataResponse { + n := flatbuffers.GetUOffsetT(buf[offset:]) + x := &PrivateKeyMetadataResponse{} + x.Init(buf, n+offset) + return x +} + +func GetSizePrefixedRootAsPrivateKeyMetadataResponse(buf []byte, offset flatbuffers.UOffsetT) *PrivateKeyMetadataResponse { + n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:]) + x := &PrivateKeyMetadataResponse{} + x.Init(buf, n+offset+flatbuffers.SizeUint32) + return x +} + +func (rcv *PrivateKeyMetadataResponse) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *PrivateKeyMetadataResponse) Table() flatbuffers.Table { + return rcv._tab +} + +func (rcv *PrivateKeyMetadataResponse) Output(obj *PrivateKeyMetadata) *PrivateKeyMetadata { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + x := rcv._tab.Indirect(o + rcv._tab.Pos) + if obj == nil { + obj = new(PrivateKeyMetadata) + } + obj.Init(rcv._tab.Bytes, x) + return obj + } + return nil +} + +func (rcv *PrivateKeyMetadataResponse) Error() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + +func PrivateKeyMetadataResponseStart(builder *flatbuffers.Builder) { + builder.StartObject(2) +} +func PrivateKeyMetadataResponseAddOutput(builder *flatbuffers.Builder, output flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(output), 0) +} +func PrivateKeyMetadataResponseAddError(builder *flatbuffers.Builder, error flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(error), 0) +} +func PrivateKeyMetadataResponseEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/bridge/model/PublicKeyMetadataResponse.go b/bridge/model/PublicKeyMetadataResponse.go new file mode 100644 index 0000000..8372254 --- /dev/null +++ b/bridge/model/PublicKeyMetadataResponse.go @@ -0,0 +1,68 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package model + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) + +type PublicKeyMetadataResponse struct { + _tab flatbuffers.Table +} + +func GetRootAsPublicKeyMetadataResponse(buf []byte, offset flatbuffers.UOffsetT) *PublicKeyMetadataResponse { + n := flatbuffers.GetUOffsetT(buf[offset:]) + x := &PublicKeyMetadataResponse{} + x.Init(buf, n+offset) + return x +} + +func GetSizePrefixedRootAsPublicKeyMetadataResponse(buf []byte, offset flatbuffers.UOffsetT) *PublicKeyMetadataResponse { + n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:]) + x := &PublicKeyMetadataResponse{} + x.Init(buf, n+offset+flatbuffers.SizeUint32) + return x +} + +func (rcv *PublicKeyMetadataResponse) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *PublicKeyMetadataResponse) Table() flatbuffers.Table { + return rcv._tab +} + +func (rcv *PublicKeyMetadataResponse) Output(obj *PublicKeyMetadata) *PublicKeyMetadata { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + x := rcv._tab.Indirect(o + rcv._tab.Pos) + if obj == nil { + obj = new(PublicKeyMetadata) + } + obj.Init(rcv._tab.Bytes, x) + return obj + } + return nil +} + +func (rcv *PublicKeyMetadataResponse) Error() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + +func PublicKeyMetadataResponseStart(builder *flatbuffers.Builder) { + builder.StartObject(2) +} +func PublicKeyMetadataResponseAddOutput(builder *flatbuffers.Builder, output flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(output), 0) +} +func PublicKeyMetadataResponseAddError(builder *flatbuffers.Builder, error flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(error), 0) +} +func PublicKeyMetadataResponseEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/flatbuffers/bridge.fbs b/flatbuffers/bridge.fbs index 19fc2c8..6932a5d 100644 --- a/flatbuffers/bridge.fbs +++ b/flatbuffers/bridge.fbs @@ -157,6 +157,18 @@ table ArmorEncodeRequest { packet:[ubyte]; } +table GetPublicKeyMetadataRequest { + public_key:string; +} + +table GetPrivateKeyMetadataRequest { + private_key:string; +} + +table ConvertPrivateKeyToPublicKeyRequest { + private_key:string; +} + /// KeyOptions collects a number of parameters along with sensible defaults. table KeyOptions { /// Hash is the default hash function to be used. @@ -242,6 +254,16 @@ table KeyPair { private_key:string; } +table PublicKeyMetadataResponse { + output:model.PublicKeyMetadata; + error:string; +} + +table PrivateKeyMetadataResponse { + output:model.PrivateKeyMetadata; + error:string; +} + table PublicKeyMetadata { key_id:string; key_id_short:string; diff --git a/openpgp/convert.go b/openpgp/convert.go new file mode 100644 index 0000000..e94f9cb --- /dev/null +++ b/openpgp/convert.go @@ -0,0 +1,38 @@ +package openpgp + +import ( + "bytes" + "errors" + "fmt" + "github.com/ProtonMail/go-crypto/openpgp" + "github.com/ProtonMail/go-crypto/openpgp/armor" +) + +func (o *FastOpenPGP) ConvertPrivateKeyToPublicKey(key string) (string, error) { + entityList, err := o.readArmoredKeyRing(key, openpgp.PrivateKeyType) + if err != nil { + return "", fmt.Errorf("publicKey error: %w", err) + } + if len(entityList) < 1 { + return "", fmt.Errorf("publicKey error: %w", errors.New("no key found")) + } + + publicKey := entityList[0].PrimaryKey + if publicKey == nil { + return "", fmt.Errorf("publicKey error: %w", errors.New("no publicKey found")) + } + + publicKeyBuf := bytes.NewBuffer(nil) + writerPublic, err := armor.Encode(publicKeyBuf, openpgp.PublicKeyType, headers) + if err != nil { + return "", err + } + defer writerPublic.Close() + + err = publicKey.Serialize(writerPublic) + if err != nil { + return "", err + } + + return publicKeyBuf.String(), nil +} diff --git a/openpgp/convert_test.go b/openpgp/convert_test.go new file mode 100644 index 0000000..26ae6d5 --- /dev/null +++ b/openpgp/convert_test.go @@ -0,0 +1,19 @@ +package openpgp + +import ( + "encoding/json" + "testing" +) + +func TestFastOpenPGP_ConvertPrivateKeyToPublicKey(t *testing.T) { + + openPGP := NewFastOpenPGP() + output, err := openPGP.ConvertPrivateKeyToPublicKey(privateKey) + if err != nil { + t.Fatal(err) + } + data, _ := json.MarshalIndent(output, "", " ") + t.Log(string(data)) + + t.Log("output:", output) +} diff --git a/openpgp/metadata_test.go b/openpgp/metadata_test.go index b7d7a66..eca034a 100644 --- a/openpgp/metadata_test.go +++ b/openpgp/metadata_test.go @@ -18,6 +18,19 @@ func TestFastOpenPGP_GetPublicKeyMetadata(t *testing.T) { t.Log("output:", output) } +func TestFastOpenPGP_GetPublicKeyMetadataWithPrivateKeyShouldWork(t *testing.T) { + + openPGP := NewFastOpenPGP() + output, err := openPGP.GetPublicKeyMetadata(privateKey) + if err != nil { + t.Fatal(err) + } + data, _ := json.MarshalIndent(output, "", " ") + t.Log(string(data)) + + t.Log("output:", output) +} + func TestFastOpenPGP_GetPrivateKeyMetadata(t *testing.T) { openPGP := NewFastOpenPGP() @@ -30,3 +43,14 @@ func TestFastOpenPGP_GetPrivateKeyMetadata(t *testing.T) { t.Log("output:", output) } + +func TestFastOpenPGP_GetPrivateKeyMetadataWithPublic(t *testing.T) { + + openPGP := NewFastOpenPGP() + output, err := openPGP.GetPrivateKeyMetadata(publicKey) + if err == nil { + t.Fatal("must return error") + } + + t.Log("output:", output) +} diff --git a/wasm/example/libs/bridge.ts b/wasm/example/libs/bridge.ts index 9e984ed..9d6231c 100644 --- a/wasm/example/libs/bridge.ts +++ b/wasm/example/libs/bridge.ts @@ -6,3 +6,5 @@ export { Hash } from './model/hash'; export { KeyOptions } from './model/key-options'; export { KeyPair } from './model/key-pair'; export { Options } from './model/options'; +export { PrivateKeyMetadata } from './model/private-key-metadata'; +export { PublicKeyMetadata } from './model/public-key-metadata'; diff --git a/wasm/example/libs/model/armor-decode-request.ts b/wasm/example/libs/model/armor-decode-request.ts deleted file mode 100644 index 015e3bb..0000000 --- a/wasm/example/libs/model/armor-decode-request.ts +++ /dev/null @@ -1,48 +0,0 @@ -// automatically generated by the FlatBuffers compiler, do not modify - -import * as flatbuffers from 'flatbuffers'; - -export class ArmorDecodeRequest { - bb: flatbuffers.ByteBuffer|null = null; - bb_pos = 0; -__init(i:number, bb:flatbuffers.ByteBuffer):ArmorDecodeRequest { - this.bb_pos = i; - this.bb = bb; - return this; -} - -static getRootAsArmorDecodeRequest(bb:flatbuffers.ByteBuffer, obj?:ArmorDecodeRequest):ArmorDecodeRequest { - return (obj || new ArmorDecodeRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb); -} - -static getSizePrefixedRootAsArmorDecodeRequest(bb:flatbuffers.ByteBuffer, obj?:ArmorDecodeRequest):ArmorDecodeRequest { - bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); - return (obj || new ArmorDecodeRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb); -} - -packet():string|null -packet(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null -packet(optionalEncoding?:any):string|Uint8Array|null { - const offset = this.bb!.__offset(this.bb_pos, 4); - return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null; -} - -static startArmorDecodeRequest(builder:flatbuffers.Builder) { - builder.startObject(1); -} - -static addPacket(builder:flatbuffers.Builder, packetOffset:flatbuffers.Offset) { - builder.addFieldOffset(0, packetOffset, 0); -} - -static endArmorDecodeRequest(builder:flatbuffers.Builder):flatbuffers.Offset { - const offset = builder.endObject(); - return offset; -} - -static createArmorDecodeRequest(builder:flatbuffers.Builder, packetOffset:flatbuffers.Offset):flatbuffers.Offset { - ArmorDecodeRequest.startArmorDecodeRequest(builder); - ArmorDecodeRequest.addPacket(builder, packetOffset); - return ArmorDecodeRequest.endArmorDecodeRequest(builder); -} -} diff --git a/wasm/example/libs/model/convert-private-key-to-public-key-request.ts b/wasm/example/libs/model/convert-private-key-to-public-key-request.ts new file mode 100644 index 0000000..2caa1f3 --- /dev/null +++ b/wasm/example/libs/model/convert-private-key-to-public-key-request.ts @@ -0,0 +1,48 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +import * as flatbuffers from 'flatbuffers'; + +export class ConvertPrivateKeyToPublicKeyRequest { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; +__init(i:number, bb:flatbuffers.ByteBuffer):ConvertPrivateKeyToPublicKeyRequest { + this.bb_pos = i; + this.bb = bb; + return this; +} + +static getRootAsConvertPrivateKeyToPublicKeyRequest(bb:flatbuffers.ByteBuffer, obj?:ConvertPrivateKeyToPublicKeyRequest):ConvertPrivateKeyToPublicKeyRequest { + return (obj || new ConvertPrivateKeyToPublicKeyRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +static getSizePrefixedRootAsConvertPrivateKeyToPublicKeyRequest(bb:flatbuffers.ByteBuffer, obj?:ConvertPrivateKeyToPublicKeyRequest):ConvertPrivateKeyToPublicKeyRequest { + bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); + return (obj || new ConvertPrivateKeyToPublicKeyRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +privateKey():string|null +privateKey(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null +privateKey(optionalEncoding?:any):string|Uint8Array|null { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null; +} + +static startConvertPrivateKeyToPublicKeyRequest(builder:flatbuffers.Builder) { + builder.startObject(1); +} + +static addPrivateKey(builder:flatbuffers.Builder, privateKeyOffset:flatbuffers.Offset) { + builder.addFieldOffset(0, privateKeyOffset, 0); +} + +static endConvertPrivateKeyToPublicKeyRequest(builder:flatbuffers.Builder):flatbuffers.Offset { + const offset = builder.endObject(); + return offset; +} + +static createConvertPrivateKeyToPublicKeyRequest(builder:flatbuffers.Builder, privateKeyOffset:flatbuffers.Offset):flatbuffers.Offset { + ConvertPrivateKeyToPublicKeyRequest.startConvertPrivateKeyToPublicKeyRequest(builder); + ConvertPrivateKeyToPublicKeyRequest.addPrivateKey(builder, privateKeyOffset); + return ConvertPrivateKeyToPublicKeyRequest.endConvertPrivateKeyToPublicKeyRequest(builder); +} +} diff --git a/wasm/example/libs/model/get-private-key-metadata-request.ts b/wasm/example/libs/model/get-private-key-metadata-request.ts new file mode 100644 index 0000000..2213968 --- /dev/null +++ b/wasm/example/libs/model/get-private-key-metadata-request.ts @@ -0,0 +1,48 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +import * as flatbuffers from 'flatbuffers'; + +export class GetPrivateKeyMetadataRequest { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; +__init(i:number, bb:flatbuffers.ByteBuffer):GetPrivateKeyMetadataRequest { + this.bb_pos = i; + this.bb = bb; + return this; +} + +static getRootAsGetPrivateKeyMetadataRequest(bb:flatbuffers.ByteBuffer, obj?:GetPrivateKeyMetadataRequest):GetPrivateKeyMetadataRequest { + return (obj || new GetPrivateKeyMetadataRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +static getSizePrefixedRootAsGetPrivateKeyMetadataRequest(bb:flatbuffers.ByteBuffer, obj?:GetPrivateKeyMetadataRequest):GetPrivateKeyMetadataRequest { + bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); + return (obj || new GetPrivateKeyMetadataRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +privateKey():string|null +privateKey(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null +privateKey(optionalEncoding?:any):string|Uint8Array|null { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null; +} + +static startGetPrivateKeyMetadataRequest(builder:flatbuffers.Builder) { + builder.startObject(1); +} + +static addPrivateKey(builder:flatbuffers.Builder, privateKeyOffset:flatbuffers.Offset) { + builder.addFieldOffset(0, privateKeyOffset, 0); +} + +static endGetPrivateKeyMetadataRequest(builder:flatbuffers.Builder):flatbuffers.Offset { + const offset = builder.endObject(); + return offset; +} + +static createGetPrivateKeyMetadataRequest(builder:flatbuffers.Builder, privateKeyOffset:flatbuffers.Offset):flatbuffers.Offset { + GetPrivateKeyMetadataRequest.startGetPrivateKeyMetadataRequest(builder); + GetPrivateKeyMetadataRequest.addPrivateKey(builder, privateKeyOffset); + return GetPrivateKeyMetadataRequest.endGetPrivateKeyMetadataRequest(builder); +} +} diff --git a/wasm/example/libs/model/get-public-key-metadata-request.ts b/wasm/example/libs/model/get-public-key-metadata-request.ts new file mode 100644 index 0000000..28ed340 --- /dev/null +++ b/wasm/example/libs/model/get-public-key-metadata-request.ts @@ -0,0 +1,48 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +import * as flatbuffers from 'flatbuffers'; + +export class GetPublicKeyMetadataRequest { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; +__init(i:number, bb:flatbuffers.ByteBuffer):GetPublicKeyMetadataRequest { + this.bb_pos = i; + this.bb = bb; + return this; +} + +static getRootAsGetPublicKeyMetadataRequest(bb:flatbuffers.ByteBuffer, obj?:GetPublicKeyMetadataRequest):GetPublicKeyMetadataRequest { + return (obj || new GetPublicKeyMetadataRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +static getSizePrefixedRootAsGetPublicKeyMetadataRequest(bb:flatbuffers.ByteBuffer, obj?:GetPublicKeyMetadataRequest):GetPublicKeyMetadataRequest { + bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); + return (obj || new GetPublicKeyMetadataRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +publicKey():string|null +publicKey(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null +publicKey(optionalEncoding?:any):string|Uint8Array|null { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null; +} + +static startGetPublicKeyMetadataRequest(builder:flatbuffers.Builder) { + builder.startObject(1); +} + +static addPublicKey(builder:flatbuffers.Builder, publicKeyOffset:flatbuffers.Offset) { + builder.addFieldOffset(0, publicKeyOffset, 0); +} + +static endGetPublicKeyMetadataRequest(builder:flatbuffers.Builder):flatbuffers.Offset { + const offset = builder.endObject(); + return offset; +} + +static createGetPublicKeyMetadataRequest(builder:flatbuffers.Builder, publicKeyOffset:flatbuffers.Offset):flatbuffers.Offset { + GetPublicKeyMetadataRequest.startGetPublicKeyMetadataRequest(builder); + GetPublicKeyMetadataRequest.addPublicKey(builder, publicKeyOffset); + return GetPublicKeyMetadataRequest.endGetPublicKeyMetadataRequest(builder); +} +} diff --git a/wasm/example/libs/model/int-response.ts b/wasm/example/libs/model/int-response.ts index 8371b3d..8f8c770 100644 --- a/wasm/example/libs/model/int-response.ts +++ b/wasm/example/libs/model/int-response.ts @@ -20,12 +20,12 @@ static getSizePrefixedRootAsIntResponse(bb:flatbuffers.ByteBuffer, obj?:IntRespo return (obj || new IntResponse()).__init(bb.readInt32(bb.position()) + bb.position(), bb); } -output():bigint { +output():flatbuffers.Long { const offset = this.bb!.__offset(this.bb_pos, 4); - return offset ? this.bb!.readInt64(this.bb_pos + offset) : BigInt('0'); + return offset ? this.bb!.readInt64(this.bb_pos + offset) : this.bb!.createLong(0, 0); } -mutate_output(value:bigint):boolean { +mutate_output(value:flatbuffers.Long):boolean { const offset = this.bb!.__offset(this.bb_pos, 4); if (offset === 0) { @@ -47,8 +47,8 @@ static startIntResponse(builder:flatbuffers.Builder) { builder.startObject(2); } -static addOutput(builder:flatbuffers.Builder, output:bigint) { - builder.addFieldInt64(0, output, BigInt('0')); +static addOutput(builder:flatbuffers.Builder, output:flatbuffers.Long) { + builder.addFieldInt64(0, output, builder.createLong(0, 0)); } static addError(builder:flatbuffers.Builder, errorOffset:flatbuffers.Offset) { @@ -60,7 +60,7 @@ static endIntResponse(builder:flatbuffers.Builder):flatbuffers.Offset { return offset; } -static createIntResponse(builder:flatbuffers.Builder, output:bigint, errorOffset:flatbuffers.Offset):flatbuffers.Offset { +static createIntResponse(builder:flatbuffers.Builder, output:flatbuffers.Long, errorOffset:flatbuffers.Offset):flatbuffers.Offset { IntResponse.startIntResponse(builder); IntResponse.addOutput(builder, output); IntResponse.addError(builder, errorOffset); diff --git a/wasm/example/libs/model/private-key-metadata-response.ts b/wasm/example/libs/model/private-key-metadata-response.ts new file mode 100644 index 0000000..4b1a154 --- /dev/null +++ b/wasm/example/libs/model/private-key-metadata-response.ts @@ -0,0 +1,61 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +import * as flatbuffers from 'flatbuffers'; + +import { PrivateKeyMetadata } from '../model/private-key-metadata'; + + +export class PrivateKeyMetadataResponse { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; +__init(i:number, bb:flatbuffers.ByteBuffer):PrivateKeyMetadataResponse { + this.bb_pos = i; + this.bb = bb; + return this; +} + +static getRootAsPrivateKeyMetadataResponse(bb:flatbuffers.ByteBuffer, obj?:PrivateKeyMetadataResponse):PrivateKeyMetadataResponse { + return (obj || new PrivateKeyMetadataResponse()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +static getSizePrefixedRootAsPrivateKeyMetadataResponse(bb:flatbuffers.ByteBuffer, obj?:PrivateKeyMetadataResponse):PrivateKeyMetadataResponse { + bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); + return (obj || new PrivateKeyMetadataResponse()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +output(obj?:PrivateKeyMetadata):PrivateKeyMetadata|null { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? (obj || new PrivateKeyMetadata()).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null; +} + +error():string|null +error(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null +error(optionalEncoding?:any):string|Uint8Array|null { + const offset = this.bb!.__offset(this.bb_pos, 6); + return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null; +} + +static startPrivateKeyMetadataResponse(builder:flatbuffers.Builder) { + builder.startObject(2); +} + +static addOutput(builder:flatbuffers.Builder, outputOffset:flatbuffers.Offset) { + builder.addFieldOffset(0, outputOffset, 0); +} + +static addError(builder:flatbuffers.Builder, errorOffset:flatbuffers.Offset) { + builder.addFieldOffset(1, errorOffset, 0); +} + +static endPrivateKeyMetadataResponse(builder:flatbuffers.Builder):flatbuffers.Offset { + const offset = builder.endObject(); + return offset; +} + +static createPrivateKeyMetadataResponse(builder:flatbuffers.Builder, outputOffset:flatbuffers.Offset, errorOffset:flatbuffers.Offset):flatbuffers.Offset { + PrivateKeyMetadataResponse.startPrivateKeyMetadataResponse(builder); + PrivateKeyMetadataResponse.addOutput(builder, outputOffset); + PrivateKeyMetadataResponse.addError(builder, errorOffset); + return PrivateKeyMetadataResponse.endPrivateKeyMetadataResponse(builder); +} +} diff --git a/wasm/example/libs/model/public-key-metadata-response.ts b/wasm/example/libs/model/public-key-metadata-response.ts new file mode 100644 index 0000000..ee5c4fa --- /dev/null +++ b/wasm/example/libs/model/public-key-metadata-response.ts @@ -0,0 +1,61 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +import * as flatbuffers from 'flatbuffers'; + +import { PublicKeyMetadata } from '../model/public-key-metadata'; + + +export class PublicKeyMetadataResponse { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; +__init(i:number, bb:flatbuffers.ByteBuffer):PublicKeyMetadataResponse { + this.bb_pos = i; + this.bb = bb; + return this; +} + +static getRootAsPublicKeyMetadataResponse(bb:flatbuffers.ByteBuffer, obj?:PublicKeyMetadataResponse):PublicKeyMetadataResponse { + return (obj || new PublicKeyMetadataResponse()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +static getSizePrefixedRootAsPublicKeyMetadataResponse(bb:flatbuffers.ByteBuffer, obj?:PublicKeyMetadataResponse):PublicKeyMetadataResponse { + bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); + return (obj || new PublicKeyMetadataResponse()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +output(obj?:PublicKeyMetadata):PublicKeyMetadata|null { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? (obj || new PublicKeyMetadata()).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null; +} + +error():string|null +error(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null +error(optionalEncoding?:any):string|Uint8Array|null { + const offset = this.bb!.__offset(this.bb_pos, 6); + return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null; +} + +static startPublicKeyMetadataResponse(builder:flatbuffers.Builder) { + builder.startObject(2); +} + +static addOutput(builder:flatbuffers.Builder, outputOffset:flatbuffers.Offset) { + builder.addFieldOffset(0, outputOffset, 0); +} + +static addError(builder:flatbuffers.Builder, errorOffset:flatbuffers.Offset) { + builder.addFieldOffset(1, errorOffset, 0); +} + +static endPublicKeyMetadataResponse(builder:flatbuffers.Builder):flatbuffers.Offset { + const offset = builder.endObject(); + return offset; +} + +static createPublicKeyMetadataResponse(builder:flatbuffers.Builder, outputOffset:flatbuffers.Offset, errorOffset:flatbuffers.Offset):flatbuffers.Offset { + PublicKeyMetadataResponse.startPublicKeyMetadataResponse(builder); + PublicKeyMetadataResponse.addOutput(builder, outputOffset); + PublicKeyMetadataResponse.addError(builder, errorOffset); + return PublicKeyMetadataResponse.endPublicKeyMetadataResponse(builder); +} +} diff --git a/wasm/example/public/openpgp.wasm b/wasm/example/public/openpgp.wasm index 34c6d89..f993934 100755 Binary files a/wasm/example/public/openpgp.wasm and b/wasm/example/public/openpgp.wasm differ diff --git a/wasm/example/public/wasm_exec.js b/wasm/example/public/wasm_exec.js index 231185a..9ce6a20 100644 --- a/wasm/example/public/wasm_exec.js +++ b/wasm/example/public/wasm_exec.js @@ -2,47 +2,18 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -(() => { - // Map multiple JavaScript environments to a single common API, - // preferring web standards over Node.js API. - // - // Environments considered: - // - Browsers - // - Node.js - // - Electron - // - Parcel - // - Webpack - - if (typeof global !== "undefined") { - // global already exists - } else if (typeof window !== "undefined") { - window.global = window; - } else if (typeof self !== "undefined") { - self.global = self; - } else { - throw new Error("cannot export Go (neither global, window nor self is defined)"); - } - - if (!global.require && typeof require !== "undefined") { - global.require = require; - } - - if (!global.fs && global.require) { - const fs = require("fs"); - if (typeof fs === "object" && fs !== null && Object.keys(fs).length !== 0) { - global.fs = fs; - } - } +"use strict"; +(() => { const enosys = () => { const err = new Error("not implemented"); err.code = "ENOSYS"; return err; }; - if (!global.fs) { + if (!globalThis.fs) { let outputBuf = ""; - global.fs = { + globalThis.fs = { constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused writeSync(fd, buf) { outputBuf += decoder.decode(buf); @@ -87,8 +58,8 @@ }; } - if (!global.process) { - global.process = { + if (!globalThis.process) { + globalThis.process = { getuid() { return -1; }, getgid() { return -1; }, geteuid() { return -1; }, @@ -102,47 +73,26 @@ } } - if (!global.crypto && global.require) { - const nodeCrypto = require("crypto"); - global.crypto = { - getRandomValues(b) { - nodeCrypto.randomFillSync(b); - }, - }; - } - if (!global.crypto) { - throw new Error("global.crypto is not available, polyfill required (getRandomValues only)"); + if (!globalThis.crypto) { + throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)"); } - if (!global.performance) { - global.performance = { - now() { - const [sec, nsec] = process.hrtime(); - return sec * 1000 + nsec / 1000000; - }, - }; + if (!globalThis.performance) { + throw new Error("globalThis.performance is not available, polyfill required (performance.now only)"); } - if (!global.TextEncoder && global.require) { - global.TextEncoder = require("util").TextEncoder; - } - if (!global.TextEncoder) { - throw new Error("global.TextEncoder is not available, polyfill required"); + if (!globalThis.TextEncoder) { + throw new Error("globalThis.TextEncoder is not available, polyfill required"); } - if (!global.TextDecoder && global.require) { - global.TextDecoder = require("util").TextDecoder; - } - if (!global.TextDecoder) { - throw new Error("global.TextDecoder is not available, polyfill required"); + if (!globalThis.TextDecoder) { + throw new Error("globalThis.TextDecoder is not available, polyfill required"); } - // End of polyfills for common API. - const encoder = new TextEncoder("utf-8"); const decoder = new TextDecoder("utf-8"); - global.Go = class { + globalThis.Go = class { constructor() { this.argv = ["js"]; this.env = {}; @@ -517,7 +467,7 @@ null, true, false, - global, + globalThis, this, ]; this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id @@ -526,7 +476,7 @@ [null, 2], [true, 3], [false, 4], - [global, 5], + [globalThis, 5], [this, 6], ]); this._idPool = []; // unused ids that have been garbage collected @@ -567,6 +517,13 @@ offset += 8; }); + // The linker guarantees global data starts from at least wasmMinDataAddr. + // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr. + const wasmMinDataAddr = 4096 + 8192; + if (offset >= wasmMinDataAddr) { + throw new Error("total length of command line and environment variables exceeds limit"); + } + this._inst.exports.run(argc, argv); if (this.exited) { this._resolveExitPromise(); @@ -594,36 +551,4 @@ }; } } - - if ( - typeof module !== "undefined" && - global.require && - global.require.main === module && - global.process && - global.process.versions && - !global.process.versions.electron - ) { - if (process.argv.length < 3) { - console.error("usage: go_js_wasm_exec [wasm binary] [arguments]"); - process.exit(1); - } - - const go = new Go(); - go.argv = process.argv.slice(2); - go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env); - go.exit = process.exit; - WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => { - process.on("exit", (code) => { // Node.js exits if no event handler is pending - if (code === 0 && !go.exited) { - // deadlock, make Go print error and stack traces - go._pendingEvent = { id: 0 }; - go._resume(); - } - }); - return go.run(result.instance); - }).catch((err) => { - console.error(err); - process.exit(1); - }); - } })();