-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdelegation.ts
48 lines (45 loc) · 1.59 KB
/
delegation.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
import type { DerEncodedPublicKey, Signature } from "@dfinity/agent";
import type { PublicKey } from "./service.interface";
import { Principal } from "@dfinity/principal";
import { Delegation } from "@dfinity/identity";
import { DelegationChain, type SignedDelegation } from "@dfinity/identity";
import type { SignedDelegation as ServiceSignedDelegation } from "./service.interface";
/**
* Converts a Uint8Array or number array to a Signature object.
*/
export function asSignature(signature: Uint8Array | number[]): Signature {
const arrayBuffer: ArrayBuffer = (signature as Uint8Array).buffer;
const s: Signature = arrayBuffer as Signature;
s.__signature__ = undefined;
return s;
}
/**
* Converts a Uint8Array or number array to a DerEncodedPublicKey object.
*/
export function asDerEncodedPublicKey(
publicKey: Uint8Array | number[]
): DerEncodedPublicKey {
const arrayBuffer: ArrayBuffer = (publicKey as Uint8Array).buffer;
const pk: DerEncodedPublicKey = arrayBuffer as DerEncodedPublicKey;
pk.__derEncodedPublicKey__ = undefined;
return pk;
}
export function createDelegationChain(
signedDelegation: ServiceSignedDelegation,
publicKey: PublicKey
) {
const delegations: SignedDelegation[] = [
{
delegation: new Delegation(
(signedDelegation.delegation.pubkey as Uint8Array).buffer,
signedDelegation.delegation.expiration,
signedDelegation.delegation.targets[0] as Principal[]
),
signature: asSignature(signedDelegation.signature),
},
];
return DelegationChain.fromDelegations(
delegations,
asDerEncodedPublicKey(publicKey)
);
}