import { AddressHashMode , AddressVersion , RECOVERABLE_ECDSA_SIG_LENGTH_BYTES , StacksMessageType , TransactionVersion , } from './constants' ; import { Buffer } from '@stacks/common' ; import { c32address } from 'c32check' ; export interface Address { readonly type : StacksMessageType . Address ; readonly version : AddressVersion ; readonly hash160 : string ; } export interface MessageSignature { readonly type : StacksMessageType . MessageSignature ; data : string ; } export function createMessageSignature ( signature ) { const length = Buffer . from ( signature , 'hex' ) . byteLength ; if ( length != RECOVERABLE_ECDSA_SIG_LENGTH_BYTES ) { throw Error ( 'Invalid signature' ) ; } return { type : StacksMessageType . MessageSignature , data : signature , } ; } see https export function addressHashModeToVersion ( hashMode , txVersion ) { switch ( hashMode ) { case AddressHashMode . SerializeP2PKH : switch ( txVersion ) { case TransactionVersion . Mainnet : return AddressVersion . MainnetSingleSig ; case TransactionVersion . Testnet : return AddressVersion . TestnetSingleSig ; default : throw new Error ( `Unexpected txVersion ${ JSON . stringify ( txVersion ) } for hashMode ${ hashMode }` ) ; } case AddressHashMode . SerializeP2SH : case AddressHashMode . SerializeP2WPKH : case AddressHashMode . SerializeP2WSH : switch ( txVersion ) { case TransactionVersion . Mainnet : return AddressVersion . MainnetMultiSig ; case TransactionVersion . Testnet : return AddressVersion . TestnetMultiSig ; default : throw new Error ( `Unexpected txVersion ${ JSON . stringify ( txVersion ) } for hashMode ${ hashMode }` ) ; } default : throw new Error ( `Unexpected hashMode ${ JSON . stringify ( hashMode ) }` ) ; } } export function addressFromVersionHash ( version , hash ) { return { type : StacksMessageType . Address , version , hash160 : hash } ; } export function addressToString ( address ) { return c32address ( address . version , address . hash160 ) . toString ( ) ; }