Created
January 21, 2021 12:10
-
-
Save alanshaw/1fcee4c03536696dddfd3118f33a10fe to your computer and use it in GitHub Desktop.
Utility functions for Addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import addr, { Address, newAddress } from '@glif/filecoin-address' | |
import { blake2b } from 'blakejs' | |
import varint from 'varint' | |
/** | |
* PayloadHashLength defines the hash length taken over addresses using the Actor and SECP256K1 protocols. | |
*/ | |
export const payloadHashLength = 20 | |
function addressHash (ingest: Uint8Array): Uint8Array { | |
return blake2b(ingest, undefined, payloadHashLength) | |
} | |
// TODO: PR to @glif/filecoin-address | |
/** | |
* idFromAddress extracts the ID from an ID address. | |
*/ | |
export function idFromAddress (address: Address): number { | |
if (address.protocol() !== addr.Protocol.ID) { | |
throw new Error('cannot get ID from non ID address') | |
} | |
return varint.decode(address.payload()) | |
} | |
/** | |
* newActorAddress returns an address using the Actor protocol. | |
*/ | |
export function newActorAddress (data: Uint8Array): Address { | |
return newAddress(addr.Protocol.ACTOR, addressHash(data)) | |
} | |
/** | |
* newSecp256k1Address returns an address using the SECP256K1 protocol. | |
*/ | |
export function newSecp256k1Address (pubkey: Uint8Array): Address { | |
return newAddress(addr.Protocol.SECP256K1, addressHash(pubkey)) | |
} | |
/** | |
* newBLSAddress returns an address using the BLS protocol. | |
*/ | |
export function newBLSAddress (pubkey: Uint8Array): Address { | |
return newAddress(addr.Protocol.BLS, pubkey) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment