@aptos-labs/ts-sdk
Version:
Aptos TypeScript SDK
61 lines • 2.94 kB
JavaScript
import { sha3_256 } from "@noble/hashes/sha3.js";
import { Serializer } from "../bcs/serializer.js";
import { AccountAddress } from "../core/accountAddress.js";
import { Hex } from "../core/hex.js";
import { AccountAuthenticatorAbstraction } from "../transactions/authenticator/account.js";
import { isValidFunctionInfo } from "../utils/helpers.js";
import { AbstractedAccount } from "./AbstractedAccount.js";
export class DerivableAbstractedAccount extends AbstractedAccount {
/**
* The abstract public key that is used to identify the account.
* Depends on the use cases, most of the time it is the public key of the source wallet
*/
abstractPublicKey;
/**
* The domain separator used to calculate the DAA account address.
*/
static ADDRESS_DOMAIN_SEPERATOR = 5;
constructor({ signer, authenticationFunction, abstractPublicKey }) {
const daaAccountAddress = new AccountAddress(DerivableAbstractedAccount.computeAccountAddress(authenticationFunction, abstractPublicKey));
super({
accountAddress: daaAccountAddress,
signer,
authenticationFunction,
});
this.abstractPublicKey = abstractPublicKey;
}
/**
* Compute the account address of the DAA
* The DAA account address is computed by hashing the function info and the account identity
* and appending the domain separator (5)
*
* @param functionInfo - The authentication function
* @param accountIdentifier - The account identity
* @returns The account address
*/
static computeAccountAddress(functionInfo, accountIdentifier) {
if (!isValidFunctionInfo(functionInfo)) {
throw new Error(`Invalid authentication function ${functionInfo} passed into DerivableAbstractedAccount`);
}
const [moduleAddress, moduleName, functionName] = functionInfo.split("::");
const hash = sha3_256.create();
// Serialize and append the function info
const serializer = new Serializer();
AccountAddress.fromString(moduleAddress).serialize(serializer);
serializer.serializeStr(moduleName);
serializer.serializeStr(functionName);
hash.update(serializer.toUint8Array());
// Serialize and append the account identity
const s2 = new Serializer();
s2.serializeBytes(accountIdentifier);
hash.update(s2.toUint8Array());
// Append the domain separator
hash.update(new Uint8Array([DerivableAbstractedAccount.ADDRESS_DOMAIN_SEPERATOR]));
return hash.digest();
}
signWithAuthenticator(message) {
const messageBytes = Hex.fromHexInput(message).toUint8Array();
return new AccountAuthenticatorAbstraction(this.authenticationFunction, sha3_256(messageBytes), this.sign(sha3_256(messageBytes)).value, this.abstractPublicKey);
}
}
//# sourceMappingURL=DerivableAbstractedAccount.js.map