diff --git a/bridge/call.go b/bridge/call.go index b46288c..2d5b091 100644 --- a/bridge/call.go +++ b/bridge/call.go @@ -64,6 +64,8 @@ func Call(name string, payload []byte) ([]byte, error) { output = instance.generate(payload) case "armorEncode": output = instance.armorEncode(payload) + case "armorDecode": + output = instance.armorDecode(payload) case "getPublicKeyMetadata": output = instance.getPublicKeyMetadata(payload) case "getPrivateKeyMetadata": @@ -265,9 +267,16 @@ func (m instance) armorEncode(payload []byte) []byte { response := flatbuffers.NewBuilder(0) request := model.GetRootAsArmorEncodeRequest(payload, 0) - output, err := m.instance.ArmorEncode(request.PacketBytes()) + output, err := m.instance.ArmorEncode(request.PacketBytes(), string(request.Type())) return m._stringResponse(response, output, err) } +func (m instance) armorDecode(payload []byte) []byte { + response := flatbuffers.NewBuilder(0) + request := model.GetRootAsArmorDecodeRequest(payload, 0) + + output, err := m.instance.ArmorDecode(string(request.Message())) + return m._armorDecodeResponse(response, output, err) +} func (m instance) getPublicKeyMetadata(payload []byte) []byte { response := flatbuffers.NewBuilder(0) @@ -441,6 +450,29 @@ func (m instance) parseCompression(input model.Compression) string { } } +func (m instance) _armorDecodeResponse(response *flatbuffers.Builder, output *openpgp.ArmorMetadata, err error) []byte { + if err != nil { + outputOffset := response.CreateString(err.Error()) + model.ArmorDecodeResponseStart(response) + model.ArmorDecodeResponseAddError(response, outputOffset) + response.Finish(model.ArmorDecodeResponseEnd(response)) + return response.FinishedBytes() + } + + bodyOffset := response.CreateByteVector(output.Body) + typeOffset := response.CreateString(output.Type) + + model.ArmorMetadataStart(response) + model.ArmorMetadataAddBody(response, bodyOffset) + model.ArmorMetadataAddType(response, typeOffset) + metadata := model.ArmorMetadataEnd(response) + + model.ArmorDecodeResponseStart(response) + model.ArmorDecodeResponseAddOutput(response, metadata) + response.Finish(model.ArmorDecodeResponseEnd(response)) + return response.FinishedBytes() +} + func (m instance) _keyPairResponse(response *flatbuffers.Builder, output *openpgp.KeyPair, err error) []byte { if err != nil { outputOffset := response.CreateString(err.Error()) diff --git a/bridge/model/ArmorDecodeRequest.go b/bridge/model/ArmorDecodeRequest.go new file mode 100644 index 0000000..c4712a6 --- /dev/null +++ b/bridge/model/ArmorDecodeRequest.go @@ -0,0 +1,52 @@ +// 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) Message() []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 ArmorDecodeRequestAddMessage(builder *flatbuffers.Builder, message flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(message), 0) +} +func ArmorDecodeRequestEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/bridge/model/ArmorDecodeResponse.go b/bridge/model/ArmorDecodeResponse.go new file mode 100644 index 0000000..85d525c --- /dev/null +++ b/bridge/model/ArmorDecodeResponse.go @@ -0,0 +1,68 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package model + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) + +type ArmorDecodeResponse struct { + _tab flatbuffers.Table +} + +func GetRootAsArmorDecodeResponse(buf []byte, offset flatbuffers.UOffsetT) *ArmorDecodeResponse { + n := flatbuffers.GetUOffsetT(buf[offset:]) + x := &ArmorDecodeResponse{} + x.Init(buf, n+offset) + return x +} + +func GetSizePrefixedRootAsArmorDecodeResponse(buf []byte, offset flatbuffers.UOffsetT) *ArmorDecodeResponse { + n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:]) + x := &ArmorDecodeResponse{} + x.Init(buf, n+offset+flatbuffers.SizeUint32) + return x +} + +func (rcv *ArmorDecodeResponse) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *ArmorDecodeResponse) Table() flatbuffers.Table { + return rcv._tab +} + +func (rcv *ArmorDecodeResponse) Output(obj *ArmorMetadata) *ArmorMetadata { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + x := rcv._tab.Indirect(o + rcv._tab.Pos) + if obj == nil { + obj = new(ArmorMetadata) + } + obj.Init(rcv._tab.Bytes, x) + return obj + } + return nil +} + +func (rcv *ArmorDecodeResponse) Error() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + +func ArmorDecodeResponseStart(builder *flatbuffers.Builder) { + builder.StartObject(2) +} +func ArmorDecodeResponseAddOutput(builder *flatbuffers.Builder, output flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(output), 0) +} +func ArmorDecodeResponseAddError(builder *flatbuffers.Builder, error flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(error), 0) +} +func ArmorDecodeResponseEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/bridge/model/ArmorEncodeRequest.go b/bridge/model/ArmorEncodeRequest.go index 9d7cb48..41fb9ec 100644 --- a/bridge/model/ArmorEncodeRequest.go +++ b/bridge/model/ArmorEncodeRequest.go @@ -67,8 +67,16 @@ func (rcv *ArmorEncodeRequest) MutatePacket(j int, n byte) bool { return false } +func (rcv *ArmorEncodeRequest) Type() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + func ArmorEncodeRequestStart(builder *flatbuffers.Builder) { - builder.StartObject(1) + builder.StartObject(2) } func ArmorEncodeRequestAddPacket(builder *flatbuffers.Builder, packet flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(packet), 0) @@ -76,6 +84,9 @@ func ArmorEncodeRequestAddPacket(builder *flatbuffers.Builder, packet flatbuffer func ArmorEncodeRequestStartPacketVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(1, numElems, 1) } +func ArmorEncodeRequestAddType(builder *flatbuffers.Builder, type_ flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(type_), 0) +} func ArmorEncodeRequestEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } diff --git a/bridge/model/ArmorMetadata.go b/bridge/model/ArmorMetadata.go new file mode 100644 index 0000000..e4e1da1 --- /dev/null +++ b/bridge/model/ArmorMetadata.go @@ -0,0 +1,92 @@ +// Code generated by the FlatBuffers compiler. DO NOT EDIT. + +package model + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) + +type ArmorMetadata struct { + _tab flatbuffers.Table +} + +func GetRootAsArmorMetadata(buf []byte, offset flatbuffers.UOffsetT) *ArmorMetadata { + n := flatbuffers.GetUOffsetT(buf[offset:]) + x := &ArmorMetadata{} + x.Init(buf, n+offset) + return x +} + +func GetSizePrefixedRootAsArmorMetadata(buf []byte, offset flatbuffers.UOffsetT) *ArmorMetadata { + n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:]) + x := &ArmorMetadata{} + x.Init(buf, n+offset+flatbuffers.SizeUint32) + return x +} + +func (rcv *ArmorMetadata) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *ArmorMetadata) Table() flatbuffers.Table { + return rcv._tab +} + +func (rcv *ArmorMetadata) Body(j int) byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + a := rcv._tab.Vector(o) + return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j*1)) + } + return 0 +} + +func (rcv *ArmorMetadata) BodyLength() int { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + return rcv._tab.VectorLen(o) + } + return 0 +} + +func (rcv *ArmorMetadata) BodyBytes() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + +func (rcv *ArmorMetadata) MutateBody(j int, n byte) bool { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + a := rcv._tab.Vector(o) + return rcv._tab.MutateByte(a+flatbuffers.UOffsetT(j*1), n) + } + return false +} + +func (rcv *ArmorMetadata) Type() []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) + if o != 0 { + return rcv._tab.ByteVector(o + rcv._tab.Pos) + } + return nil +} + +func ArmorMetadataStart(builder *flatbuffers.Builder) { + builder.StartObject(2) +} +func ArmorMetadataAddBody(builder *flatbuffers.Builder, body flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(body), 0) +} +func ArmorMetadataStartBodyVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { + return builder.StartVector(1, numElems, 1) +} +func ArmorMetadataAddType(builder *flatbuffers.Builder, type_ flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(type_), 0) +} +func ArmorMetadataEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/flatbuffers/bridge.fbs b/flatbuffers/bridge.fbs index bb8bedb..86ea185 100644 --- a/flatbuffers/bridge.fbs +++ b/flatbuffers/bridge.fbs @@ -202,6 +202,11 @@ table GenerateRequest { table ArmorEncodeRequest { packet:[ubyte]; + type:string; +} + +table ArmorDecodeRequest { + message:string; } table GetPublicKeyMetadataRequest { @@ -297,6 +302,16 @@ table IntResponse { error:string; } +table ArmorDecodeResponse { + output:model.ArmorMetadata; + error:string; +} + +table ArmorMetadata { + body:[ubyte]; + type:string; +} + table KeyPairResponse { output:model.KeyPair; error:string; diff --git a/openpgp/armor.go b/openpgp/armor.go index 4cfa6cc..6ee91d2 100644 --- a/openpgp/armor.go +++ b/openpgp/armor.go @@ -2,10 +2,13 @@ package openpgp import ( "bytes" + "io/ioutil" + "strings" + "github.com/ProtonMail/go-crypto/openpgp/armor" ) -func (o *FastOpenPGP) ArmorEncode(packet []byte) (string, error) { +func (o *FastOpenPGP) ArmorEncode(packet []byte, messageType string) (string, error) { buf := bytes.NewBuffer(nil) writer, err := armor.Encode(buf, messageType, headers) if err != nil { @@ -21,3 +24,17 @@ func (o *FastOpenPGP) ArmorEncode(packet []byte) (string, error) { } return buf.String(), nil } + +func (o *FastOpenPGP) ArmorDecode(message string) (*ArmorMetadata, error) { + block, err := armor.Decode(strings.NewReader(message)) + if err != nil { + return nil, err + } + + output, err := ioutil.ReadAll(block.Body) + if err != nil { + return nil, err + } + + return &ArmorMetadata{Body: output, Type: block.Type}, nil +} diff --git a/openpgp/armor_test.go b/openpgp/armor_test.go index 523da2f..e50336f 100644 --- a/openpgp/armor_test.go +++ b/openpgp/armor_test.go @@ -13,7 +13,7 @@ cmFuZG9tIHN0cmluZw== func TestFastOpenPGP_ArmorEncode(t *testing.T) { openPGP := NewFastOpenPGP() - output, err := openPGP.ArmorEncode([]byte("random string")) + output, err := openPGP.ArmorEncode([]byte("random string"), messageType) if err != nil { t.Fatal(err) } @@ -24,3 +24,21 @@ func TestFastOpenPGP_ArmorEncode(t *testing.T) { t.Fatal("not same input") } } + +func TestFastOpenPGP_DecodeEncode(t *testing.T) { + + openPGP := NewFastOpenPGP() + output, err := openPGP.ArmorDecode(encoded) + if err != nil { + t.Fatal(err) + } + + t.Log("output:", output) + + if output.Type != messageType { + t.Fatal("not same type:", output.Type) + } + if string(output.Body) != "random string" { + t.Fatal("not same input") + } +} diff --git a/openpgp/entities.go b/openpgp/entities.go index ce4cee3..acd8ff6 100644 --- a/openpgp/entities.go +++ b/openpgp/entities.go @@ -1,5 +1,10 @@ package openpgp +type ArmorMetadata struct { + Body []byte + Type string +} + type KeyPair struct { PublicKey string PrivateKey string diff --git a/wasm/example/libs/bridge.ts b/wasm/example/libs/bridge.ts index 79df345..4a412f1 100644 --- a/wasm/example/libs/bridge.ts +++ b/wasm/example/libs/bridge.ts @@ -1,4 +1,5 @@ export { Algorithm } from './model/algorithm'; +export { ArmorMetadata } from './model/armor-metadata'; export { Cipher } from './model/cipher'; export { Compression } from './model/compression'; export { Curve } from './model/curve'; diff --git a/wasm/example/libs/model/armor-decode-request.ts b/wasm/example/libs/model/armor-decode-request.ts new file mode 100644 index 0000000..c728ae3 --- /dev/null +++ b/wasm/example/libs/model/armor-decode-request.ts @@ -0,0 +1,48 @@ +// 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); +} + +message():string|null +message(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null +message(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 addMessage(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset) { + builder.addFieldOffset(0, messageOffset, 0); +} + +static endArmorDecodeRequest(builder:flatbuffers.Builder):flatbuffers.Offset { + const offset = builder.endObject(); + return offset; +} + +static createArmorDecodeRequest(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset):flatbuffers.Offset { + ArmorDecodeRequest.startArmorDecodeRequest(builder); + ArmorDecodeRequest.addMessage(builder, messageOffset); + return ArmorDecodeRequest.endArmorDecodeRequest(builder); +} +} diff --git a/wasm/example/libs/model/armor-decode-response.ts b/wasm/example/libs/model/armor-decode-response.ts new file mode 100644 index 0000000..e2d561b --- /dev/null +++ b/wasm/example/libs/model/armor-decode-response.ts @@ -0,0 +1,61 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +import * as flatbuffers from 'flatbuffers'; + +import { ArmorMetadata } from '../model/armor-metadata'; + + +export class ArmorDecodeResponse { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; +__init(i:number, bb:flatbuffers.ByteBuffer):ArmorDecodeResponse { + this.bb_pos = i; + this.bb = bb; + return this; +} + +static getRootAsArmorDecodeResponse(bb:flatbuffers.ByteBuffer, obj?:ArmorDecodeResponse):ArmorDecodeResponse { + return (obj || new ArmorDecodeResponse()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +static getSizePrefixedRootAsArmorDecodeResponse(bb:flatbuffers.ByteBuffer, obj?:ArmorDecodeResponse):ArmorDecodeResponse { + bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); + return (obj || new ArmorDecodeResponse()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +output(obj?:ArmorMetadata):ArmorMetadata|null { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? (obj || new ArmorMetadata()).__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 startArmorDecodeResponse(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 endArmorDecodeResponse(builder:flatbuffers.Builder):flatbuffers.Offset { + const offset = builder.endObject(); + return offset; +} + +static createArmorDecodeResponse(builder:flatbuffers.Builder, outputOffset:flatbuffers.Offset, errorOffset:flatbuffers.Offset):flatbuffers.Offset { + ArmorDecodeResponse.startArmorDecodeResponse(builder); + ArmorDecodeResponse.addOutput(builder, outputOffset); + ArmorDecodeResponse.addError(builder, errorOffset); + return ArmorDecodeResponse.endArmorDecodeResponse(builder); +} +} diff --git a/wasm/example/libs/model/armor-encode-request.ts b/wasm/example/libs/model/armor-encode-request.ts index df458b2..5f78c49 100644 --- a/wasm/example/libs/model/armor-encode-request.ts +++ b/wasm/example/libs/model/armor-encode-request.ts @@ -35,8 +35,15 @@ packetArray():Uint8Array|null { return offset ? new Uint8Array(this.bb!.bytes().buffer, this.bb!.bytes().byteOffset + this.bb!.__vector(this.bb_pos + offset), this.bb!.__vector_len(this.bb_pos + offset)) : null; } +type():string|null +type(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null +type(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 startArmorEncodeRequest(builder:flatbuffers.Builder) { - builder.startObject(1); + builder.startObject(2); } static addPacket(builder:flatbuffers.Builder, packetOffset:flatbuffers.Offset) { @@ -55,14 +62,19 @@ static startPacketVector(builder:flatbuffers.Builder, numElems:number) { builder.startVector(1, numElems, 1); } +static addType(builder:flatbuffers.Builder, typeOffset:flatbuffers.Offset) { + builder.addFieldOffset(1, typeOffset, 0); +} + static endArmorEncodeRequest(builder:flatbuffers.Builder):flatbuffers.Offset { const offset = builder.endObject(); return offset; } -static createArmorEncodeRequest(builder:flatbuffers.Builder, packetOffset:flatbuffers.Offset):flatbuffers.Offset { +static createArmorEncodeRequest(builder:flatbuffers.Builder, packetOffset:flatbuffers.Offset, typeOffset:flatbuffers.Offset):flatbuffers.Offset { ArmorEncodeRequest.startArmorEncodeRequest(builder); ArmorEncodeRequest.addPacket(builder, packetOffset); + ArmorEncodeRequest.addType(builder, typeOffset); return ArmorEncodeRequest.endArmorEncodeRequest(builder); } } diff --git a/wasm/example/libs/model/armor-metadata.ts b/wasm/example/libs/model/armor-metadata.ts new file mode 100644 index 0000000..b76115e --- /dev/null +++ b/wasm/example/libs/model/armor-metadata.ts @@ -0,0 +1,80 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +import * as flatbuffers from 'flatbuffers'; + +export class ArmorMetadata { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; +__init(i:number, bb:flatbuffers.ByteBuffer):ArmorMetadata { + this.bb_pos = i; + this.bb = bb; + return this; +} + +static getRootAsArmorMetadata(bb:flatbuffers.ByteBuffer, obj?:ArmorMetadata):ArmorMetadata { + return (obj || new ArmorMetadata()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +static getSizePrefixedRootAsArmorMetadata(bb:flatbuffers.ByteBuffer, obj?:ArmorMetadata):ArmorMetadata { + bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); + return (obj || new ArmorMetadata()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +body(index: number):number|null { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.readUint8(this.bb!.__vector(this.bb_pos + offset) + index) : 0; +} + +bodyLength():number { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0; +} + +bodyArray():Uint8Array|null { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? new Uint8Array(this.bb!.bytes().buffer, this.bb!.bytes().byteOffset + this.bb!.__vector(this.bb_pos + offset), this.bb!.__vector_len(this.bb_pos + offset)) : null; +} + +type():string|null +type(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null +type(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 startArmorMetadata(builder:flatbuffers.Builder) { + builder.startObject(2); +} + +static addBody(builder:flatbuffers.Builder, bodyOffset:flatbuffers.Offset) { + builder.addFieldOffset(0, bodyOffset, 0); +} + +static createBodyVector(builder:flatbuffers.Builder, data:number[]|Uint8Array):flatbuffers.Offset { + builder.startVector(1, data.length, 1); + for (let i = data.length - 1; i >= 0; i--) { + builder.addInt8(data[i]!); + } + return builder.endVector(); +} + +static startBodyVector(builder:flatbuffers.Builder, numElems:number) { + builder.startVector(1, numElems, 1); +} + +static addType(builder:flatbuffers.Builder, typeOffset:flatbuffers.Offset) { + builder.addFieldOffset(1, typeOffset, 0); +} + +static endArmorMetadata(builder:flatbuffers.Builder):flatbuffers.Offset { + const offset = builder.endObject(); + return offset; +} + +static createArmorMetadata(builder:flatbuffers.Builder, bodyOffset:flatbuffers.Offset, typeOffset:flatbuffers.Offset):flatbuffers.Offset { + ArmorMetadata.startArmorMetadata(builder); + ArmorMetadata.addBody(builder, bodyOffset); + ArmorMetadata.addType(builder, typeOffset); + return ArmorMetadata.endArmorMetadata(builder); +} +} diff --git a/wasm/example/pages/caller.ts b/wasm/example/pages/caller.ts index 00b3fd4..049e014 100644 --- a/wasm/example/pages/caller.ts +++ b/wasm/example/pages/caller.ts @@ -6,10 +6,18 @@ import {PublicKeyMetadataResponse} from '../libs/model/public-key-metadata-respo import {KeyPairResponse} from "../libs/model/key-pair-response"; import {PrivateKeyMetadataResponse} from '../libs/model/private-key-metadata-response'; import {GetPrivateKeyMetadataRequest} from '../libs/model/get-private-key-metadata-request'; +import { ArmorDecodeRequest } from '../libs/model/armor-decode-request'; +import { ArmorDecodeResponse } from '../libs/model/armor-decode-response'; export const GenerateSample = async () => { console.log("GenerateSample") +var encoded = `-----BEGIN PGP MESSAGE----- +Version: openpgp-mobile + +cmFuZG9tIHN0cmluZw== +-----END PGP MESSAGE-----` + const builder = new flatbuffers.Builder(0); KeyOptions.startKeyOptions(builder); @@ -55,9 +63,35 @@ export const GenerateSample = async () => { await MetadataPublicKey(output!.publicKey()|| '') await MetadataPrivateKey(output!.privateKey()|| '') + await ArmorDecode(encoded) } + +var ArmorDecode = async (message: string) => { + + console.log("ArmorDecode") + const builder = new flatbuffers.Builder(0); + const messageOffset = builder.createString(message) + + ArmorDecodeRequest.startArmorDecodeRequest(builder) + ArmorDecodeRequest.addMessage(builder, messageOffset); + const offsetMetadata = ArmorDecodeRequest.endArmorDecodeRequest(builder); + builder.finish(offsetMetadata); + const bytes = builder.asUint8Array() + const rawResponse = await sendToWorker('armorDecode', bytes) + + const responseBuffer = new flatbuffers.ByteBuffer(rawResponse); + const response = ArmorDecodeResponse.getRootAsArmorDecodeResponse(responseBuffer) + if (response.error()) { + throw new Error(response.error()!) + } + const output = response.output() + + console.log('bodyArray', output?.bodyArray()); + console.log('type', output?.type()); +} + var MetadataPublicKey = async (publicKey: string) => { console.log("MetadataPublicKey") const builder = new flatbuffers.Builder(0); diff --git a/wasm/example/public/openpgp.wasm b/wasm/example/public/openpgp.wasm index 10ea7eb..19643b7 100755 Binary files a/wasm/example/public/openpgp.wasm and b/wasm/example/public/openpgp.wasm differ