|
| 1 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 2 | +import { TransactionBuilder } from '../transactionBuilder'; |
| 3 | +import { BaseKey, BaseTransaction, BuildTransactionError } from '@bitgo/sdk-core'; |
| 4 | +import { Principal } from '@dfinity/principal'; |
| 5 | +import { createHash } from 'crypto'; |
| 6 | +import utils, { Utils } from '../utils'; |
| 7 | +import { Transaction } from '../transaction'; |
| 8 | +import { UnsignedTransactionBuilder } from '../unsignedTransactionBuilder'; |
| 9 | +import { |
| 10 | + GOVERNANCE_CANISTER_ID, |
| 11 | + IcpTransaction, |
| 12 | + IcpTransactionData, |
| 13 | + OperationType, |
| 14 | + CurveType, |
| 15 | + IcpPublicKey, |
| 16 | + IcpOperation, |
| 17 | + ClaimNeuronParams, |
| 18 | +} from '../iface'; |
| 19 | + |
| 20 | +export class ClaimNeuronBuilder extends TransactionBuilder { |
| 21 | + private _neuronMemo: bigint; |
| 22 | + private utils: Utils; |
| 23 | + |
| 24 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 25 | + super(_coinConfig); |
| 26 | + this._neuronMemo = 0n; |
| 27 | + this.utils = new Utils(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Set the neuron memo for staking |
| 32 | + * |
| 33 | + * @param {bigint} memo - The memo to use for neuron identification |
| 34 | + * @returns {this} The builder instance |
| 35 | + */ |
| 36 | + public neuronMemo(memo: bigint): this { |
| 37 | + this.utils.validateMemo(memo); |
| 38 | + this._neuronMemo = memo; |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Generate the neuron subaccount based on controller principal and memo |
| 44 | + * |
| 45 | + * @param {Principal} controllerPrincipal - The principal ID of the controller |
| 46 | + * @param {bigint} memo - The memo value |
| 47 | + * @returns {Uint8Array} The generated subaccount |
| 48 | + */ |
| 49 | + private getNeuronSubaccount(controllerPrincipal: Principal, memo: bigint): Uint8Array { |
| 50 | + const nonceBuf = Buffer.alloc(8); |
| 51 | + nonceBuf.writeBigUInt64BE(memo); |
| 52 | + const domainSeparator = Buffer.from([0x0c]); |
| 53 | + const context = Buffer.from('neuron-stake', 'utf8'); |
| 54 | + const principalBytes = controllerPrincipal.toUint8Array(); |
| 55 | + |
| 56 | + const hashInput = Buffer.concat([domainSeparator, context, principalBytes, nonceBuf]); |
| 57 | + |
| 58 | + return new Uint8Array(createHash('sha256').update(hashInput).digest()); |
| 59 | + } |
| 60 | + |
| 61 | + /** @inheritdoc */ |
| 62 | + protected async buildImplementation(): Promise<Transaction> { |
| 63 | + if (!this._sender || !this._publicKey) { |
| 64 | + throw new BuildTransactionError('Sender address and public key are required'); |
| 65 | + } |
| 66 | + if (!this._amount) { |
| 67 | + throw new BuildTransactionError('Staking amount is required'); |
| 68 | + } |
| 69 | + |
| 70 | + // Get controller principal from public key |
| 71 | + const controllerPrincipal = this.utils.derivePrincipalFromPublicKey(this._publicKey); |
| 72 | + |
| 73 | + // Generate neuron subaccount |
| 74 | + const subaccount = this.getNeuronSubaccount(controllerPrincipal, this._neuronMemo); |
| 75 | + |
| 76 | + // Set receiver as governance canister with neuron subaccount |
| 77 | + const governancePrincipal = Principal.fromUint8Array(GOVERNANCE_CANISTER_ID); |
| 78 | + this._receiverId = this.utils.fromPrincipal(governancePrincipal, subaccount); |
| 79 | + |
| 80 | + const publicKey: IcpPublicKey = { |
| 81 | + hex_bytes: this._publicKey, |
| 82 | + curve_type: CurveType.SECP256K1, |
| 83 | + }; |
| 84 | + |
| 85 | + const senderOperation: IcpOperation = { |
| 86 | + type: OperationType.TRANSACTION, |
| 87 | + account: { address: this._sender }, |
| 88 | + amount: { |
| 89 | + value: `-${this._amount}`, |
| 90 | + currency: { |
| 91 | + symbol: this._coinConfig.family, |
| 92 | + decimals: this._coinConfig.decimalPlaces, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }; |
| 96 | + |
| 97 | + const receiverOperation: IcpOperation = { |
| 98 | + type: OperationType.TRANSACTION, |
| 99 | + account: { address: this._receiverId }, |
| 100 | + amount: { |
| 101 | + value: this._amount, |
| 102 | + currency: { |
| 103 | + symbol: this._coinConfig.family, |
| 104 | + decimals: this._coinConfig.decimalPlaces, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }; |
| 108 | + |
| 109 | + const feeOperation: IcpOperation = { |
| 110 | + type: OperationType.FEE, |
| 111 | + account: { address: this._sender }, |
| 112 | + amount: { |
| 113 | + value: this.utils.feeData(), |
| 114 | + currency: { |
| 115 | + symbol: this._coinConfig.family, |
| 116 | + decimals: this._coinConfig.decimalPlaces, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }; |
| 120 | + |
| 121 | + const createdTimestamp = this._transaction.createdTimestamp; |
| 122 | + const { metaData, ingressEndTime } = this.utils.getMetaData( |
| 123 | + Number(this._neuronMemo), |
| 124 | + createdTimestamp, |
| 125 | + this._ingressEnd |
| 126 | + ); |
| 127 | + |
| 128 | + const icpTransaction: IcpTransaction = { |
| 129 | + public_keys: [publicKey], |
| 130 | + operations: [senderOperation, receiverOperation, feeOperation], |
| 131 | + metadata: metaData, |
| 132 | + }; |
| 133 | + |
| 134 | + const icpTransactionData: IcpTransactionData = { |
| 135 | + senderAddress: this._sender, |
| 136 | + receiverAddress: this._receiverId, |
| 137 | + amount: this._amount, |
| 138 | + fee: this.utils.feeData(), |
| 139 | + senderPublicKeyHex: this._publicKey, |
| 140 | + transactionType: OperationType.TRANSACTION, |
| 141 | + expiryTime: ingressEndTime, |
| 142 | + memo: Number(this._neuronMemo), |
| 143 | + }; |
| 144 | + |
| 145 | + this._transaction.icpTransactionData = icpTransactionData; |
| 146 | + this._transaction.icpTransaction = icpTransaction; |
| 147 | + |
| 148 | + const unsignedTransactionBuilder = new UnsignedTransactionBuilder(this._transaction.icpTransaction); |
| 149 | + this._transaction.payloadsData = await unsignedTransactionBuilder.getUnsignedTransaction(); |
| 150 | + return this._transaction; |
| 151 | + } |
| 152 | + |
| 153 | + /** @inheritdoc */ |
| 154 | + protected signImplementation(key: BaseKey): BaseTransaction { |
| 155 | + const signatures = utils.getSignatures(this._transaction.payloadsData, this._publicKey, key.key); |
| 156 | + this._transaction.addSignature(signatures); |
| 157 | + return this._transaction; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Get the parameters needed for claiming the neuron after the staking transaction is confirmed. |
| 162 | + * This allows the consumer to handle the network calls themselves. |
| 163 | + * |
| 164 | + * @returns {ClaimNeuronParams} Parameters needed for claiming the neuron |
| 165 | + * @throws {BuildTransactionError} If required fields are missing |
| 166 | + */ |
| 167 | + public getClaimNeuronParams(): ClaimNeuronParams { |
| 168 | + if (!this._publicKey) { |
| 169 | + throw new BuildTransactionError('Public key is required'); |
| 170 | + } |
| 171 | + |
| 172 | + const controllerPrincipal = this.utils.derivePrincipalFromPublicKey(this._publicKey); |
| 173 | + return { |
| 174 | + controller: controllerPrincipal, |
| 175 | + memo: this._neuronMemo, |
| 176 | + }; |
| 177 | + } |
| 178 | +} |
0 commit comments