From 9f4ce35edc1f1a2873effe5d176a42ac7af511b6 Mon Sep 17 00:00:00 2001 From: Einhornimmond Date: Fri, 1 Sep 2023 09:41:56 +0200 Subject: [PATCH] add first draft of protobuf entities --- dlt-connector/src/graphql/enum/AddressType.ts | 8 +++ .../src/graphql/enum/CrossGroupType.ts | 7 +++ .../src/proto/GradidoConfirmedTransaction.ts | 0 dlt-connector/src/proto/GradidoCreation.ts | 16 +++++ .../src/proto/GradidoDeferredTransfer.ts | 31 ++++++++++ dlt-connector/src/proto/GradidoTransaction.ts | 21 +++++++ dlt-connector/src/proto/GradidoTransfer.ts | 13 ++++ dlt-connector/src/proto/GroupFriendsUpdate.ts | 15 +++++ dlt-connector/src/proto/RegisterAddress.ts | 19 ++++++ dlt-connector/src/proto/SignatureMap.ts | 10 +++ dlt-connector/src/proto/SignaturePair.ts | 11 ++++ dlt-connector/src/proto/Timestamp.ts | 13 ++++ dlt-connector/src/proto/TimestampSeconds.ts | 9 +++ .../src/proto/TransactionBody.test.ts | 10 +-- dlt-connector/src/proto/TransactionBody.ts | 62 ++++++++++++++++--- dlt-connector/src/proto/TransferAmount.ts | 16 +++++ 16 files changed, 247 insertions(+), 14 deletions(-) create mode 100644 dlt-connector/src/graphql/enum/AddressType.ts create mode 100644 dlt-connector/src/graphql/enum/CrossGroupType.ts create mode 100644 dlt-connector/src/proto/GradidoConfirmedTransaction.ts create mode 100644 dlt-connector/src/proto/GradidoCreation.ts create mode 100644 dlt-connector/src/proto/GradidoDeferredTransfer.ts create mode 100644 dlt-connector/src/proto/GradidoTransaction.ts create mode 100644 dlt-connector/src/proto/GradidoTransfer.ts create mode 100644 dlt-connector/src/proto/GroupFriendsUpdate.ts create mode 100644 dlt-connector/src/proto/RegisterAddress.ts create mode 100644 dlt-connector/src/proto/SignatureMap.ts create mode 100644 dlt-connector/src/proto/SignaturePair.ts create mode 100644 dlt-connector/src/proto/Timestamp.ts create mode 100644 dlt-connector/src/proto/TimestampSeconds.ts create mode 100644 dlt-connector/src/proto/TransferAmount.ts diff --git a/dlt-connector/src/graphql/enum/AddressType.ts b/dlt-connector/src/graphql/enum/AddressType.ts new file mode 100644 index 000000000..d75ad9988 --- /dev/null +++ b/dlt-connector/src/graphql/enum/AddressType.ts @@ -0,0 +1,8 @@ +export enum AddressType { + NONE = 0, // if no address was found + HUMAN = 1, + PROJECT = 2, // no creations allowed + SUBACCOUNT = 3, // no creations allowed + CRYPTO_ACCOUNT = 4, // user control his keys, no creations + COMMUNITY_ACCOUNT = 5, // community control keys, creations allowed +} diff --git a/dlt-connector/src/graphql/enum/CrossGroupType.ts b/dlt-connector/src/graphql/enum/CrossGroupType.ts new file mode 100644 index 000000000..921ae4eed --- /dev/null +++ b/dlt-connector/src/graphql/enum/CrossGroupType.ts @@ -0,0 +1,7 @@ +export enum CrossGroupType { + LOCAL = 0, + INBOUND = 1, + OUTBOUND = 2, + // for cross group transaction which haven't a direction like group friend update + CROSS = 3, +} diff --git a/dlt-connector/src/proto/GradidoConfirmedTransaction.ts b/dlt-connector/src/proto/GradidoConfirmedTransaction.ts new file mode 100644 index 000000000..e69de29bb diff --git a/dlt-connector/src/proto/GradidoCreation.ts b/dlt-connector/src/proto/GradidoCreation.ts new file mode 100644 index 000000000..90ca85806 --- /dev/null +++ b/dlt-connector/src/proto/GradidoCreation.ts @@ -0,0 +1,16 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { TimestampSeconds } from './TimestampSeconds' +import { TransferAmount } from './TransferAmount' + +// need signature from group admin or +// percent of group users another than the receiver +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class GradidoCreation extends Message { + @Field.d(1, TransferAmount) + public recipient: TransferAmount + + @Field.d(3, 'TimestampSeconds') + public targetDate: TimestampSeconds +} diff --git a/dlt-connector/src/proto/GradidoDeferredTransfer.ts b/dlt-connector/src/proto/GradidoDeferredTransfer.ts new file mode 100644 index 000000000..a97cc22a7 --- /dev/null +++ b/dlt-connector/src/proto/GradidoDeferredTransfer.ts @@ -0,0 +1,31 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { GradidoTransfer } from './GradidoTransfer' +import { Timestamp } from './Timestamp' + +// transaction type for chargeable transactions +// for transaction for people which haven't a account already +// consider using a seed number for key pair generation for recipient +// using seed as redeem key for claiming transaction, technically make a default Transfer transaction from recipient address +// seed must be long enough to prevent brute force, maybe base64 encoded +// to own account +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class GradidoDeferredTransfer extends Message { + // amount is amount with decay for time span between transaction was received and timeout + // useable amount can be calculated + // recipient address don't need to be registered in blockchain with register address + @Field.d(1, GradidoTransfer) + public transfer: GradidoTransfer + + // if timeout timestamp is reached if it wasn't used, it will be booked back minus decay + // technically on blockchain no additional transaction will be created because how should sign it? + // the decay for amount and the seconds until timeout is lost no matter what happened + // consider is as fee for this service + // rest decay could be transferred back as separate transaction + @Field.d(2, 'Timestamp') + public timeout: Timestamp + + // split for n recipient + // max gradido per recipient? or per transaction with cool down? +} diff --git a/dlt-connector/src/proto/GradidoTransaction.ts b/dlt-connector/src/proto/GradidoTransaction.ts new file mode 100644 index 000000000..ca1a59e30 --- /dev/null +++ b/dlt-connector/src/proto/GradidoTransaction.ts @@ -0,0 +1,21 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { SignatureMap } from './SignatureMap' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class GradidoTransaction extends Message { + @Field.d(1, SignatureMap) + public sigMap: SignatureMap + + // inspired by Hedera + // bodyBytes are the payload for signature + // bodyBytes are serialized TransactionBody + @Field.d(2, 'bytes') + public bodyBytes: Buffer + + // if it is a cross group transaction the parent message + // id from outbound transaction or other by cross + @Field.d(3, 'bytes') + public parentMessageId: Buffer +} diff --git a/dlt-connector/src/proto/GradidoTransfer.ts b/dlt-connector/src/proto/GradidoTransfer.ts new file mode 100644 index 000000000..203b281ad --- /dev/null +++ b/dlt-connector/src/proto/GradidoTransfer.ts @@ -0,0 +1,13 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { TransferAmount } from './TransferAmount' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class GradidoTransfer extends Message { + @Field.d(1, TransferAmount) + public sender: TransferAmount + + @Field.d(2, 'bytes') + public recipient: Buffer +} diff --git a/dlt-connector/src/proto/GroupFriendsUpdate.ts b/dlt-connector/src/proto/GroupFriendsUpdate.ts new file mode 100644 index 000000000..64e74a694 --- /dev/null +++ b/dlt-connector/src/proto/GroupFriendsUpdate.ts @@ -0,0 +1,15 @@ +import { Field, Message } from '@apollo/protobufjs' + +// connect group together +// only CrossGroupType CROSS (in TransactionBody) +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class GroupFriendsUpdate extends Message { + // if set to true, colors of this both groups are trait as the same + // on creation user get coins still in there color + // on transfer into another group which a connection exist, + // coins will be automatic swapped into user group color coin + // (if fusion between src coin and dst coin is enabled) + @Field.d(1, 'bool') + public colorFusion: boolean +} diff --git a/dlt-connector/src/proto/RegisterAddress.ts b/dlt-connector/src/proto/RegisterAddress.ts new file mode 100644 index 000000000..85b8390df --- /dev/null +++ b/dlt-connector/src/proto/RegisterAddress.ts @@ -0,0 +1,19 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { AddressType } from '@enum/AddressType' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class RegisterAddress extends Message { + @Field.d(1, 'bytes') + public userPubkey: Buffer + + @Field.d(2, 'AddressType') + public addressType: AddressType + + @Field.d(3, 'bytes') + public nameHash: Buffer + + @Field.d(4, 'bytes') + public subaccountPubkey: Buffer +} diff --git a/dlt-connector/src/proto/SignatureMap.ts b/dlt-connector/src/proto/SignatureMap.ts new file mode 100644 index 000000000..e48b0232d --- /dev/null +++ b/dlt-connector/src/proto/SignatureMap.ts @@ -0,0 +1,10 @@ +import { Field, Message } from '@apollo/protobufjs' + +import { SignaturePair } from './SignaturePair' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class SignatureMap extends Message { + @Field.d(1, SignaturePair, 'repeated') + public sigPair: SignaturePair +} diff --git a/dlt-connector/src/proto/SignaturePair.ts b/dlt-connector/src/proto/SignaturePair.ts new file mode 100644 index 000000000..07ed4cc55 --- /dev/null +++ b/dlt-connector/src/proto/SignaturePair.ts @@ -0,0 +1,11 @@ +import { Field, Message } from '@apollo/protobufjs' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class SignaturePair extends Message { + @Field.d(1, 'bytes') + public pubKey: Buffer + + @Field.d(2, 'bytes') + public signature: Buffer +} diff --git a/dlt-connector/src/proto/Timestamp.ts b/dlt-connector/src/proto/Timestamp.ts new file mode 100644 index 000000000..2d3335272 --- /dev/null +++ b/dlt-connector/src/proto/Timestamp.ts @@ -0,0 +1,13 @@ +import { Field, Message } from '@apollo/protobufjs' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class Timestamp extends Message { + // Number of complete seconds since the start of the epoch + @Field.d(1, 'int64') + public seconds: number + + // Number of nanoseconds since the start of the last second + @Field.d(2, 'int32') + public nanoSeconds: number +} diff --git a/dlt-connector/src/proto/TimestampSeconds.ts b/dlt-connector/src/proto/TimestampSeconds.ts new file mode 100644 index 000000000..9a8c5b9de --- /dev/null +++ b/dlt-connector/src/proto/TimestampSeconds.ts @@ -0,0 +1,9 @@ +import { Field, Message } from '@apollo/protobufjs' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class TimestampSeconds extends Message { + // Number of complete seconds since the start of the epoch + @Field.d(1, 'int64') + public seconds: number +} diff --git a/dlt-connector/src/proto/TransactionBody.test.ts b/dlt-connector/src/proto/TransactionBody.test.ts index 168d15b63..c9315ba81 100644 --- a/dlt-connector/src/proto/TransactionBody.test.ts +++ b/dlt-connector/src/proto/TransactionBody.test.ts @@ -3,25 +3,27 @@ import { TransactionType } from '@enum/TransactionType' import { TransactionInput } from '@input/TransactionInput' import Decimal from 'decimal.js-light' import { TransactionBody } from './TransactionBody' +import { TimestampSeconds } from './TimestampSeconds' describe('proto/TransactionBodyTest', () => { it('test compatible with graphql/input/TransactionInput', async () => { // test data const type = TransactionType.SEND const amount = new Decimal('10') - const createdAt = 1688992436 + const createdAt = new TimestampSeconds() + createdAt.seconds = 1688992436 // init both objects // graphql input object const transactionInput = new TransactionInput() transactionInput.type = type transactionInput.amount = amount - transactionInput.createdAt = createdAt + transactionInput.createdAt = createdAt.seconds // protobuf object const transactionBody = new TransactionBody() - transactionBody.type = type - transactionBody.amount = amount.toString() + // transactionBody.type = type + // transactionBody.amount = amount.toString() transactionBody.createdAt = createdAt // create protobuf object from graphql Input object diff --git a/dlt-connector/src/proto/TransactionBody.ts b/dlt-connector/src/proto/TransactionBody.ts index 8fda5b863..bb90f788a 100644 --- a/dlt-connector/src/proto/TransactionBody.ts +++ b/dlt-connector/src/proto/TransactionBody.ts @@ -1,18 +1,60 @@ -import { TransactionType } from '../graphql/enum/TransactionType' -import { Field, Message } from '@apollo/protobufjs' +import { Field, Message, OneOf } from '@apollo/protobufjs' + +import { CrossGroupType } from '@/graphql/enum/CrossGroupType' + +import { TimestampSeconds } from './TimestampSeconds' +import { GradidoTransfer } from './GradidoTransfer' +import { GradidoCreation } from './GradidoCreation' +import { GradidoDeferredTransfer } from './GradidoDeferredTransfer' +import { GroupFriendsUpdate } from './GroupFriendsUpdate' +import { RegisterAddress } from './RegisterAddress' + +/*interface OneofExample { + result: + | { oneofKind: 'value'; value: number } + | { oneofKind: 'error'; error: string } + | { oneofKind: undefined } +}*/ // https://www.npmjs.com/package/@apollo/protobufjs // eslint-disable-next-line no-use-before-define export class TransactionBody extends Message { - @Field.d(1, TransactionType) - type: TransactionType + @Field.d(1, 'string') + public memo: string - @Field.d(2, 'string') - amount: string + @Field.d(2, TimestampSeconds) + public createdAt: TimestampSeconds - @Field.d(3, 'uint64') - createdAt: number + @Field.d(3, 'string') + public versionNumber: string - // @protoField.d(4, 'string') - // communitySum: Decimal + @Field.d(4, CrossGroupType) + public type: CrossGroupType + + @Field.d(5, 'string') + public otherGroup: string + + @OneOf.d( + 'gradidoTransfer', + 'gradidoCreation', + 'groupFriendsUpdate', + 'registerAddress', + 'gradidoDeferredTransfer', + ) + public data: string + + @Field.d(6, 'GradidoTransfer') + transfer?: GradidoTransfer + + @Field.d(7, 'GradidoCreation') + creation?: GradidoCreation + + @Field.d(8, 'GroupFriendsUpdate') + groupFriendsUpdate?: GroupFriendsUpdate + + @Field.d(9, 'RegisterAddress') + registerAddress?: RegisterAddress + + @Field.d(10, 'GradidoDeferredTransfer') + deferredTransfer?: GradidoDeferredTransfer } diff --git a/dlt-connector/src/proto/TransferAmount.ts b/dlt-connector/src/proto/TransferAmount.ts new file mode 100644 index 000000000..f6adc47ff --- /dev/null +++ b/dlt-connector/src/proto/TransferAmount.ts @@ -0,0 +1,16 @@ +import { Field, Message } from '@apollo/protobufjs' + +// https://www.npmjs.com/package/@apollo/protobufjs +// eslint-disable-next-line no-use-before-define +export class TransferAmount extends Message { + @Field.d(1, 'bytes') + public pubkey: Buffer + + @Field.d(2, 'string') + public amount: string + + // community which created this coin + // used for colored coins + @Field.d(3, 'string') + public communityId: string +}