p2p: move some functions to utils.js