Bitcoin exchange from first principles