-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdelegation-signature.ts
88 lines (84 loc) · 3.28 KB
/
delegation-signature.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { decode, Encoded } from '../utils/encoder';
import { ArgumentError } from '../utils/errors';
import { AensName } from '../tx/builder/constants';
import AccountBase from '../account/Base';
import { isNameValid } from '../tx/builder/helpers';
import Node from '../node/Direct';
function ensureOracleQuery(oq: string): asserts oq is Encoded.OracleQueryId {
if (!oq.startsWith('oq_')) throw new ArgumentError('oq', 'oracle query', oq);
}
/**
* Helper to generate a signature to delegate
* - pre-claim/claim/transfer/revoke of a name to a contract.
* - register/extend/respond of an Oracle to a contract.
* @category contract
* @param contractAddress - Address of contract to delegate access
* @param ids - The list of id's to prepend
* @param options - Options
* @param options.omitAddress - Prepend delegation signature with an account address
* @param options.onAccount - Account to use
* @param options.onNode - Node to use
* @returns Signature
* @deprecated use methods `sign*DelegationToContract` of Account instance instead
* @example
* ```js
* const aeSdk = new AeSdk({ ... })
* const contractAddress = 'ct_asd2ks...'
* const aensName = 'example.chain'
* const onAccount = new MemoryAccount(...) // Sign with a specific account
* // Preclaim signature
* const preclaimSig = await aeSdk.createDelegationSignature(contractAddress, [], { onAccount })
* // Claim, transfer and revoke signature
* const aensDelegationSig = await aeSdk
* .createDelegationSignature(contractAddress, [aensName], { onAccount })
*
* const oracleQueryId = 'oq_...'
* const onAccount = new MemoryAccount(...) // Sign with a specific account
* // Oracle register and extend signature
* const oracleDelegationSig = await aeSdk
* .createDelegationSignature(contractAddress, [], { onAccount })
* // Oracle respond signature
* const respondSig = await aeSdk
* .createDelegationSignature(contractAddress, [oracleQueryId], { onAccount, omitAddress: true })
* ```
*/
export default async function createDelegationSignature(
contractAddress: Encoded.ContractAddress,
ids: Array<Encoded.Any | AensName>,
{
onAccount, omitAddress, isOracle, ...options
}: {
omitAddress?: boolean;
onAccount: AccountBase;
isOracle?: boolean;
onNode: Node;
},
): Promise<Uint8Array> {
if (ids.length > 1) throw new ArgumentError('ids', 'shorter than 2', ids);
const { nodeNetworkId, consensusProtocolVersion } = await options.onNode.getNodeInfo();
const signOpts = { networkId: nodeNetworkId, consensusProtocolVersion };
if (ids.length === 0) {
if (omitAddress === true) {
throw new ArgumentError('omitAddress', 'equal false', omitAddress);
}
return decode(
await onAccount.signDelegationToContract(contractAddress, { ...signOpts, isOracle }),
);
}
const [payload] = ids;
if (isNameValid(payload)) {
if (omitAddress === true) {
throw new ArgumentError('omitAddress', 'equal false', omitAddress);
}
return decode(
await onAccount.signNameDelegationToContract(contractAddress, payload, signOpts),
);
}
ensureOracleQuery(payload);
if (omitAddress !== true) {
throw new ArgumentError('omitAddress', 'equal true', omitAddress);
}
return decode(
await onAccount.signOracleQueryDelegationToContract(contractAddress, payload, signOpts),
);
}