As a general rule, as long as you're in valid H1B status, your writing and publishing a book in your spare time should not be a problem. Such part-time authorship would not constitute being regularly engaged in a profession. The royalties can be classified as passive income and reported on a Schedule E.

Still, as they say, the devil is in the details, so you would likely benefit from discussing your plan more fully with an immigration lawyer.