Posted on c/ElrondDevelopers 1 SrY7881514751c/ElrondDevelopers 2 years ago Is there any way to know the shard of address/account? Comment 1 Comments mwf81318617 2years ago /** * Computes a shard for an address. * * @param {Address} address Address to use for calculation * @return {number} Shard the address belongs to * @memberof ElrondService */ public computeShard(address: Address): number { const numShards = 3; const maskHigh = parseInt("11", 2); const maskLow = parseInt("01", 2); const pubKey = address.pubkey(); const lastByteOfPubKey = pubKey[31]; if (this.isAddressOfMetachain(pubKey)) { return 4294967295; } let shard = lastByteOfPubKey & maskHigh; if (shard > numShards - 1) { shard = lastByteOfPubKey & maskLow; } return shard; } /** * Checks if an address belongs to the metachain. (Should only be system scs). * * @private * @param {Buffer} pubKey Public key of the address * @return {boolean} Whether the address belongs to the metachain * @memberof ElrondService */ private isAddressOfMetachain(pubKey: Buffer): boolean { // prettier-ignore const metachainPrefix = Buffer.from([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]); const pubKeyPrefix = pubKey.slice(0, metachainPrefix.length); if (pubKeyPrefix.equals(metachainPrefix)) { return true; } const zeroAddress = Buffer.alloc(32).fill(0); if (pubKey.equals(zeroAddress)) { return true; } return false; }
/** * Computes a shard for an address. * * @param {Address} address Address to use for calculation * @return {number} Shard the address belongs to * @memberof ElrondService */ public computeShard(address: Address): number { const numShards = 3; const maskHigh = parseInt("11", 2); const maskLow = parseInt("01", 2); const pubKey = address.pubkey(); const lastByteOfPubKey = pubKey[31]; if (this.isAddressOfMetachain(pubKey)) { return 4294967295; } let shard = lastByteOfPubKey & maskHigh; if (shard > numShards - 1) { shard = lastByteOfPubKey & maskLow; } return shard; } /** * Checks if an address belongs to the metachain. (Should only be system scs). * * @private * @param {Buffer} pubKey Public key of the address * @return {boolean} Whether the address belongs to the metachain * @memberof ElrondService */ private isAddressOfMetachain(pubKey: Buffer): boolean { // prettier-ignore const metachainPrefix = Buffer.from([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]); const pubKeyPrefix = pubKey.slice(0, metachainPrefix.length); if (pubKeyPrefix.equals(metachainPrefix)) { return true; } const zeroAddress = Buffer.alloc(32).fill(0); if (pubKey.equals(zeroAddress)) { return true; } return false; }