-
Notifications
You must be signed in to change notification settings - Fork 3
/
RecipientInfo.ts
72 lines (67 loc) · 2.46 KB
/
RecipientInfo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { SmartBuffer } from '@ylide/smart-buffer';
export interface IRecipientInfo {
// resolved address of the recipient that was used to retrieve the key to encrypt the message
address: string;
// blockchain from which the Ylide public key was retrieved
blockchain: string;
// optional alias data, example: {type: 'ENS', data: 'mydomain.eth'}, {type: 'gnosis-safe', data: '0x123...'}
// this alias was used to retrieve actual address of the recipient
alias?: { type: string; data: any };
}
export class RecipientInfo implements IRecipientInfo {
address: string;
blockchain: string;
alias?: { type: string; data: any };
constructor(data: IRecipientInfo) {
this.address = data.address;
this.blockchain = data.blockchain;
this.alias = data.alias;
}
toBytes(): Uint8Array {
const addressBytes = new TextEncoder().encode(this.address);
const blockchainBytes = new TextEncoder().encode(this.blockchain);
const isThereAlias = !!this.alias;
let aliasLength = 0;
let aliasTypeBytes: Uint8Array;
let aliasDataBytes: Uint8Array;
if (this.alias) {
aliasTypeBytes = new TextEncoder().encode(this.alias.type);
aliasDataBytes = new TextEncoder().encode(this.alias.data);
aliasLength =
1 + // alias type length
aliasTypeBytes.length + // alias type
2 + // alias data length
aliasDataBytes.length; // alias data
}
const buf = SmartBuffer.ofSize(
2 + // address length
addressBytes.length + // address
1 + // blockchain length
blockchainBytes.length + // blockchain
1 + // is there alias length
aliasLength,
);
buf.writeBytes16Length(addressBytes);
buf.writeBytes8Length(blockchainBytes);
buf.writeUint8(isThereAlias ? 1 : 0);
if (isThereAlias) {
buf.writeBytes8Length(aliasTypeBytes!);
buf.writeBytes16Length(aliasDataBytes!);
}
return buf.bytes;
}
static fromBytes(bytes: Uint8Array): RecipientInfo {
const buf = new SmartBuffer(bytes);
const address = new TextDecoder().decode(buf.readBytes16Length());
const blockchain = new TextDecoder().decode(buf.readBytes8Length());
const isThereAlias = buf.readUint8() === 1;
if (isThereAlias) {
const aliasType = new TextDecoder().decode(buf.readBytes8Length());
const aliasData = new TextDecoder().decode(buf.readBytes16Length());
return new RecipientInfo({ address, blockchain, alias: { type: aliasType, data: aliasData } });
} else {
return new RecipientInfo({ address, blockchain });
}
}
}